#include "./BossInclude.dnh"

@Event{
	let LIFE_VALUE = 750;
	let TIME_VALUE = 37;
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(LIFE_VALUE);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(TIME_VALUE);
	}
}

@Initialize{
	Boundary = [64,128,64,128];
	MAIN_Initialize;
	BOSS_Initialize("CHAR_SEIJA");
	SPELL_Finalize;
	FLIP_Routine;
	//----------//
	ObjEnemy_SetCollision(objBoss,32,16);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtBrake(objBoss,CenX,CenY,90);
	ObjAnim_SetActionA1(objBoss,ACT_MOVE,90);
	//----------//
	ObjScene_SetSpellCard(2,0);
	MainThread_Standard;
}

task MainThread_Standard{
	wait(90);
	PlaySoundA1(SE_CHARGE_WARP);
	ObjAnim_SetActionA1(objBoss,ACT_SHOT_A,150);
	ObjBoss_SetChargeA1(PURPLE,6,10,30);
	ObjBoss_SetChargeA2(GREEN,6,10,30);
	wait(60);
	PlaySoundA1(SE_HUM_LOUD);
	FlipScreenV1(60);
	ObjEnemy_WaitDamageRate(objBoss,25,20,60);
	let rValue = GetInverser;
	local{
		let rMod = rValue;
		loop(2){
			let setAngle = -90+(90+25)*rMod;
			loop(2){
				PlaySoundA1(SE_BURST_MAIN);
				SetLoopStream(GetBossX,GetBossY,10,setAngle,3);
				setAngle += 180;
				wait(30);
			}
			rMod = -rMod;
		}
	}
	ObjAnim_SetActionA1(objBoss,ACT_MOVE,60);
	ObjMove_SetDestAtBrake(objBoss,CenX,CenY-64,60);
	wait(120);
	while(ObjEnemy_IsAlive(objBoss)){
		let LTRB = [MinX+64,CenY-80,MaxX-64,CenY-48];
		ObjAnim_SetActionA1(objBoss,ACT_MOVE,60);
		ObjMove_SetDestAtPlayerX(objBoss,48,rand(0,24),LTRB,60);
		wait(120);
	}
	
	task SetLoopStream(let sX, let sY, let extSpeed, let setAngle, let expRate){
		let rLimit = 8;
		let lpCount = 0;
		loop{
			while(Pos_IsInBoundaries(sX,sY,rLimit)){
				PlaySoundA1(SE_WAVE_NEW);
				sX += extSpeed*cos(setAngle);
				sY += extSpeed*sin(setAngle);
				if(modc(lpCount,25) != 0){
					let objLoc = CreateShotS1(sX,sY,0.25,rand_deg,FIRE_PURPLE,-10,PURPLE);
					PlaySoundO1(objLoc,SE_SHOT_BREAK,45);
					Obj_WaitDelete(objLoc,45);
				}else{
					Explosion_SetDestAtPos(sX,sY,20,30,SHOT_ARGB,RED);
					let toPlayer = GetAngleToPlayer2(sX,sY)+rand(-10,10);
					CreateShotS2(sX,sY,0,toPlayer,1.25/90,1.25,FIRE_RED,-10,RED);
				}
				lpCount++;
				wait(expRate);
				if(!ObjEnemy_IsAlive(objBoss)){ return; }
			}
			if(sX < MinX-rLimit){
				sX = 2*(MinX-rLimit)-sX;
				setAngle = 180-setAngle;
			}else if(sX > MaxX+rLimit){
				sX = 2*(MaxX+rLimit)-sX;
				setAngle = 180-setAngle;
			}else if(sY < MinY-rLimit){
				sY = 2*(MinY-rLimit)-sY;
				setAngle = 360-setAngle;
			}else if(sY > MaxY+rLimit){
				sY = 2*(MaxY+rLimit)-sY;
				setAngle = 360-setAngle;
			}
			/*
			if(sX <= MinX-rLimit){
				sX += (MaxX-MinX)+(2*rLimit);
			}else if(sX >= MaxX+rLimit){
				sX -= (MaxX-MinX)+(2*rLimit);
			}else if(sY <= MinY-rLimit){
				sY += (MaxY-MinY)+(2*rLimit);
			}else if(sY >= MaxY+rLimit){
				sY -= (MaxY-MinY)+(2*rLimit);
			}
			*/
			Explosion_SetDestAtPos(sX,sY,20,30,SHOT_ARGB,PURPLE);
			yield;
		}
	}
}

task FlipScreenV1(let frameTime){
	let iniRatio = Get2DCameraRatioY;
	let finRatio = 1.00*-sgn(iniRatio);
	if(!finRatio){ finRatio = 1.00; }
	ascent(let a in 1..frameTime+1){
		let flipRatio = iniRatio+a*(finRatio-iniRatio)/frameTime;
		SetFlipCameraRatioY(flipRatio);
		yield;
		if(!ObjEnemy_IsAlive(objBoss)){ break; }
	}
}