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

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

let imgBoss = CSD ~ "/Texture/Charlotta.png";
let AttackSFX = CSD ~ "/SFX/ATTACK4.wav";
let Attack2SFX = CSD ~ "/SFX/ATTACK2.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,"Judgement Rush",5);
	LoadSound(AttackSFX);
	mainTask;
 }

@Event {
	
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(4500);
	}		
	case(EV_REQUEST_TIMER) {
		SetScriptResult(70);
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(500000);
	}
 }

@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("CHARLOT");
	callbackboss;
	TEnd;
	Fire2;
	wait(200);
	Fire;
 }
 
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 T= 0;
	let Angle;
	loop{
		ObjMove_SetDestAtFrame(bossObj,GetPlayerX,rand(90,131),60);
		wait(40);
		loop(5){TChargeEffect(bossX,bossY);}
		wait(20);
		Angle=GetAngleToPlayer(bossObj);
		PlaySound(Attack2SFX);
		loop(5){
		CreateShotA2(bossX+30*sin(Angle-T+90),bossY-30*cos(Angle-T+90),5,Angle-T,-0.05,2,260,12);
		CreateShotA2(bossX+30*sin(Angle+T+90),bossY-30*cos(Angle+T+90),5,Angle+T,-0.05,2,260,12);
		wait(1);
		T+=2;
		}
		T=0;
		wait(240);
		if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) <= 0) {break;}
		yield;
	}
}

task Fire2{
	wait(60);
	loop{
		PlaySound(AttackSFX);
		let obj= CreateCurveLaserA1(-20,bossY+rand(-50,51),10,rand(45,0),100,40,264,0);
			ObjMove_AddPatternA2(obj,0,0,NO_CHANGE,1,-0.5,10);
			BulletVanish(obj);
			wait(50);
			
		PlaySound(AttackSFX);
		let obj2= CreateCurveLaserA1(400,bossY+rand(-50,51),10,rand(145,181),100,40,264,0);
			ObjMove_AddPatternA2(obj2,0,0,NO_CHANGE,1,0.5,10);
			BulletVanish(obj2);
			wait(50);
		yield;
	}
}

task BulletVanish(obj){
	let B2=rand(0,360);
	let T = rand(0,11);
	let angle2=0;
	loop{
		while (angle2<360){	
		CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),3,angle2+B2,-0.2,2,176,6);
		angle2+=360/4;
		}
		angle2=0;
		if(T>=4){B2+=16;}else{B2-=16;}
		wait(2);
		if(Obj_IsDeleted(obj)){break;}
		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);
}