
#ScriptVersion[3]
#Title["ClownpieceS1"]
#Text["ClownpieceS1"]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#Background["./BG.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/AllStarShot_Const.dnh"
#include"./system/Default_Effect.Txt"


let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let action = false;
let objScene=GetEnemyBossSceneObjectID();
@Initialize
{
SetBoss("Clownpiece",true);
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),18);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(1500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(45);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(35000000);
	}
}

//----------------------------------------------------
task mainTask{
	magiccircle(objBoss,1,-1);
	DamageRateAtFrame(50,20,240);
	ObjMove_SetDestAtWeight(objBoss,stgx,200,10,7);
	Cutin("Lunar Mare\"Meteor Impact\"",15,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	wait(105);
	TShot1;
	wait(1200);
	DamageRateAtFrame(75,20,0);
	}
//----------------------------------------------------

task TShot1
{
	let spiral = [2,4,6][Level];
	let sangle = [2,1.25,1][Level];
	attack(30,1);
	MoonAroundBoss;
	PlaySound("Boon1",100,0);
	wait(90);
	MapleExplode(GetEnemyX,GetEnemyY,1.5,"PINK2");
	PinkWave;
	wait(300);
	BlueWave;
	task BlueWave
	{
		while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
		{
			let result = rand(-15,15);
			PlaySound("BulletWave",95,0);
			ascent(i in 0..12)
			{
				let angle = i*30+result;
				TShotA2(GetEnemyX+30*cos(angle),GetEnemyY+30*sin(angle),2.5,angle,0.03,2.5,270,8);
			}
			wait(25);
		}
	}
	
	task PinkWave
	{
		let result = 0;
		while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0)
		{
			PlaySound("BulletWave",95,0);
		if(Level<3)
		{
			ascent(i in 0..spiral)
			{
				let angle = i*360/(spiral)+result;
				TShotA2(GetEnemyX+30*cos(angle),GetEnemyY+30*sin(angle),2.5,angle,0.03,2.5,269,8);
			}
			result += sangle;
		}
			wait(8);
		}
	}
}

task MoonAroundBoss
{
	let mon = Moon(GetEnemyX,GetEnemyY,1);
	let count = 0;
	let ra = 0;
	let r = 0;
	let angle = 0;
	while(!Obj_IsDeleted(mon))
	{
		if(count<90)
		{
			r = 120*sin(count);
		}
		ra = r+30*sin(count*1.5);
		angle-=1;
		count++;
		ObjMove_SetPosition(mon,GetEnemyX+ra*cos(angle),GetEnemyY+ra*sin(angle));
		CheckBullet;
		yield;
	}
	
	task CheckBullet
	{
		let arr = GetShotIdInCircleA2(ObjMove_GetX(mon),ObjMove_GetY(mon),58,TARGET_ENEMY);
		ascent(i in 0..length(arr))
		{
			if(ObjShot_GetImageID(arr[i])==270 && Obj_GetValueD(arr[i],"MoonHit",0)==0)
			{
				BlueStar(ObjMove_GetX(arr[i]),ObjMove_GetY(arr[i]),ObjMove_GetSpeed(arr[i]),ObjMove_GetAngle(arr[i]));
				DeleteEffect(arr[i]);
			}
			if(ObjShot_GetImageID(arr[i])==269 && Obj_GetValueD(arr[i],"MoonHit",0)==0)
			{
				PinkStar(arr[i],ObjMove_GetX(arr[i]),ObjMove_GetY(arr[i]),ObjMove_GetSpeed(arr[i]),ObjMove_GetAngle(arr[i]));
				DeleteEffect(arr[i]);
			}
		}
	}
	
	task BlueStar(mx,my,speed,dir)
	{
		ascent(i in -1-Level..2+Level)
		{
			TShotA2(mx,my,speed/2,dir+i*3,0.03,10,167,8);
		}
	}

	task PinkStar(co,mx,my,speed,dir)
	{
		TShotA2(mx,my,speed/2,GetAngleToPlayer(co),0.03,10,165,8);
	}
	
	task DeleteEffect(obj)
	{
		PlaySound("Shot6",90,0);
		Obj_SetValue(obj,"MoonHit",1);
		ObjMove_SetSpeed(obj,0);
		ascent(i in 0..10)
		{
			ObjRender_SetScaleXYZ(obj,1+i/10,1+i/10,0);
			ObjRender_SetAlpha(obj,255-i*20);
			yield;
		}
		Obj_Delete(obj);
	}
}



//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(1);
		yield;
		CloseScript(GetOwnScriptID());
	}