#TouhouDanmakufu
#ScriptVersion[3]
#Title["NS4_L"]
#System["./system/system.txt"]
#Background["./background.txt"]
#Text["Nonspell 4 Lunatic"]

#include"./system/function.txt"

let frame = 0;
let rad = 0;
let angle = 0;
let objBoss;
let imgBoss = GetCurrentScriptDirectory ~ "img/boss.png";

@Initialize {
   DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
   SetAutoDeleteObject(true);
   objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
   ObjEnemy_Regist(objBoss);
   ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-70, 10, 7);
   mainTask;
}

@Event {
   alternative(GetEventType())
   case(EV_REQUEST_LIFE){
   	SetScriptResult(3000);
   }
   case(EV_REQUEST_TIMER){
	SetScriptResult(45);
   }
}

@MainLoop {
	frame++; 
	let bossx = ObjMove_GetX(objBoss);
	let bossy = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss, bossx, bossy, 40);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, bossx, bossy, 12);
	
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){
		Obj_Delete(objBoss); 
		DeleteShotAll(TYPE_ALL,TYPE_ITEM); 
		SetAutoDeleteObject(true);
		CloseScript(GetOwnScriptID()); 
	}
	yield;   
}

@Finalize {

}

task mainTask {
	renderBoss("Cirno");
	Circle(objBoss);
	FireBoss(objBoss, 200, 255);
	moveTask;
	
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	loop(90){yield;}
	ObjEnemy_SetDamageRate(objBoss, 60, 60);
	
	while(!Obj_IsDeleted(objBoss)){
		loop{
			loop(20){
				PlaySound("se_tan02");
				ascent(i in 0..8){
					ascent(a in 0..3){
						DelayCloudBulletA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2 + a/2, frame/2 + 360/8*i, 86);
					}
				}
			loop(4){yield;}
			}
			loop(20){
				PlaySound("se_tan01");
				ascent(i in 0..8){
					ascent(a in 0..2){
						DelayCloudBulletA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3.25 + a/2, -frame*6 + 360/8*i, 87);
					}
				}
			loop(2){yield;}
			}
			loop(150){yield;}
			angle = atan2(GetPlayerY - ObjMove_GetY(objBoss), GetPlayerX - ObjMove_GetX(objBoss));
			ascent(a in 0..8){
				ascent(b in 0..10){
					PlaySound("se_kira00");
					ascent(i in 0..6){
						DelayCloudBulletA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3 - b/5, angle + 360/6*i + a*4, 81);
						DelayCloudBulletA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3 - b/5, angle + 360/6*i - a*4, 81);
					}
				loop(1){yield;}
				}
			}
			loop(150){yield;}
			}
	}
}

task renderBoss(name){
	BossName(name, objBoss);
	
	let i = 0;
	
	ObjPrim_SetTexture(objBoss, imgBoss);
	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(objBoss);
	ObjRender_SetScaleXYZ(objBoss, 0.35, 0.35, 0);
	
	while(!Obj_IsDeleted(objBoss)){
		i++;
		yield;
	}
}

task moveTask{
	let i = 0;
	
	while(!Obj_IsDeleted(objBoss)){
		i++;
		if(i % 300 == 0){
			ObjMove_SetDestAtWeight(objBoss, rand(GetStgFrameLeft + 50, GetStgFrameWidth - 50), rand(70, GetStgFrameHeight/2 - 70), 10, 1);
		}
		yield;
	}
}