//---------------------------------------------------------------------------------
// Graphical Variables
//---------------------------------------------------------------------------------

let EFFECT_HEX			= CSD ~ "./SystemData/ImageData/HexCircle.png";

let BULLETBREAK_MAIN	= CSD ~ "./SystemData/ImageData/BulletBreak_Main.png";
let BULLETBREAK_ALT		= CSD ~ "./SystemData/ImageData/BulletBreak_Alt.png";
let BULLETBREAK_SUB		= CSD ~ "./SystemData/ImageData/BulletBreak_Sub.png";

let NUCLEAR_MAIN		= CSD ~ "./SystemData/ImageData/Nuclear_Main.png";
let NUCLEAR_HALO 		= CSD ~ "./SystemData/ImageData/Nuclear_Halo.png";
let NUCLEAR_HALO_2		= CSD ~ "./SystemData/ImageData/Nuclear_Halo_Grayscale.png";

let EFFECT_DELAY_MAIN	= CSD ~ "./SystemData/ImageData/DelayCloud_Main.png";
let EFFECT_DELAY_SOFT 	= CSD ~ "./SystemData/ImageData/DelayCloud_Soft.png";
let EFFECT_EXPLODE 		= CSD ~ "./SystemData/ImageData/Explode.png";
let EFFECT_CONCENTRATE	= CSD ~ "./SystemData/ImageData/Concentrate.png";
let EFFECT_INVERSE 		= CSD ~ "./SystemData/ImageData/Inverse.png";
let EFFECT_ENERGY		= CSD ~ "./SystemData/ImageData/EnergyWave.png";
let EFFECT_RING 		= CSD ~ "./SystemData/ImageData/Ring.png";

//---------------------------------------------------------------------------------
// Initialization Tasks
//---------------------------------------------------------------------------------

task MAIN_Initialize{
	SETTING_Initialize;
	TEXTURE_Initialize;
	SOUND_Initialize;
}

task TEXTURE_Initialize{
	LoadTexture(BULLETBREAK_MAIN);
	LoadTexture(BULLETBREAK_ALT);
	LoadTexture(BULLETBREAK_SUB);
	LoadTexture(EFFECT_DELAY_MAIN);
	LoadTexture(EFFECT_DELAY_SOFT);
	
	LoadTexture(EFFECT_EXPLODE);
	LoadTexture(EFFECT_INVERSE);
	LoadTexture(EFFECT_ENERGY);
	LoadTexture(EFFECT_RING);
	
	LoadTexture(NUCLEAR_MAIN);
	LoadTexture(NUCLEAR_HALO);
	LoadTexture(NUCLEAR_HALO_2);
}

task SETTING_Initialize{
	SetPlayerClip(MinX,MinY+16,MaxX,MaxY-8);
	SetShotAutoDeleteClip(Boundary[0],Boundary[1],Boundary[2],Boundary[3]);
}

//---------------------------------------------------------------------------------
// Spell Card Declaration
//---------------------------------------------------------------------------------

task ObjScene_SetSpellCard(SpellCardID,wtTime){
	wait(wtTime);
	let objScene = GetEnemyBossSceneObjectID;
	PlaySoundA1(SE_SPELLCARD);
	SetCommonData("Spell_Number",SpellCardID);
	ObjEnemyBossScene_StartSpell(objScene);
}

//---------------------------------------------------------------------------------
// Finalization Tasks
//---------------------------------------------------------------------------------

task BULLET_Finalize(let pX, let pY, let expCount){
	ascent(let a in 1..expCount+1){
		let expRadius = a*(AbsMaxX)/expCount;
		let objArray = GetShotIdInCircleA2(pX,pY,expRadius,TARGET_ENEMY);
		ascent(let i in 0..length(objArray)){
			if(!Obj_GetValueD(objArray[i],"IS_STRAIGHT_LASER",false)){
				Obj_Delete(objArray[i]);
			}
		}
		yield;
	}
	DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
}

task NORMAL_Finalize{
	while(ObjEnemy_IsAlive(objBoss)){ yield; }
//	SetAutoDeleteObject(true);
	BULLET_Finalize(GetBossX,GetBossY,60);
	Obj_Delete(objBoss);
	wait(120);
	CloseScript(GetOwnScriptID);
	return;
}

task SPELL_Finalize{
	while(ObjEnemy_IsAlive(objBoss)){ yield; }
//	SetAutoDeleteObject(true);
	let oX = GetBossX;
	let oY = GetBossY;
	Obj_Delete(objBoss);
	loop(30){
		let objArray = GetShotIdInCircleA2(oX,oY,AbsMaxX,TARGET_ENEMY);
		ascent(let i in 0..length(objArray)){
			if(!Obj_GetValueD(objArray[i],"IS_STRAIGHT_LASER",false)){
				Obj_Delete(objArray[i]);
			}
		}
		yield;
	}
	wait(60);
	CloseScript(GetOwnScriptID);
	return;
}

