#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 3 (Cagliostro)"]
#Text["3rd SpellCard"]
#include"./Index.txt"

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

let imgBoss = GetCurrentScriptDirectory ~ "/Texture/Cagliostro.png";
let AttackSFX = GetCurrentScriptDirectory ~ "/SFX/TWINKLE3.wav";
let Attack2SFX = GetCurrentScriptDirectory ~ "/SFX/ATTACK.wav";

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

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

	ObjMove_SetDestAtWeight(bossObj,190,100,8,5);
	
	Cutin(bossObj,"Ars Magna",3);
	LoadSound(AttackSFX);
	LoadSound(Attack2SFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(7500);
	}		
	case(EV_REQUEST_TIMER) {
		SetScriptResult(90);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(300000);
	}
 }

@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("CAGLI");
	callbackboss;
	Fire;
	Fire2;
	TEnd;
	wait(200);
	Fire3;
 }
 
task renderBoss{
	let X = 0;
	let Z = 0;
		
	ObjPrim_SetTexture(bossObj, imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,0,0,125,125);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,0.6,0.6,0);
	
	loop{
		loop(255*2){
		ObjRender_SetAngleZ(bossObj,50*sin(X*2));
			X+=0.5;
			wait(1);
		}
		loop(255*2){
		ObjRender_SetAngleZ(bossObj,50*sin(X*2));
			X+=0.5;
			wait(1);
		}
	yield;
	}
}
 
task Fire{
	wait(120);
	let A = 0;
	let B = rand(360,0);
	let C = 50;
	let angle= 0;
	loop{
		PlaySound(AttackSFX);
		while (angle<360){
			CreateShotA2(bossX+C*sin(angle+B),bossY-C*cos(angle+B),-1,angle+B-90,0.05,2,187,30);
			angle+=360/20;
		}
		angle=0;
		A+=1.6*8;
		B+=(10*sin(A/3))*2;
		C+=(2*sin(A))*2;
		wait(8);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		yield;
	}
}

task Fire2{
	wait(120);
	let A = 0;
	let B = rand(360,0);
	let C = 50;
	let angle= 0;
	loop{
		while (angle<360){
			CreateShotA2(bossX+C*sin(angle-B),bossY-C*cos(angle-B),4,angle-B+90,-0.1,1.5,226,30);
			angle+=360/16;
		}
		angle=0;
		A+=1.6*4;
		B+=10*cos(A);
		C+=5*sin(A);
		wait(8);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		yield;
	}
}

task Fire3{
	let A;
	loop{
		loop(10){TChargeEffect(bossX,bossY);}
		wait(20);
		PlaySound(Attack2SFX);
		A=rand(0,-45);
		let obj= CreateCurveLaserA1(bossX,bossY,0,A,100,40,39,0);
			ObjMove_AddPatternA2(obj,0,0,NO_CHANGE,0.05,-0.5,3);
			ObjMove_AddPatternA2(obj,60,3,NO_CHANGE,-0.05,5,2);
			ObjMove_AddPatternA2(obj,90,2,NO_CHANGE,0.05,0.5,5);
		obj= CreateCurveLaserA1(bossX,bossY,0,A,100,40,39,0);
			ObjMove_AddPatternA2(obj,0,0,NO_CHANGE,0.05,-0.5,3);
			ObjMove_AddPatternA2(obj,60,3,NO_CHANGE,-0.05,5,2);
			ObjMove_AddPatternA2(obj,90,2,NO_CHANGE,0.05,0.5,5);
			wait(100);
			
		loop(10){TChargeEffect(bossX,bossY);}
		wait(20);
		PlaySound(Attack2SFX);
		A=rand(180,225);
		let obj2= CreateCurveLaserA1(bossX,bossY,0,A,100,40,35,0);
			ObjMove_AddPatternA2(obj2,0,0,NO_CHANGE,0.05,0.5,3);
			ObjMove_AddPatternA2(obj2,60,3,NO_CHANGE,-0.05,-5,2);
			ObjMove_AddPatternA2(obj2,90,2,NO_CHANGE,0.05,-0.5,5);
		obj2= CreateCurveLaserA1(bossX,bossY,0,A,100,40,35,0);
			ObjMove_AddPatternA2(obj2,0,0,NO_CHANGE,0.05,0.5,3);
			ObjMove_AddPatternA2(obj2,60,3,NO_CHANGE,-0.05,-5,2);
			ObjMove_AddPatternA2(obj2,90,2,NO_CHANGE,0.05,-0.5,5);
			wait(100);
		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);
}