#ScriptVersion[3]
#Title["DEATH"]
#System["./system/system.txt"]
#Background["./background.txt"]
#Text[""]

#include"./system/function.txt"

let frame = 0;
let health = 1;
let objBoss;
let imgBoss = GetCurrentScriptDirectory ~ "img/boss.png";

@Initialize {
   DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
   SetAutoDeleteObject(true);
   objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
   ObjEnemy_Regist(objBoss);
   mainTask;
}

@Event {
   alternative(GetEventType())
   case(EV_REQUEST_LIFE){
   	SetScriptResult(1);
   }
   case(EV_REQUEST_TIMER){
	SetScriptResult(100);
   }
}

@MainLoop {
	frame++; 
	let bossx = ObjMove_GetX(objBoss);
	let bossy = ObjMove_GetY(objBoss);
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){
		Obj_Delete(objBoss); 
		DeleteShotAll(TYPE_ALL,TYPE_ITEM); 
		SetAutoDeleteObject(true);
		CloseScript(GetOwnScriptID()); 
	}
	yield;   
}

@Finalize {
}

task mainTask{
	renderBoss;
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	loop(60){charge(objBoss, 0, 255, 255); charge(objBoss, 120, 255, 255); charge(objBoss, 240, 255, 255); yield;}
	loop(60){yield;}
	PlaySound("se_enep01");
	ExplodeBoss(objBoss);
	loop(60){yield;}
	health = 0;
	PlaySound("se_enep01");
	ExplodeBoss2(objBoss);
	loop(180){yield;}
	ObjEnemy_SetLife(objBoss, 0);
}

task renderBoss{
	let i = 0;
	ObjPrim_SetTexture(objBoss,imgBoss);
	ObjSprite2D_SetSourceRect(objBoss,0,0,512,512);
	ObjSprite2D_SetDestRect(objBoss,0,0,512,512);
	ObjSprite2D_SetDestCenter(objBoss);
	ObjRender_SetScaleXYZ(objBoss,0.35,0.35,0);
	while(!Obj_IsDeleted(objBoss) && health == 1){
		yield;
	}
	ObjPrim_SetTexture(objBoss, "");
	ObjSprite2D_SetSourceRect(objBoss,0,0,0,0);
	ObjSprite2D_SetDestRect(objBoss,0,0,0,0);
}