#ScriptVersion[3]
#Title["Die"]
#Background["./background.txt"]
#Text["Kasumi Die"]

let frame=0;
let bossObj;
let imgBoss = GetCurrentScriptDirectory ~ "img/boss.png";

#include "./ZUNShot_Const.dnh"
#include"./function.txt"

@Initialize {
   SetAutoDeleteObject(true);
   bossObj=ObjEnemy_Create(OBJ_ENEMY_BOSS);
   ObjEnemy_Regist(bossObj);
   mainTask;
}

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

@MainLoop {
   frame++; 
   let bossx=ObjMove_GetX(bossObj);
   SetCommonData(bossx,ObjMove_GetX(bossObj));
   let bossy=ObjMove_GetY(bossObj);
   SetCommonData(bossy,ObjMove_GetY(bossObj));
   ObjEnemy_SetIntersectionCircleToShot(bossObj,bossx,bossy,24);
   ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossx,bossy,24);
   if(ObjEnemy_GetInfo(bossObj,INFO_LIFE)==0){Obj_Delete(bossObj); DeleteShotAll(TYPE_ALL,TYPE_ITEM); CloseScript(GetOwnScriptID()); DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);}
   yield;   
}

@Finalize {
}

task mainTask {
loop(100){explode;}
PlaySound("se_enep01");
renderBoss;
ObjEnemy_SetDamageRate(bossObj, 0, 0);
loop(160){yield;}
loop(240){explode2;}
PlaySound("se_enep01");
while(!Obj_IsDeleted(bossObj)){
	yield;
}
}

task renderBoss{
let i = 0;
while(!Obj_IsDeleted(bossObj)){
	yield;
}
}


task explode{
	let x = ObjMove_GetX(bossObj);
	let y = ObjMove_GetY(bossObj);
	let angle = rand(0, 360);
	let randRadius = rand(1,3);
	let randSquare = 5+rand(-0.25,1);
	let inc = 0;
	let light = ObjPrim_Create(OBJ_SPRITE_2D);
	let texture = GetCurrentScriptDirectory~"img/chg.png";
	ObjPrim_SetTexture(light, texture);
	ObjSprite2D_SetSourceRect(light,0,0,32,32);
	ObjSprite2D_SetDestCenter(light);
	ObjRender_SetScaleXYZ(light, randSquare-inc/5, randSquare-inc/5, 0);
	while(!Obj_IsDeleted(bossObj)){
		ObjRender_SetScaleXYZ(light, randSquare-inc/50, randSquare-inc/50, 0);
		ObjRender_SetAlpha(light, 255-inc);
		ObjRender_SetBlendType(light, BLEND_ADD_ARGB);
		ObjRender_SetPosition(light, x+inc*cos(angle), y+inc*sin(angle), 0);
		inc+=randRadius;
		if((255-inc)<=0){Obj_Delete(light);}
		yield;
	}
}

task explode2{
	let x = ObjMove_GetX(bossObj);
	let y = ObjMove_GetY(bossObj);
	let angle = rand(0, 360);
	let randRadius = rand(1,3);
	let randSquare = 5+rand(-0.25,1);
	let inc = 0;
	let light = ObjPrim_Create(OBJ_SPRITE_2D);
	let texture = GetCurrentScriptDirectory~"img/chg.png";
	ObjPrim_SetTexture(light, texture);
	ObjSprite2D_SetSourceRect(light,0,0,32,32);
	ObjSprite2D_SetDestCenter(light);
	ObjRender_SetScaleXYZ(light, randSquare-inc/5, randSquare-inc/5, 0);
	while(!Obj_IsDeleted(bossObj)){
		ObjRender_SetScaleXYZ(light, randSquare-inc/50, randSquare-inc/50, 0);
		ObjRender_SetAlpha(light, 255-inc);
		ObjRender_SetBlendType(light, BLEND_ADD_ARGB);
		ObjRender_SetPosition(light, x+2*inc*cos(angle), y+2*inc*sin(angle), 0);
		inc+=randRadius;
		if((255-inc)<=0){Obj_Delete(light);}
		yield;
	}
}