#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Last Word? (Eustace)"]
#Text["Fun with Eustace"]
#include"./Index.txt"

let bossObj;
let bossX = 0;
let bossY = 0;
let AnimFrame = 0;
let AnimFrame2 = 0;

let imgBoss = CSD ~ "/Texture/Eustace.png";
let AttackSFX = CSD ~ "/SFX/ATTACK5.wav";
let Attack2SFX = CSD ~ "/SFX/ATTACK4.wav";
let Attack3SFX = CSD ~ "/SFX/TWINKLE3.wav";
let BGM = GetCurrentScriptDirectory ~ "/SFX/Tiamat Magna.mp3";

@Initialize {
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);

	ObjMove_SetPosition(bossObj,190,-100);
	ObjMove_SetDestAtWeight(bossObj,190,120,8,5);
	
	LoadSound(BGM);
	PlayBGM(BGM,1,397);
	Cutin(bossObj,"Dead-End Shot ~ Last Word",8);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	LoadSound(Attack3SFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_IS_LAST_SPELL){
		SetScriptResult(true)
	}
	case(EV_REQUEST_LIFE) {
		SetScriptResult(5500);
	}		
	case(EV_REQUEST_TIMER) {
		SetScriptResult(80);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(800000);
	}
 }

@MainLoop {
	
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,15);
	DeleteShotInCircle(TYPE_ALL,TYPE_FADE,bossX,bossY,30);
		
	yield;
 }

@Finalize {

 }

task mainTask{
	renderBoss;
	renderBG("EUSTACE");
	callbackboss;
	movement;
	TEnd;
	wait(60);
	Fire;
	wait(200);
	Fire2;
 }
 
task movement{
	wait(70);
	loop{
		ObjMove_SetDestAtFrame(bossObj,rand(200,181),rand(90,131),300);
		wait(330);
		ObjMove_SetDestAtFrame(bossObj,rand(200,181),rand(340,381),300);
		wait(330);
	}
}
 
task renderBoss{
	let Z=0;
	let X=0;
	let dir;
	
	ObjPrim_SetTexture(bossObj, imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,250,250);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,0.5,0.5,0);
	
	while(!Obj_IsDeleted(bossObj)) {
		dir = ObjMove_GetAngle(bossObj);
		
		if(cos(dir) < 0) {
			loop(90){
				ObjRender_SetScaleXYZ(bossObj,0.4+0.02*sin(Z),0.4-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X>0){X-=2;}
				wait(1);
			}
		}
		else if(cos(dir) > 0) {
			loop(90){
				ObjRender_SetScaleXYZ(bossObj,0.4+0.02*sin(Z),0.4-0.02*sin(Z),0);
				Z+=3;
				ObjRender_SetAngleXYZ(bossObj,0,X,0);
				if(X<180){X+=2;}
		wait(1);
			}}
		yield;
	}
 }

task Fire{
	let B = rand(360,0);
	let angle= 0;
	loop{
	PlaySound(AttackSFX);
		loop(10){
			while (angle<360){
				let obj = CreateShotA2(bossX+40*sin(angle+B),bossY-40*cos(angle+B),0,angle+B-90,0.1,2.5,136,6);
				ReflectSide(obj);
				angle+=360/2;
			}
			angle=0;
			B-=1.618*3;
			wait(12);
		}
		yield;
	}
}

task Fire2{
	let B = rand(360,0);
	let angle= 0;
	loop{
		loop(30){
			while (angle<360){
				CreateShotA2(bossX+40*sin(angle+B),bossY-40*cos(angle+B),0,angle+B-90,0.1,2,145,6);
				angle+=360/4;
			}
			angle=0;
			B+=1.618*2;
			wait(6);
		}
		loop(1){
			while (angle<360){
				CreateShotA2(bossX+40*sin(angle+B),bossY-40*cos(angle+B),0,angle+B-90,0.1,2,145,6);
				angle+=360/4;
			}
			angle=0;
			while (angle<360){
				CreateShotA2(bossX+45*sin(angle+B),bossY-45*cos(angle+B),0,angle+B-90,0.1,2.5,143,12);
				angle+=360/36;
			}
			angle=0;
			B+=1.618*2;
			wait(6);
		}
		yield;
	}
}

 task ReflectSide(obj){
	while(ObjMove_GetX(obj)>0&&ObjMove_GetX(obj)<GetStgFrameWidth&&ObjMove_GetY(obj)>-1&&ObjMove_GetY(obj)<GetStgFrameHeight){yield;}
	PlaySound(Attack3SFX);
		if(ObjMove_GetX(obj)<=0){
			if(!Obj_IsDeleted(obj)){CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,GetAngleToPlayer(obj),0.1,2,134,12);}			
		}else if(ObjMove_GetX(obj)>=GetStgFrameWidth){
			if(!Obj_IsDeleted(obj)){CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,GetAngleToPlayer(obj),0.1,2,134,12);}
		} else if(ObjMove_GetY(obj)<=0){
			if(!Obj_IsDeleted(obj)){CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,GetAngleToPlayer(obj),0.1,2,134,12);}
		} else if(ObjMove_GetY(obj)>=GetStgFrameHeight){
			if(!Obj_IsDeleted(obj)){CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,GetAngleToPlayer(obj),0.1,2,134,12);}
		}
 }

 task TEnd {
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0) {
		yield;
	}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(1){
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.3,255,255,10);
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.2,255,10,255);
		TExplosionB(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),5,0.1,255,100,0);
		wait(1);
	}
	Obj_Delete(bossObj);
	ResetShaderI(0, 100);
	wait(60);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	CloseScript(GetOwnScriptID);
}