#include "./BossInclude.dnh"

@Event{
	let LIFE_VALUE = 850;
	let TIME_VALUE = 50;
	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;
	//----------//
	ObjEnemy_SetCollision(objBoss,32,16);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtBrake(objBoss,CenX,CenY-64,90);
	ObjAnim_SetActionA1(objBoss,ACT_MOVE,90);
	//----------//
	ObjScene_SetSpellCard(3,0);
	MainThread_Standard;
}

task ObjBoss_SetChargeB1(let wtTime, let maxCount){
	let rColor = [RED,ORANGE,YELLOW,GREEN,AQUA,BLUE,PURPLE];
	let iColor = [CYAN,AQUA,BLUE,PURPLE,RED,YELLOW,GREEN];
	ascent(let a in 0..length(rColor)){
		ObjBoss_SetChargeA1(rColor[a],1,0,maxCount);
		ObjBoss_SetChargeA2(iColor[a],1,0,maxCount);
		wait(wtTime);
	}
}

task MainThread_Standard{
	wait(90);
	PlaySoundA1(SE_CHARGE_WARP);
	ObjAnim_SetActionA2(objBoss,30,ACT_SHOT_B,120);
	ObjBoss_SetChargeB1(10,30);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss,25,25);
	let rValue = GetInverser;
	let wtTime = 60*7;
	while(ObjEnemy_IsAlive(objBoss)){
		local{
			let setAngle = -90+(360/32)/2*rValue;
			ascent(let iWay in 0..32){
				PlaySoundA1(SE_SHOT_MAIN);
				let wayAngle = setAngle+iWay*(360/32)*rValue;
				let sX = GetGapX(GetBossX,36,wayAngle);
				let sY = GetGapY(GetBossY,36,wayAngle);
				local{
					let objLoc = CreateShotS1(sX,sY,1,wayAngle,ARROW_GRAY,-10,GRAY);
					ObjMove_AddPatternA2(objLoc,0,NO_CHANGE,NO_CHANGE,0.01,0,3.75);
					ObjMove_SetLooping(objLoc,8);
				}
				wait(2);
				if(!ObjEnemy_IsAlive(objBoss)){ return; }
			}
		}
		wait(180);
		if(!ObjEnemy_IsAlive(objBoss)){ return; }
		ObjMove_SetDestAtBrake(objBoss,CenX-96*rValue,CenY-64,90);
		ObjAnim_SetActionA1(objBoss,ACT_MOVE,90);
		wait(180);
		if(!ObjEnemy_IsAlive(objBoss)){ return; }
		ObjMove_SetDestAtBrake(objBoss,CenX,CenY-64,90);
		ObjAnim_SetActionA1(objBoss,ACT_MOVE,90);
		local{
			let setAngle = rand_deg;
			ascent(let i in 0..15){
				PlaySoundA1(SE_BURST_SIDE);
				let addSpeed = 2+i*4/8;
				ascent(let iWay in 0..16){
					let wayAngle = setAngle+iWay*(360/16);
					let sX = GetGapX(GetBossX,24,wayAngle);
					let sY = GetGapY(GetBossY,24,wayAngle);
					CreateShotS1(sX,sY,addSpeed,wayAngle,REV_ELLIPSE_PURPLE,10,PURPLE);
				}
				setAngle += 2.25*rValue;
				wait(6);
				if(!ObjEnemy_IsAlive(objBoss)){ return; }
			}
		}
		wait(30);
		rValue = -rValue;
		ObjAnim_SetActionA1(objBoss,ACT_SHOT_B,120);
	}
	
	task ObjMove_SetLooping(let objTarget, let limBound){
		ObjShot_SetSpellResist(objTarget,true);
		ObjShot_SetAfterImage(objTarget);
		Obj_SetValueAtFrame(objTarget,60*8.5,"WARP_FLAG",false);
		loop{
			while(Obj_IsInBoundaries(objTarget,limBound-1)){
				if(!Obj_GetValueD(objTarget,"WARP_FLAG",true)){ return; }
				if(Obj_IsDeleted(objTarget)){ return; }
				yield;
			}
			if(Obj_IsDeleted(objTarget)){ return; }
			PlaySoundA1(SE_WAVE_NEW);
			let oX = ObjMove_GetX(objTarget);
			let oY = ObjMove_GetY(objTarget);
			let getAngle = ObjMove_GetAngle(objTarget);
			Explosion_SetDestAtPos(oX,oY,16,30,SHOT_ARGB,Obj_GetValueD(objTarget,"RGB",GRAY));
			if(oX <= MinX-limBound){
				oX += (MaxX-MinX)+(2*limBound);
			}else if(oX >= MaxX+limBound){
				oX -= (MaxX-MinX)+(2*limBound);
			}else if(oY <= MinY-limBound){
				oY += (MaxY-MinY)+(2*limBound);
			}else if(oY >= MaxY+limBound){
				oY -= (MaxY-MinY)+(2*limBound);
			}
			Explosion_SetDestAtPos(oX,oY,16,30,SHOT_ARGB,Obj_GetValueD(objTarget,"RGB",GRAY));
			ObjMove_SetPosition(objTarget,oX,oY);
			yield;
		}
		task ObjShot_SetAfterImage(let objTarget){
			let graphID = ObjShot_GetImageID(objTarget);
			let lpCount = 120;
			while(!Obj_IsDeleted(objTarget)){
				let RGB = GetRainbowRGB(lpCount);
				let oX = ObjMove_GetX(objTarget);
				let oY = ObjMove_GetY(objTarget);
				let getAngle = ObjMove_GetAngle(objTarget);
				SubTask_SetAfterImage(oX,oY,getAngle,graphID,RGB);
				Obj_SetValue(objTarget,"RGB",RGB);
				lpCount -= 2.5;
				wait(2);
			}
			task SubTask_SetAfterImage(let x, let y, let angle, let graph, let RGB){
				let objFade = CreateShotA1(x,y,0,angle,graph,0);
				ObjShot_SetSpellResist(objFade,true);
				ObjShot_SetIntersectionEnable(objFade,false);
				ObjRender_SetBlendType(objFade,BLEND_ADD_ARGB);
				ObjRender_SetColorArray(objFade,RGB);
				ObjRender_SetScaleXYZ(objFade,0.50,0.50,0.00);
				
				let frameTime = 30;
				descent(let d in 0..frameTime){
					let decAlpha = d*255/(frameTime-1);
					ObjRender_SetAlpha(objFade,decAlpha);
					yield;
				}
				Obj_Delete(objFade);
			}
		}
	}
}