#ScriptVersion[3]

#include "./../../lib_cutin.txt"
#include "./../../lib_common.txt"
#include "./../../lib_shotdata.txt"
#include "./../../lib_effect.txt"
#include "./../../lib_familiar.txt"
#include "./boss.txt"

@Initialize{
	SetAutoDeleteObject(true);
	startBoss;
	mainTask;
}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){ 
		SetScriptResult(1000); 
	}
	case(EV_REQUEST_TIMER){ 
		SetScriptResult(63);
	}
}

@MainLoop{yield;}

task mainTask{
	SetAreaCommonData("Boss","BossName","Koishi Komeiji");

	let bossObj = createBoss("koishi","green");
	ObjMove_SetPosition(bossObj,GetCommonData("BossPositionX",[0])[0],GetCommonData("BossPositionY",[0])[0]);
	MoveA3(bossObj,45,centerX,centerY-100);

	SetCommonData("BossList",[bossObj,ID_INVALID,ID_INVALID]);
	
	wait(20);
	
	SetAreaCommonData("Boss","Score",100000);
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	cutin_boss("&quot;Rushed Action =Lapsus=&quot;","koishi_00",CR("green"),"Normal");
	wait(8*18);

	let t = 0;
	let ta = 4;

	ObjEnemy_SetDamageRate(bossObj,25,25);

	while(ObjEnemy_GetInfo(boss,INFO_LIFE) > 0 && t < 60*60){

		if(t%2 == 0){
			let xa = 64*cos(-t*ta);
			let ya = 64*sin(-t*ta)*cos(80);
			let cx = xa*cos(-30)-ya*sin(-30);
			let cy = xa*sin(-30)+ya*cos(-30);
			_PlaySE(26);
			S1(ObjMove_GetX(bossObj)+cx,ObjMove_GetY(bossObj)+cy,-t*ta-30,DS_BALL_S_A_GREEN);
		}

		if(t%2 == 0){
			let xa = 64*cos(t*ta+180);
			let ya = 64*sin(t*ta+180)*cos(80);
			let cx = xa*cos(30)-ya*sin(30);
			let cy = xa*sin(30)+ya*cos(30);
			S1(ObjMove_GetX(bossObj)+cx,ObjMove_GetY(bossObj)+cy,t*ta+30+180,DS_BALL_S_A_GREEN);
		}

		if(t%16 == 0){
			let xa = 64*1.5*cos(-t/3*ta);
			let ya = 64*1.5*sin(-t/3*ta)*cos(60);
			let cx = xa*cos(-30+90)-ya*sin(-30+90);
			let cy = xa*sin(-30+90)+ya*cos(-30+90);
			_PlaySE(27);
			S1(ObjMove_GetX(bossObj)+cx,ObjMove_GetY(bossObj)+cy,-t/3*ta-30+90,DS_BALL_L_RED);
		}

		if(t%16 == 0){
			let xa = 64*1.5*cos(t/3*ta);
			let ya = 64*1.5*sin(t/3*ta)*cos(60);
			let cx = xa*cos(30+90)-ya*sin(30+90);
			let cy = xa*sin(30+90)+ya*cos(30+90);
			S1(ObjMove_GetX(bossObj)+cx,ObjMove_GetY(bossObj)+cy,t/3*ta+30+90,DS_BALL_L_RED);
		}

		if(t > 120){
			S1(ObjMove_GetX(bossObj)+rand(-32,32),ObjMove_GetY(bossObj)+rand(-32,32),rand(0,360),DS_BALL_SS_ORANGE);_PlaySE(26);
		}

		if(t%240 == 120){MoveA4(bossObj,50);}

		t++;
		
		yield;
	}

	NotifyEventAll(EV_USER_STAGE + 2, [ObjMove_GetX(bossObj),ObjMove_GetY(bossObj)]);
	wait(200);

	ObjEnemy_SetLife(boss,-1);

	NotifyEventAll(EV_USER_STAGE + 3, [GetStgFrameWidth/2,centerY]);
	
	SetCommonData("BossPositionX",[ObjMove_GetX(bossObj)]);
	SetCommonData("BossPositionY",[ObjMove_GetY(bossObj)]);

	Obj_Delete(boss);
	Obj_Delete(bossObj);

	CloseScript(GetOwnScriptID());
}

task S1(x,y,angle,dan){
	let obj = CreateShotA1(x,y,0,angle,dan,15);
	MoveA2(obj,120,0,NO_CHANGE,1/60,1);
}