#ScriptVersion[3]
#Title["Spell5"]
#Text["Spell5"]
#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 s = 1;
@Initialize
{
	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);
	SetPlayerSpeed(4,2);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(2000);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(50);
	}
	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","Poison Sign\"Poison Doll\"",15);
	wait(60);
	TPoison;
	TShot;
	}
//----------------------------------------------------

task TShot{
	let color = [82,88,91,89];
	let dir = 0;
	let obj = 0;
	while(!Obj_IsDeleted(objBoss)){
	ascent(i in 0..4){
	obj = CreateShotOA1(objBoss,1,i*90+dir,color[i],12);
	ObjMove_AddPatternA2(obj,90,1,NO_CHANGE,0.02,0,2);
	obj = CreateShotOA1(objBoss,1,i*90-dir*1.5,color[i],12);
	ObjMove_AddPatternA2(obj,90,1,NO_CHANGE,0.02,0,2);
	}
	dir += 7;
	wait(5);
	}
	}

task TPoison{
	ascent(n in 0..10){
	ascent(i in 0..12){
	spoison(GetEnemyX,GetEnemyY,1.5,i*30+n*15,0,0,0.5,60);
	}
	wait(120);
	}
	}

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


function spoison(x,y,spd,dir,acc,maxspd,maxscale,frame){
	let shot = CreateShotA2(x,y,spd,dir,acc,maxspd,SMALL_RED,0); // Invisible Bullet
	let dis = 0;
	ObjShot_SetIntersectionEnable(shot,false);
	ObjRender_SetAlpha(shot,0);
	poisonshot(x,y,1);
	poisonshot(x,y,-1);
	return shot;
	task poisonshot(x,y,way){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/poison2.png";
	let c = 0;
	let scale = 0;
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjRender_SetColor(obj,128,256,128);
		while(!Obj_IsDeleted(shot)){
		if(scale<maxscale){scale += maxscale/frame;}
		ObjRender_SetScaleXYZ(obj,scale,scale,0);
		dis = GetDistance(ObjMove_GetX(shot),ObjMove_GetY(shot),GetPlayerX,GetPlayerY);
		if(dis<scale*95){
		SetPlayerSpeed(8,4);
		}
		ObjRender_SetPosition(obj,ObjMove_GetX(shot),ObjMove_GetY(shot),0); // stick poison to the bullet
		ObjRender_SetAngleXYZ(obj,0,0,c*1*way);
		c++;
		yield;
		}
		Obj_Delete(obj);
	}
}

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

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