#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("Provocation &quot;Fixation on the Rock&quot;","koishi_00",CR("green"),"Normal");
	wait(8*18);

	ObjEnemy_SetDamageRate(bossObj,25,25);

	let t = 0;

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

		if(t%20 == 0){
			ascent(a in 0..9){S1(a*360/9-t/20*12);}
			_PlaySE(26);
		}

		if(t%45 < 15 && t%3 == 0){
			ascent(a in 0..5){
				S2(a*360/5+90-floor(t/45)*13,1,DS_ICE_BLUE);
				S2(a*360/5+90+floor(t/45)*13,-1,DS_ICE_RED);
			}
			if(t%45 == 0){_PlaySE(27);}
		}

		let lai = min(1000-ObjEnemy_GetInfo(boss,INFO_LIFE),500)/500;

		Set2DCameraRatio(1+lai*0.3);
		Set2DCameraFocusY(240-lai*100);

		t++;
		
		yield;
	}

	ObjEnemy_SetLife(boss,-1);

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

	Obj_Delete(boss);
	Obj_Delete(bossObj);

	CloseScript(GetOwnScriptID());

	task S1(as){
		let obj = CreateShotA1(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),0,as,601,5);
		let t2 = 0;
		while(!Obj_IsDeleted(obj)){
			ObjMove_SetSpeed(obj,absolute(2*cos(t2)));
			t2+=1.5;
			yield;
		}
	}

	task S2(as,dir,dan){
		let obj = CreateShotA1(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),0,as,dan,5);
		wait(5);
		MoveOA1(obj,dir);
		let t2 = 0;
		while(!Obj_IsDeleted(obj)){
			ObjMove_SetSpeed(obj,absolute(1*cos(t2))+1);
			t2++;
			yield;
		}
	}
}