task BOSS_Finalize{
	while(ObjEnemy_IsAlive(objBoss)){ yield; }
	let objScene = GetEnemyBossSceneObjectID;
	/*
	let REM_TIME = ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER);
	let IS_DURABLE = ObjEnemyBossScene_GetInfo(objScene,INFO_IS_DURABLE_SPELL);
	if(REM_TIME == 0 && !IS_DURABLE){
		ObjMove_SetDestAtBrake(objBoss,-64,-64,60);
		SetAction(objBoss,ACT_MOVE,60);
		loop(90){
			let objArray = GetShotIdInCircleA2(GetBossX,GetBossY,AbsMaxX,TARGET_ENEMY);
			ascent(let i in 0..length(objArray)){
				if(!Obj_GetValueD(objArray[i],"IS_STRAIGHT_LASER",false)){
					Obj_Delete(objArray[i]);
				}
			}
			yield;
		}
		Obj_Delete(objBoss);
		wait(60);
		CloseScript(GetOwnScriptID);
		return;
	}
	*/
//	ObjBoss_SetChargeA1(WHITE,6,10,30);
	local{
		let obj = ObjSound_Create;
		ObjSound_Load(obj,SE_BOSS_VANISH);
		ObjSound_SetVolumeRate(obj,90);
		ObjSound_SetSoundDivision(obj,SOUND_SE);
		ObjSound_Play(obj);
	}
	BULLET_Finalize(GetBossX,GetBossY,60);
	wait(60);
	local{
		let obj = ObjSound_Create;
		ObjSound_Load(obj,SE_BOSS_VANISH);
		ObjSound_SetVolumeRate(obj,90);
		ObjSound_SetSoundDivision(obj,SOUND_SE);
		ObjSound_Play(obj);
	}
	SetScreenShake(40,90);
	Finalize_BossVanish(GetBossX,GetBossY);
	Obj_Delete(objBoss);
	wait(120);
	CloseScript(GetOwnScriptID);
	return;
	
	task Finalize_BossVanish(let sX, let sY){
		ascent(let agCount in 0..4){
			let pX = GetGapX(sX,48,agCount*90);
			let pY = GetGapY(sY,48,agCount*90);
			SetInverseWave(pX,pY,32,90);
		}
		ascent(let ccCount in 0..2){
			SetInverseWave(sX,sY,(32+16)-ccCount*16,90-ccCount*45);
			wait(45);
		}
		
		task SetInverseWave(let pX, let pY, let maxSpeed, let maxCount){
			let objMain = ObjInverse_SetProperties;
			ObjRender_SetPosition(objMain,pX,pY,0);
			
			let expValue = 0;
			let waveSpeed = 0;
			loop(maxCount){
				ObjRender_SetScaleXYZ(objMain,expValue/512,expValue/512,0);
				expValue += waveSpeed;
				if(waveSpeed < maxSpeed){
					waveSpeed += maxSpeed/(45-1);
				}
				yield;
			}
			Obj_Delete(objMain);
		}
	}
}

task FLIP_Routine{
	while(ObjEnemy_IsAlive(objBoss)){
		Set2DCameraRatioX(GetFlipCameraRatioX);
		Set2DCameraRatioY(GetFlipCameraRatioY);
		Set2DCameraAngleZ(GetFlipCameraAngleZ);
		yield; 
	}
	SetFlipCameraRatioX(1.0);
	SetFlipCameraRatioY(1.0);
	SetFlipCameraAngleZ(0);
	let INVERSE_SCREEN = CSD ~ "./SystemData/ImageData/BackGround/Dummy_Fog.png";
	let objInv = ObjPrim_Create(OBJ_SPRITE_2D);
	local{
		ObjPrim_SetTexture(objInv,INVERSE_SCREEN);
		Obj_SetRenderPriority(objInv,0.70);
		ObjRender_SetPosition(objInv,CenX,CenY,0);
		ObjRender_SetBlendType(objInv,BLEND_INV_DESTRGB);
		
		ObjSprite2D_SetSourceRect(objInv,0,0,AbsMaxX,AbsMaxY);
		ObjSprite2D_SetDestCenter(objInv);
	}
	descent(let d in 1..21){
		let decAlpha = d*255/20;
		ObjRender_SetAlphaFromRGB(objInv,WHITE,decAlpha);
		if(d == 20/2){
			Reset2DCamera;
		}
		yield;
	}
	Obj_Delete(objInv);
}