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

#include"./Functions/Index.txt"
#include"script/Medicine/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();
let px;
let py;
@Initialize
{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtWeight(objBoss,stgx,120,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);
	SetPlayerSpeed(4,2);
	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{
	wait(60);
	ObjEnemy_SetDamageRate(objBoss,50,25);
	PlaySound("cardcall",100,0);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin("Medi","\"Harmful Flower\"",15);
	ObjMove_SetDestAtWeight(objBoss,stgx+100,120,10,7);
	wait(60);
	Tree;
	}
//----------------------------------------------------

task Tree{
	let obj;
	loop(3){
	maple(3);
	wait(120);
	obj = CreateShotA1(stgx-80,stgy*2,6,270,G_LARGE_PURPLE,0);
	Manage(obj,1);
	wait(150);
	maple(2);
	wait(60);
	TBullet;
	wait(225);
	ObjMove_SetDestAtWeight(objBoss,stgx-100,100,20,4);
	wait(75);
	maple(3);
	wait(120);
	obj = CreateShotA1(stgx+80,stgy*2,6,270,G_LARGE_PURPLE,0);
	Manage(obj,-1);
	wait(150);
	maple(2);
	wait(60);
	TBullet;
	wait(225);
	ObjMove_SetDestAtWeight(objBoss,stgx+100,120,20,4);
	wait(75);
	}
	}

task TPoison{
	ascent(n in 0..3){
	ascent(i in 0..12){
	//poison(GetEnemyX,GetEnemyY,2,i*30+n*10,0,2,0.5,60);
	}
	wait(90);
	}
	}

task TBullet{
	TPoison;
	let r = 0;
	let dir = 0;
	let angle = 0;
	ascent(n in 0..30){
	dir = rand(0,359);
	r = rand(20,100);
	PlaySound("BulletWave",95,0);
	loop(3){
	CreateShotB2(px+r*cos(dir),py+r*sin(dir),rand(-1.75,1.75),rand(-1,-0.5),0,0.04,0,rand(1,2.5),82,12);
	}
	ascent(i in 0..10){
	angle = (140+i*30)+50*sin(n*12);
	CreateShotA1(GetEnemyX+30*cos(angle),GetEnemyY+30*sin(angle),2,angle,PELLET_RED,12);
	}
	wait(6);
	}
	}

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

task Manage(obj,way){
	ObjRender_SetColor(obj,0,0,0);
	ControlAngular;
	ControlBullet;
		task ControlBullet{
		loop(47){
		PlaySound("BulletWave",100,0);
		let tshot = CreateShotOA1(obj,0,0,BL_SMALL_PURPLE,20);
		ManageDelete(tshot);
		//ObjRender_SetBlendType(tshot,BLEND_ADD_RGB);
		wait(2);
		}
		wait(4);
		ObjMove_SetSpeed(obj,0);
		px = ObjMove_GetX(obj);
		py = ObjMove_GetY(obj);
		ControlAlpha;
		ControlPoison;
		 ascent(i in 0..18){
		 let shot = CreateShotOA1(obj,3,i*20,SMALL_PURPLE,0);
		 Manage2(shot);
		 ascent(a in 0..5){
		 CreateShotOA1(obj,3-a/2,i*20,SMALL_PURPLE,0);
		 }
		 }
		}
		wait(510);
		PlaySound("BulletWave",95,0);
		ascent(a in 0..12){
		ascent(i in 0..18){
		let gshot = CreateShotOA1(obj,0.25+a/4,i*20+10,G_LARGE_PURPLE,0);
		ObjMove_AddPatternA2(gshot,0,0,NO_CHANGE,0.0025+a/500,0,5);
		}
		}
		Obj_Delete(obj);
			task ControlAngular{
			wait(5);
			ObjMove_SetAngularVelocity(obj,-1.5*way);
			wait(30);
			ObjMove_SetAngularVelocity(obj,3.2*way);
			wait(30);
			ObjMove_SetAngularVelocity(obj,5*way);
			}
			task ControlAlpha{
			let alpha = 0 ;
			while(alpha<250){
			alpha+=5;
			ObjRender_SetColor(obj,alpha,alpha,alpha);
			yield;
			}
			}
			task Manage2(obj2){
			wait(10);
			ascent(n in 0..15){
			let lshot = CreateShotOA1(obj2,0,ObjMove_GetAngle(obj2),BL_SMALL_PURPLE,12);
			Manage3(lshot,n);
			}
			}
			 task Manage3(obj,spd){
			 PlaySound("BulletWave",95,330);
			 ObjMove_AddPatternA2(obj,330,0,NO_CHANGE,0.0025+spd/500,0,5);
			 }
			 task ManageDelete(obj){
			 wait(340);
			 let fake = CreateShotOA1(obj,0,0,BL_SMALL_PURPLE,12);
			 ObjShot_SetDeleteFrame(fake,0);
			 Obj_Delete(obj);
			 }
			 task ControlPoison{
			 ascent(n in 0..4){
			 ascent(i in 0..12){
			 poison(px,py,2,i*30+n*10,0,2,0.5,60);
			 }
			 wait(90);
			 }
			}
	}

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

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){
		if(f<8){ObjSprite2D_SetSourceRect(objBoss, 0, 0, 64, 64);}
		if(f>=8 && f<16){ObjSprite2D_SetSourceRect(objBoss, 64, 0, 128, 64);}
		if(f>=16 && f<24){ObjSprite2D_SetSourceRect(objBoss, 128, 0, 192, 64);}
		if(f>=24 && f<32){ObjSprite2D_SetSourceRect(objBoss, 192, 0, 256, 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;
	}
