#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 8 (Eustace)"]
#Text["8th SpellCard"]
#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";

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

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

	ObjMove_SetDestAtWeight(bossObj,190,120,8,5);
	
	Cutin(bossObj,"Dead-End Shot",8);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	LoadSound(Attack3SFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(7500);
	}		
	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;
	Fire;
	movement;
	TEnd;
	wait(200);
	Fire2;
 }
 
task movement{
	wait(70);
	loop{
		ObjMove_SetDestAtFrame(bossObj,rand(270,111),rand(90,131),240);
		wait(300);
	}
}
 
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{
	wait(120);
	let X = 2;
	let angle= 0;
	loop{
		PlaySound(AttackSFX);
		loop(43){
			let obj = CreateShotA2(X,0,5,90,-0.1,2,136,18);
			ReflectSide(obj);
			X+=9;
		}
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		X = 1;
		wait(120);
		yield;
	}
}

task Fire2{
	let B = rand(360,0);
	let angle= 0;
	loop{
		loop(5){TChargeEffect(bossX,bossY);}
		wait(20);
		loop(8){
			PlaySound(Attack2SFX);
			while (angle<360){
				let obj = CreateShotA2(bossX+40*sin(angle+B),bossY-40*cos(angle+B),-1,angle+B-90,-0.1,2,145,18);
				ReflectSide(obj);
				angle+=360/5;
			}
			angle=0;
			B-=1.6*3;
			wait(3);
		}
		wait(180);
		
		loop(5){TChargeEffect(bossX,bossY);}
		wait(20);
		PlaySound(Attack2SFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+40*sin(angle+B),bossY-40*cos(angle+B),-1,angle+B-90,-0.1,2,145,18);
			ReflectSide(obj);
			angle+=360/27;
			wait(1);
		}
		wait(180);
		
		loop(5){TChargeEffect(bossX,bossY);}
		wait(20);		
		loop(8){
			PlaySound(Attack2SFX);
			while (angle<360){
				let obj = CreateShotA2(bossX+40*sin(angle+B),bossY-40*cos(angle+B),-1,angle+B-90,-0.1,2,145,18);
				ReflectSide(obj);
				angle+=360/5;
			}
		angle=0;
		B+=1.6*3;
		wait(3);
		}
		wait(180);
		
		loop(5){TChargeEffect(bossX,bossY);}
		wait(20);
		PlaySound(Attack2SFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+40*sin(angle+B),bossY-40*cos(angle+B),-1,angle+B-90,-0.1,2,145,18);
			ReflectSide(obj);
			angle+=360/27;
			wait(1);
		}
		wait(180);
		yield;
	}
}

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

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