#東方弾幕風[Single]
#ScriptVersion[3]
#Title["Cold Sign\"Fucking Glacial Missile\""]
#Text["Spell3I"]
#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(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);
	}
}

//----------------------------------------------------
task mainTask{
	SetShotAutoDeleteClip(64,64,64,64);
	wait(60);
	PlaySound("cardcall",100,0);
	Cutin("Medi","Cold Sign\"Fucking Glacial Missile\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss,50,25);
	TShot;
	wait(300);
	TWall;
	}
//----------------------------------------------------

task TShot{
	let s = 1;
	let obj;
	maple(1);
	attack(150);	
	wait(120);
	PlaySound("Weird2",100,0);
	obj = CreateShotOA1(objBoss,0,120,LARGE_BLUE,12);
	Manage(obj);
	obj = CreateShotOA1(objBoss,0,240,LARGE_BLUE,12);
	Manage(obj);
	obj = CreateShotOA1(objBoss,0,0,LARGE_BLUE,12);
	Manage(obj);
	}

task TWall{
	let n = 0;
	let s = 1;
	maple(2);
	wait(60);
	while(!Obj_IsDeleted(objBoss)){
	PlaySound("Shot1",100,0);
	ascent(i in 0..10){
	ascent(a in 0..11){
	ShotOA1(objBoss,0.5+a/6,i*36+a*1.8*s,PELLET_AQUA,12);
	ShotOA1(objBoss,0.5+a/6,i*36-a*1.8*s,PELLET_AQUA,12);
	}
	}
	s = -s;
	wait(60);
	TMovement(20,5,5);
	wait(60);
	}
	}

task Manage(obj){
	let c = 0;
	ObjShot_SetAutoDelete(obj,false);
	ObjShot_SetIntersectionEnable(obj,false);
	ObjRender_SetAlpha(obj,0);
	ControlAim;
	while(!Obj_IsDeleted(obj)){
	ObjMove_SetSpeed(obj,2+1*sin(c));
	c++;
	if(c%12==0){
	let s = snow(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,rand(0,359),3,30,10);
	Manage2(s,c);
	}
	yield;
	}
		 task ControlAim{
		 let maxdir = 1.5;
		 let angle = ObjMove_GetAngle(obj);
		 let dir = 0;
		 let diff = 0;
		 let diffAbs = 0;
		 while(!Obj_IsDeleted(obj)){
		 ObjMove_SetAngle(obj,angle);
		 dir = getAtanAngle(obj,objBoss);
		 diff = dir-angle;
		 while(diff >= 180){diff-=360;}
		 while(diff<-180){diff+=360;}
		 diffAbs = (|diff|);
		 if(diffAbs<maxdir){
		 angle = dir;
		 }
		 else{
		 angle += maxdir*diff/diffAbs;
		 }
		 yield;
		 }
		 Obj_Delete(obj);
		 }
	task Manage2(s,c){
	wait(120);
	let x = ObjMove_GetX(s);
	let y = ObjMove_GetY(s);
	Obj_Delete(s);
	ascent(i in 0..4){
	PlaySound("Shot1",95,0);
	ShotA2(x+rand(-10,10),y+rand(-10,10),3.5,getDotAngle(GetEnemyX,GetEnemyY,x,y)+i*90,-0.1,rand(0.8,1),DOT_BLUE,15);
	wait(6);
	ShotA2(x+rand(-10,10),y+rand(-10,10),3.5,getDotAngle(GetEnemyX,GetEnemyY,x,y)+i*90,-0.1,rand(0.8,1),DOT_BLUE,15);
	}
	if(c%120==0){
	PlaySound("BulletWave",100,0);
	ascent(i in 0..12){
	CreateShotA2(x,y,1,i*360/12+c*1.7,0,0,G_LARGE_BLUE,20);
	}
	}
	}

}

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

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;
	}