#ScriptVersion[3]
#Title["NS1"]
#Background["./background.txt"]
#Text["Nonspell 1"]

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

let frame = 0;
let angle = 0;
let variable = 0;
let i = 0;
let state = 0;
let objBoss;
let imgBoss = GetCurrentScriptDirectory ~ "img/boss.png";

@Initialize {
   SetAutoDeleteObject(true);
   objBoss=ObjEnemy_Create(OBJ_ENEMY_BOSS);
   ObjEnemy_Regist(objBoss);
   ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-120, 10, 5);
   mainTask;
}

@Event {
   alternative(GetEventType())
   case(EV_REQUEST_LIFE){
   	SetScriptResult(2000);
   }
   case(EV_REQUEST_TIMER){
	SetScriptResult(45);
   }
}

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

@Finalize {
}

task mainTask {
renderBoss;
Circle(objBoss);
ObjEnemy_SetDamageRate(objBoss, 0, 0);
loop(120){yield;}
ObjEnemy_SetDamageRate(objBoss, 60, 60);
StateSwitchTask;
while(!Obj_IsDeleted(objBoss)){
		loop(70){yield;}
		angle=rand(0,360);
		loop(10){
		loop(3){
			loop(4){
			DelayCloudBullet(ObjMove_GetX(objBoss)+150,ObjMove_GetY(objBoss),2,angle+variable,35+i*4,i);
			DelayCloudBullet(ObjMove_GetX(objBoss)-150,ObjMove_GetY(objBoss),2,angle+variable,35+i*4,i);
			variable+=360/120;
			}
			i+=1;
		}
		i=0;
		}
	}
}

task StateSwitchTask{
	while(!Obj_IsDeleted(objBoss)){
	loop(100){yield;}
	PlaySound("click");
	if(state==2){state=0;}else{state+=1;}
	}
}

task DelayCloudBullet(x,y,speed,angle,graphic,a){
	if(ObjEnemy_GetInfo(objBoss,INFO_LIFE)==0){return;}
	let bullet = CreateShotA1(x,y,speed,angle,graphic,0);
	let saveSpeed = speed;
	let i = 0;
	ObjMove_SetSpeed(bullet, 0);
	while(i<=10){
		i+=1;
		ObjShot_SetIntersectionEnable(bullet, false);
		ObjRender_SetScaleXYZ(bullet, 2 - 1/10*i, 2-1/10*i,0);
		ObjRender_SetAlpha(bullet, 255/10*i);
		yield;
	}
	ObjShot_SetIntersectionEnable(bullet, true);
	ObjMove_SetSpeed(bullet, saveSpeed);
	while(!Obj_IsDeleted(bullet)){
		if(state==a){
		ObjShot_SetIntersectionEnable(bullet, true);
		ObjRender_SetAlpha(bullet, 255);
		}
		else{
		ObjShot_SetIntersectionEnable(bullet, false);
		ObjRender_SetAlpha(bullet, 128);
		}
		yield;
	}
}

task renderBoss{
	let i = 0;
	ObjPrim_SetTexture(objBoss,imgBoss);
	ObjSprite2D_SetSourceRect(objBoss,6,7,76,102);
	ObjSprite2D_SetDestRect(objBoss,6,7,76,102);
	ObjSprite2D_SetDestCenter(objBoss);
	ObjRender_SetScaleXYZ(objBoss,1,1,0);
	while(!Obj_IsDeleted(objBoss)){
		i++;
		if(i%3==0){glowBoss;}
		yield;
	}
}

task glowBoss{
	let objx = ObjMove_GetX(objBoss);
	let objy = ObjMove_GetY(objBoss);
	let texture = GetCurrentScriptDirectory~"/img/boss.png";
	let objCircle = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objCircle, 0.225);
	ObjPrim_SetTexture(objCircle, texture);
	ObjSprite2D_SetSourceRect(objCircle,6,7,76,102);
	ObjSprite2D_SetDestCenter(objCircle);
	let ct = 0;
	while(!Obj_IsDeleted(objBoss)){
		ObjRender_SetColorHSV(objCircle, frame, 128, 255);
		ObjRender_SetPosition(objCircle, objx, objy, 0);
		ObjRender_SetAlpha(objCircle,255-(ct*255/50));
		ObjRender_SetBlendType(objCircle, BLEND_ADD_ARGB);
		ObjRender_SetScaleXYZ(objCircle, 1+ct/100, 1+ct/100, 0);
		ct++;
		yield;
		if(ct==50){Obj_Delete(objCircle);}
	}
}