#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(4000); 
	}
	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,60,centerX,centerY-100);

	SetCommonData("BossList",[bossObj,ID_INVALID,ID_INVALID]);
	
	descent(a in 0..30){
		Set2DCameraRatio(1+a/30*0.3);
		Set2DCameraFocusY(240-a/30*100);
		yield;
	}

	wait(60);

	ObjEnemy_SetDamageRate(bossObj,100,100);

	let t = 0;

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


		if(t%180 == 40){ascent(a in 0..1){S1(GetAngA2(bossObj)+a*360/6);}}
		if(t%60 == 0){S2([-1,1][(t/60)%2]);}

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

		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(angl){
	let x = ObjMove_GetX(bossObj);
	let y = ObjMove_GetY(bossObj);
	let t = 0;
	let r = 30;

	_PlaySE(27);

	while(true){

		let x2 = r*cos(angl);
		let y2 = r*sin(angl);

		r = 30+t*3;

		if(t%2 == 0){
			let ax = CreateShotA1(x+x2+16*cos(angl+90)*cos(t*15+90),y+y2+16*sin(angl+90)*cos(t*15+90),0,angl-180,602,0);
			let ay = CreateShotA1(x+x2+16*cos(angl-90)*cos(t*15+90),y+y2+16*sin(angl-90)*cos(t*15+90),0,angl-180,603,0);

			Deletquicklol(40,ax);
			Deletquicklol(40,ay);
		 }

		t++;

		yield;
	}

	task Deletquicklol(a,b){
		descent(c in 0..a){ObjRender_SetScaleXYZ(b,c/a,c/a,0);yield;}Obj_Delete(b);
	}
}
task S2(dir){
	let x = ObjMove_GetX(bossObj);
	let y = ObjMove_GetY(bossObj);
	let as = rand(0,360);
	ascent(a in 0..3){
		ascent(b in 0..9){
			let z = CreateShotA1(x,y,1.5+a/8,a*360/(9*18)*dir+b*360/9+as,601,2);
			MoveOA1(z,3);
			let z2 = CreateShotA1(x,y,1.5+a/8,a*360/(9*18)*dir+b*360/9+as,602,2);
			MoveOA1(z2,-3);
		}
		_PlaySE(26);
		wait(3);
	}
}
}