#include "./BossInclude.dnh"

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

@Initialize{
	Boundary = [64,64,64,64];
	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-80,90);
	ObjAnim_SetActionA1(objBoss,ACT_MOVE,90);
	//----------//
	ObjScene_SetSpellCard(1,0);
	MainThread_Standard;
}

task MainThread_Standard{
	wait(90);
	PlaySoundA1(SE_CHARGE_WARP);
	ObjAnim_SetActionA1(objBoss,ACT_SHOT_A,120);
	ObjBoss_SetChargeA1(PURPLE,6,10,30);
	ObjBoss_SetChargeA2(GREEN,6,10,30);
	wait(60);
	PlaySoundA1(SE_HUM_LOUD);
	FlipScreenH1(60);
	ObjEnemy_WaitDamageRate(objBoss,25,15,60);
// 	Pattern Start --------------------------------
	SubTask_SetPatternA1(GetInverser);
	SubTask_SetPatternA2;
	
	task SubTask_SetPatternA1(let rMod){
		wait(60);
		let iMax = (NowLengthY)/48;
		let jMax = (NowLengthX)/48;
		let rArray = [rMod,-rMod];
		while(ObjEnemy_IsAlive(objBoss)){
			ascent(let i in 0..iMax){
				PlaySoundA1(SE_SHOT_MAIN);
				ascent(let a in 0..length(rArray)){
					let pX = (MinX)+a*(NowLengthX);
					let pY = CenY-((NowLengthY)/2)*rArray[a]+(i*48)*rArray[a];
					let randAngle = rand(-10,10);
					Explosion_SetDestAtPos(pX,pY,24,20,SHOT_ARGB,PURPLE);
					ascent(let iWay in 0..4){
						let wayAngle = randAngle+iWay*(360/4);
						CreateShotS2(pX,pY,3.50,wayAngle,-2.25/30,1.25,FIRE_PURPLE,-10,PURPLE);
					}
				}
				wait(9);
				if(!ObjEnemy_IsAlive(objBoss)){ return; }
			}
			ascent(let j in 0..jMax){
				PlaySoundA1(SE_SHOT_MAIN);
				ascent(let a in 0..length(rArray)){
					let pX = CenX+((NowLengthX)/2)*rArray[a]-(j*48)*rArray[a];
					let pY = (MinY)+a*(NowLengthY);
					let randAngle = rand(-10,10);
					Explosion_SetDestAtPos(pX,pY,24,20,SHOT_ARGB,PURPLE);
					ascent(let iWay in 0..4){
						let wayAngle = randAngle+iWay*(360/4);
						CreateShotS2(pX,pY,3.50,wayAngle,-2.25/30,1.25,FIRE_PURPLE,-10,PURPLE);
					}
				}
				wait(9);
				if(!ObjEnemy_IsAlive(objBoss)){ return; }
			}
		}
	}
	
	task SubTask_SetPatternA2{
		while(ObjEnemy_IsAlive(objBoss)){
			let toPlayer = GetAngleToPlayer(objBoss);
			let mainWide = 180;
			ascent(let wd1 in 0..5){
				PlaySoundA1(SE_WAVE_NEW);
				let wdAngle1 = (toPlayer-(mainWide/2))+wd1*(mainWide/4);
				let subWide = 20;
				ascent(let wd2 in 0..7){
					let wdAngle2 = (wdAngle1-(subWide/2))+wd2*(subWide/6);
					let sX = GetBossX;
					let sY = GetBossY;
					let objLoc = CreateShotS2(sX,sY,2.75,wdAngle2,-1.75/60,1,REV_ELLIPSE_RED,12,RED);
				}
			}
			wait(60);
			if(!ObjEnemy_IsAlive(objBoss)){ return; }
			let LTRB = [MinX+96,CenY-96,MaxX-96,CenY-64];
			ObjAnim_SetActionA1(objBoss,ACT_MOVE,60);
			ObjMove_SetDestAtPlayerX(objBoss,40,rand(0,24),LTRB,60);
			wait(60);
		}
	}
}

task FlipScreenH1(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;
		SetFlipCameraRatioX(flipRatio);
		yield;
		if(!ObjEnemy_IsAlive(objBoss)){ break; }
	}
}

task FlipScreenH2(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;
		SetFlipCameraRatioX(flipRatio);
		yield;
		if(!ObjEnemy_IsAlive(objBoss)){ break; }
	}
}