#東方弾幕風[Single]
#ScriptVersion[3]
#Title["LastSpell"]
#Text["LastSpell"]
#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(2000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(30000);
	}
	case(EV_REQUEST_IS_LAST_SPELL)
	{
		SetScriptResult(true);
	}
}

//----------------------------------------------------
task mainTask{
	SetShotAutoDeleteClip(24,24,24,24);
	wait(90);
	TExplosionA(GetEnemyX,GetEnemyY,5,0.6);
	ObjEnemy_SetDamageRate(objBoss,30,0);
	PlaySound("cardcall",100,0);
	Cutin("Medi","\"Crysis Snow\"",15);
	ObjEnemyBossScene_StartSpell(objScene);
	wait(60);
	TSnow;
	}
//----------------------------------------------------

task TSnow{
	let px;
	let py;
	let r;
	let obj;
	let angle;
	let shot;
	let color = [PELLET_BLUE,DOT_BLUE];
	while(!Obj_IsDeleted(objBoss)){
	color = [PELLET_BLUE,DOT_BLUE];
	r = 60;
	maple(3);
	wait(120);
	attack(60);
	ascent(n in 0..30){
	ascent(m in 0..5){
	PlaySound("BulletWave",95,0);
	px = GetEnemyX+r*cos(270-n*6);
	py = GetEnemyY+r*sin(270-n*6);
	obj = snow(px,py,rand(0.05,0.1),rand(0,359),2,30,10);
	Manage(obj,n);
	px = GetEnemyX+r*cos(270+n*6);
	py = GetEnemyY+r*sin(270+n*6);
	obj = snow(px,py,rand(0.05,0.1),rand(0,359),2,30,10);
	Manage(obj,n);
	r  = 60+m*70;
	}
	wait(2);
	}
	wait(60);
	maple(2);
	TMovement(15,5,5);
	wait(60);
	ascent(n in 0..20){
	PlaySound("Shot5",98,0);
	PlaySound("BulletWave",98,60);
	shot = ShotA2(GetEnemyX+60*cos(180),GetEnemyY+60*sin(180),5,rand(150,210),rand(-0.15,-0.05),0,OVAL_BLUE,15);
	ObjMove_AddPatternA1(shot,60,2,GetAngleToPlayer(shot));
	shot = ShotA2(GetEnemyX+60*cos(0),GetEnemyY+60*sin(0),5,rand(-30,30),rand(-0.15,-0.05),0,OVAL_BLUE,15);
	ObjMove_AddPatternA1(shot,60,2,GetAngleToPlayer(shot));
	//shot = ShotA2(GetEnemyX+60*cos(270),GetEnemyY+60*sin(270),5,rand(210,330),rand(-0.15,-0.05),0,OVAL_BLUE,15);
	//ObjMove_AddPatternA1(shot,60,2,GetAngleToPlayer(shot));
	wait(2);
	}
	wait(60);
	ascent(n in 0..40){
	PlaySound("Shot1",95,0);
	r = rand(30,90);
	angle = rand(0,359);
	ascent(i in 0..6){
	ShotA1(GetEnemyX+r*cos(angle),GetEnemyY+r*sin(angle)-60,1,i*60+r,color[n%2],20);
	}
	wait(3);
	}
	TMovement(15,5,5);
	maple(2);
	wait(60);
	ascent(n in 0..24){
	PlaySound("BulletWave",100,0);
	ascent(i in 0..9){
	ShotOA1(objBoss,3-n/12,i*40+n*11,OVAL_AQUA,12);
	}
	wait(7);
	}
	maple(2);
	wait(60);
	PlaySound("BulletWave",100,0);
	PlaySound("BulletWave",100,60);
	ascent(i in 0..36){
	ascent(a in 0..4){
	shot = CreateShotA2(GetEnemyX,GetEnemyY,3,90-i*5,-3/60,0,OVAL_BLUE,20);
	ObjMove_AddPatternA2(shot,60,2,NO_CHANGE,0,-i/4-0.25,0);
	ObjMove_AddPatternA2(shot,90,1.25+a/4,NO_CHANGE,0,0,0);
	shot = CreateShotA2(GetEnemyX,GetEnemyY,3,90+i*5,-3/60,0,OVAL_BLUE,20);
	ObjMove_AddPatternA2(shot,60,2,NO_CHANGE,0,i/4+0.25,0);
	ObjMove_AddPatternA2(shot,90,1.25+a/4,NO_CHANGE,0,0,0);
	}
	}
	wait(90);
	color = [PELLET_AQUA,195];
	ascent(n in 0..20){
	PlaySound("Shot1",95,0);
	r = rand(30,90);
	angle = rand(0,359);
	ascent(i in 0..6){
	ShotA1(GetEnemyX+r*cos(angle),GetEnemyY+r*sin(angle)-60,1.2,i*60+r,color[n%2],20);
	}
	wait(3);
	}
	TMovement(20,5,4);
	ascent(n in 0..20){
	PlaySound("Shot1",95,0);
	r = rand(30,90);
	angle = rand(0,359);
	ascent(i in 0..6){
	ShotA1(GetEnemyX+r*cos(angle),GetEnemyY+r*sin(angle)-60,1.2,i*60+r,color[n%2],20);
	}
	wait(3);
	}
	color = [SMALL_AQUA,G_LARGE_BLUE];
	PlaySound("Shot1",95,0);
	ascent(i in 0..24){
	ascent(a in 0..8){
	ShotA1(GetEnemyX+60*cos(i*15),GetEnemyY+60*sin(i*15),0.5+a/3,i*15+180,color[a%2],30);
	}
	}
	ObjEnemy_SetDamageRate(objBoss,50,0);
	}
	}

task Manage(obj,num){
	wait(60);
	PlaySound("piss",95,0);
	ascent(i in 0..3){
	let angle = getAtanAngle(obj,objBoss)+i*120+num*0;
	let shot =  ShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,angle,0.002,1,ICE_BLUE,15);
	ObjMove_AddPatternA2(shot,30-num,0,NO_CHANGE,0.0075,0,0.75);
	}
	Obj_Delete(obj);
	}

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

task TRender
{
	magiccircle(objBoss,1.75,true);
	let imgMiko = GetCurrentScriptDirectory~"img/boss.png";
	let f = 0;
	let t = 0;
	let c = 0;
	ObjPrim_SetTexture(objBoss, imgMiko);
	ObjRender_SetScaleXYZ(objBoss,1.05,1.05,1);
	while(!Obj_IsDeleted(objBoss))
	{
		c++;
		if(c<120){ObjRender_SetAlpha(objBoss,0);}
		else{ObjRender_SetAlpha(objBoss,255);}
		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;}
		StartSlow(TARGET_ALL,30);
		SetAutoDeleteObject(true);
		loop(10){
		TExplosionA(GetEnemyX+rand(-50,50),GetEnemyY+rand(-50,50),10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		wait(10);
		}
		TExplosionA(GetEnemyX,GetEnemyY,2,0.6);
		Obj_Delete(objBoss);
		SetAutoDeleteObject(true);
		PlaySound("enemoff",100,0);
		StopSlow(TARGET_ALL);
		loop(180){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
		}
		CloseScript(GetOwnScriptID());
		return;
	}
