#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 6 (Ferry)"]
#Text["6th SpellCard"]
#include"./Index.txt"

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

let imgBoss = CSD ~ "/Texture/Ferry.png";
let AttackSFX = CSD ~ "/SFX/TWINKLE3.wav";
let Attack2SFX = CSD ~ "/SFX/TWINKLE.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,"Aetheryte Procession",6);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(5000);
	}		
	case(EV_REQUEST_TIMER) {
		SetScriptResult(80);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(600000);
	}
 }

@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("FERRY");
	callbackboss;
	Fire;
	Fire2;
	movement;
	TEnd;
 }
 
task movement{
	wait(70);
	loop{
		ObjMove_SetDestAtFrame(bossObj,rand(270,111),rand(90,131),50);
		wait(100);
	}
}
 
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.03*sin(Z),0.4-0.03*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.03*sin(Z),0.4-0.03*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 angle= 0;
	loop{
		loop(15){
		PlaySound(AttackSFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),0,angle+B+45,0.1,1,105,12);
			angle+=360/7;
			ObjMove_AddPatternA2(obj,30,1,NO_CHANGE,0.1,4,2);
			ObjMove_AddPatternA2(obj,120,NO_CHANGE,NO_CHANGE,0.05,-2,3);
			ObjMove_AddPatternA2(obj,200,NO_CHANGE,NO_CHANGE,-0.05,0.1,1.5);
		}
		angle=0;
		B+=2;
		wait(2);}
		
		wait(24);
		B = rand(360,0);
		loop(15){
		PlaySound(AttackSFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),0,angle+B+45,0.1,1,105,12);
			angle+=360/7;
			ObjMove_AddPatternA2(obj,30,1,NO_CHANGE,0.1,-4,2);
			ObjMove_AddPatternA2(obj,120,NO_CHANGE,NO_CHANGE,0.05,2,3);
			ObjMove_AddPatternA2(obj,200,NO_CHANGE,NO_CHANGE,-0.05,-0.1,1.5);
		}
		angle=0;
		B-=2;
		wait(2);}
		
		B = rand(360,0);
		wait(24*2);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		yield;
	}
}

task Fire2{
	wait(220);
	let B = rand(360,0);
	let angle= 0;
	loop{
		loop(15){TChargeEffect(bossX,bossY);}
		wait(20);
		PlaySound(Attack2SFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),5,angle+B-45,-0.1,2,321,12);
			ObjMove_AddPatternA2(obj,0,5,NO_CHANGE,-0.5,0.5,3);
			
			let obj2 = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),5,angle+B+130,-0.1,2,321,12);
			ObjMove_AddPatternA2(obj2,0,5,NO_CHANGE,-0.5,0.5,3);
			angle+=360/15;
		}
		angle=0;
		B = rand(360,0);
		wait(100);
		loop(15){TChargeEffect(bossX,bossY);}
		wait(20);
		
		PlaySound(Attack2SFX);
		while (angle<360){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),5,angle+B+45,-0.1,2,316,12);
			ObjMove_AddPatternA2(obj,0,5,NO_CHANGE,-0.5,-0.5,3);
			
			let obj2 = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),5,angle+B-130,-0.1,2,316,12);
			ObjMove_AddPatternA2(obj2,0,5,NO_CHANGE,-0.5,-0.5,3);
			angle+=360/15;
		}
		angle=0;
		B = rand(360,0);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		wait(100);
		yield;
	}	
}

task Fire3{
	wait(120);
	let B = rand(360,0);
	let angle= 0;
	loop{
		PlaySound(AttackSFX);
		while (angle<90){
			let obj = CreateShotA2(bossX+30*sin(angle+B),bossY-30*cos(angle+B),-1,angle+B-45,0.1,2,302,18);
			ObjMove_AddPatternA2(obj,0,0,NO_CHANGE,0.1,0.5,1);
			ObjMove_AddPatternA2(obj,120,NO_CHANGE,NO_CHANGE,-0.1,0,2);
			
			let obj2 = CreateShotA2(bossX+30*sin(angle-B),bossY-30*cos(angle-B),-1,angle-B+45,0.1,2,302,18);
			ObjMove_AddPatternA2(obj2,0,0,NO_CHANGE,0.1,-0.5,1);
			ObjMove_AddPatternA2(obj2,120,NO_CHANGE,NO_CHANGE,-0.1,0,2);
			angle+=90/4;
		}
		angle=0;
		B+=11;
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		wait(6);
		yield;
	}	
}

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