
#ScriptVersion[3]
#Title["Spell2"]
#Text["Spell2"]
#Background["./Background.txt"]
#BGM[DEFAULT]
#Player[DEFAULT]

#include"./Functions/Index.txt"
#include"script/Letty/shot/ZUNShot_Const.txt"
#include"script/Default_system/Default_Effect.Txt"

let objBoss;
let enemy;
let bSpell=false;
let GCSD=GetCurrentScriptDirectory~"";
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let objScene=GetEnemyBossSceneObjectID();
@Initialize
{LoadTexture(GCSD~"shot/ZUNShot.png");
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtWeight(objBoss,stgx,150,10,7);
	mainTask;
	TRender;
	TEnd;
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);//出現と同時に敵弾を全て削除	
}

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

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(2250);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);
	}
}

//----------------------------------------------------
task mainTask{
	SetShotAutoDeleteClip(32,32,32,32);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss,50,25);
	PlaySound("cardcall",100,0);
	Cutin("Medi","Cold Sign\"Cold Cyclone\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(60);
	TShot;
	}
//----------------------------------------------------

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

task TShot{
	let obj;
	let s = 1;
	let angle = 0;
	let n = 0;
	while(!Obj_IsDeleted(objBoss)){
	if(n>0){
	 maple(2);
	 ascent(i in 0..8){
	 ascent(a in 0..5){
	 ShotOA1(objBoss,0.5+a/2,i*45,OVAL_AQUA,20);
	 }
	 }
	 ascent(i in 0..24){
	 ShotOA1(objBoss,1.5,i*15,OVAL_AQUA,20);
	 }
	}
	attack(150);
	ascent(i in 0..4){
	angle = i*90+n*17;
	obj = ShotA1(GetEnemyX,GetEnemyY,3,angle,SMALL_BLUE,0);
	Manage(obj,1);
	ObjMove_SetAngularVelocity(obj,2*s);
	}
	wait(180);
	s = -s;
	if(n%2==0){TMovement(20,5,4);}
	wait(180);
	n++;
	}
	}

task Manage(obj,s){
	let c=0;
	let d=0;
	Angular;
	ObjRender_SetAlpha(obj,0);
	ObjShot_SetIntersectionEnable(obj,false);
		task Angular{
		ObjMove_SetAngularVelocity(obj,0.75);
		wait(150);
		ObjMove_SetAngularVelocity(obj,0);
		}
	wait(30);
	while(!Obj_IsDeleted(obj)){
	if(c%20==0){
	PlaySound("BulletWave",100,0);
	d++;
	let sn = snow(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(0.1,0.2),rand(0,359),3,60,0);
	ManageSnow(sn,d);
	ascent(i in 0..5){
	ShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,i*72+d*11*s,0.01,1,PELLET_BLUE,30);
	}
	}
	c++;
	yield;
	}
	 task ManageSnow(sn,num){
	 wait(90);
	 let x = ObjMove_GetX(sn)+rand(-15,15);
	 let y = ObjMove_GetY(sn)+rand(-15,15);
	 Obj_Delete(sn);
	 ascent(i in 0..2){
	 PlaySound("Shot1",95,0);
	 let shot = ShotA2(x+rand(-10,10),y+rand(-10,10),1,NULL,-0.1,rand(0.5,1),SMALL_BLUE,20);
	 let angle = getAtanAngle(shot,objBoss);
	 ObjMove_SetAngle(shot,i*180+angle+num*3.7);
	 wait(10);
	 }
	 }
	}

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

task TRender
{
	magiccircle(objBoss,1.75,true);
	let imgMiko = GetCurrentScriptDirectory~"img/boss.png";
	let f = 0;
	let t = 0;
	ObjPrim_SetTexture(objBoss, imgMiko);
	ObjRender_SetScaleXYZ(objBoss,1.05,1.05,1);
	while(!Obj_IsDeleted(objBoss))
	{
		let angle = ObjMove_GetAngle(objBoss);
		let speed = ObjMove_GetSpeed(objBoss);
		if(speed == 0 && idle == true){
		ObjRender_SetAngleY(objBoss,180);
		if(f<8){ObjSprite2D_SetSourceRect(objBoss, 0, 0, 48, 64);}
		if(f>=8 && f<16){ObjSprite2D_SetSourceRect(objBoss, 48, 0, 96, 64);}
		if(f>=16 && f<24){ObjSprite2D_SetSourceRect(objBoss, 96, 0, 144, 64);}
		if(f>=24 && f<32){ObjSprite2D_SetSourceRect(objBoss, 144, 0, 192, 64);}
		if(f>=32){f=0;}
		f++;
		t = 0;
		}
		else if(cos(angle) > 0 || cos(angle) < 0 && t == 0){
		if(cos(angle)<0){ObjRender_SetAngleY(objBoss,0);}
		else{ObjRender_SetAngleY(objBoss,180);}
		manageSprite;
		t = 1;
		}
		ObjSprite2D_SetDestCenter(objBoss);
		yield;
	}
}

//----------------------------------------------------
task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		//TExplosionA(GetEnemyX,GetEnemyY,10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		SetAutoDeleteObject(true);
		loop(10){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}
