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

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

let imgBoss = CSD~ "/Texture/Ange.png";
let AttackSFX = CSD ~ "/SFX/TWINKLE3.wav";
let Attack2SFX = CSD ~ "/SFX/ATTACK3.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,"Fleur Tourbillon ~ Last Word",1);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_IS_LAST_SPELL){
		SetScriptResult(true)
	}
	case(EV_REQUEST_LIFE) {
		SetScriptResult(2500);
	}	
	case(EV_REQUEST_TIMER) {
		SetScriptResult(60);
	}	
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(100000);
	}
 }

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

@Finalize {

 }

task mainTask{
	renderBoss;
	renderBG("ANGE");
	Fire;
	//Fire2;
	movement;
	TEnd;
	callbackboss;
 }
 
task movement{
	wait(70);
	loop{
		ObjMove_SetDestAtFrame(bossObj,rand(240,141),rand(90,131),180);
		wait(60);
	}
}
 
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 B = rand(360,0);
	let X = bossX;
	let Y = bossY;
	let angle= 0;
	loop{
		X = bossX;
		Y = bossY;
		loop(16){
		while (angle<360){
			let obj = CreateShotA2(X+30*sin(angle+B),Y-30*cos(angle+B),3,angle+B-90,-0.1,0,35,12);
			angle+=360/9;
			ObjMove_AddPatternA2(obj,30,0,NO_CHANGE,0.1,-0.3,2.5);
			ObjMove_AddPatternA2(obj,120,NO_CHANGE,NO_CHANGE,0.1,0,5);
		}
		angle=0;
		B+=35;
		wait(1);}
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		wait(6);
		B = rand(360,0);
		X = bossX;
		Y = bossY;
		loop(16){
		while (angle<360){
			let obj = CreateShotA2(X+30*sin(angle+B),Y-30*cos(angle+B),3,angle+B-90,-0.1,0,35,12);
			angle+=360/9;
			ObjMove_AddPatternA2(obj,30,0,NO_CHANGE,0.1,0.3,2.5);
			ObjMove_AddPatternA2(obj,120,NO_CHANGE,NO_CHANGE,0.1,-0,5);
		}
		angle=0;
		B-=35;
		wait(1);}
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}		
		B = rand(360,0);
		wait(6);
		yield;
	}
}

task Fire2{
	wait(240);
	let B = rand(360,0);
	let angle= 0;
	loop{
		PlaySound(Attack2SFX);
		while (angle<360){
			CreateShotA2(bossX+20*sin(angle+B),bossY-20*cos(angle+B),8,angle+B+90,-1,2,260,12);
			angle+=360/36;
		}
		B = rand(360,0);
		angle= 0;
		wait(60);
			if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		yield;
	}
	
}

 task TEnd {
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0) {
		yield;
	}
	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);
	}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	Obj_Delete(bossObj);
	ResetShaderI(0, 100);
	wait(60);
	DeleteShotAll(TYPE_ALL,TYPE_FADE);
	CloseScript(GetOwnScriptID);
}