#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,45,centerX,centerY-100);

	SetCommonData("BossList",[bossObj,ID_INVALID,ID_INVALID]);
	
	wait(60);

	ObjEnemy_SetDamageRate(bossObj,100,100);

	let t = 0;

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

		if(t%6 == 0){
			ascent(a in 0..11){
				let angle = floor(t/30)*360/(10*6)+(t%30)/30*15+a*360/11;
				CreateShotA1(ObjMove_GetX(bossObj)+30*cos(angle),ObjMove_GetY(bossObj)+30*sin(angle),2+(t%30)/30*1,angle,604,10);
			}
			_PlaySE(26);
		}

		if(t%180 == 0){S2([-1,1][(t/180)%2]);}

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

		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 S2(dir){
	let x = ObjMove_GetX(bossObj);
	let y = ObjMove_GetY(bossObj);
	let z = CreateCorrectedLaserA1(x,y,-90,600,20,500,DS_BALL_S_A_RED,0);
	let t = 0;
	ObjRender_SetBlendType(z,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(z,120);
	ObjShot_SetSpellResist(z,true);
	ObjShot_SetIntersectionEnable(z,false);

	descent(a in 0..160){
		ObjMove_SetPosition(z,ObjMove_GetX(bossObj),ObjMove_GetY(bossObj));
		ObjStLaser_SetAngle(z,GetAngA2(bossObj)+a*270/160*dir);
		t++;

		yield;
	}

	let andz = GetAngA2(bossObj);
	x = GetPlayerX;
	y = GetPlayerY;

	descent(a in 0..40){
		ObjMove_SetPosition(z,ObjMove_GetX(bossObj),ObjMove_GetY(bossObj));
		ObjStLaser_SetAngle(z,andz-10*cos(a/40*90)*dir);
		ObjRender_SetAlpha(z,120/40*a);

		if(a%5 == 4){_PlaySE(12);TExplosionA2(x,y, CR("red"),196);}

		yield;
	}
	Obj_Delete(z);

	_PlaySE(28);

	ascent(a in 0..50){
		let z = CreateShotA2(x,y,8,a*360/50,-2/20*4,0,DS_BALL_S_A_RED,5);
		ObjShot_SetDeleteFrame(z,40);
	}

	loop(3){
		loop(25){
			let z = CreateShotA2(x,y,rand(1,4)*2,rand(0,360),-2/20*4,0,DS_BALL_S_A_RED,5);
			ObjShot_SetDeleteFrame(z,40);
		}
		wait(1);
	}

}
}