#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell1 Ex"]
#Text["Morale 'Removing the Burrowing bullet' "]
#Background["./Background.txt"]
#System["./System test/SystemMain.txt"]
#BGM ["./BGM/The Whirlwind of Seven Evil Samurais.mp3"]
let bossObj;
let bossX = 0;
let bossY = 0;

#include "./Expanded CTC (Darkness1)/Expanded_ShotConst.txt"
#include"./FX/Bloom VFX darkwalker247/BloomFX.txt"
#include"./lib.txt"
#include"./FX/Effects.cs"
#include"./Cutin/Cutin.txt"

@Initialize {
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	ObjMove_SetDestAtSpeed(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/4,5);
	SFXload;
	mainTask;
} 

@Event { 
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(4206);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

@MainLoop {
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,32);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,20);
	
	yield;
} 

@Finalize {
} 

task mainTask {
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	ObjEnemy_SetDamageRate(bossObj,1,1);
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	Start;
	BossRender;
	movement;
	end;
} 


task Start{
	let BossImg = GetCurrentScriptDirectory ~ "img/Cut-ins/Yi/YSS Normal.png";
	ObjCutin_SetSpellcardS4("Morale \"Removing the Burrowing Round\"", BossImg, KANAKO, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, BossImg, "Extra");
}

task movement {
wait(120);
BossConcentrateB1(45);
PlaySE(charge5);
BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
wait(60);

fireA1(90);
fireA2(90);

wait(240);
BossConcentrateB1(45);
PlaySE(charge5);
BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
wait(60);
firefollowingshot;
ObjEnemy_SetDamageRate(bossObj,100,50);
loop{
fireA1(rand(0,360));
loop(3){
ObjMove_SetDestAtSpeed(bossObj,rand(GetPlayerX-30,GetPlayerX+30),75,1);
wait(150/3);
}
PlaySE(charge5);
BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
wait(30);
fireA2(rand(0,360));
loop(3){
ObjMove_SetDestAtSpeed(bossObj,rand(GetPlayerX-30,GetPlayerX+30),75,1);
wait(150/3);
}
PlaySE(charge5);
BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
wait(50);
ObjMove_SetDestAtSpeed(bossObj,rand(GetPlayerX-30,GetPlayerX+30),75,1);
}

}

task fireA1(ang){
	PlaySE(shot7);
	loop(5){
		let ObjFireA = CreateShotA1(bossX,bossY,3,ang-180,25,0);
		ObjFireAshot(ObjFireA);
		ObjFireAdel(ObjFireA);
		ObjShot_SetSpellResist(ObjFireA,true);
		ObjShot_SetAutoDelete(ObjFireA,false);
		move_ObjFireA(ObjFireA,ang);
		ang+=360/25;
	}
}

task move_ObjFireA(obj,ID){ //credits: Sparen
	let objcount = 0;
	let trig = 0;
	let X = bossX;
	let Y = bossY;
	while(!Obj_IsDeleted(obj)){
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount++;
            trig+=0.75;
            yield;
        }
	
}

task ObjFireAshot(obj){
	wait(30);
	loop(15){
		if(Obj_IsDeleted(obj)){return;}
		PlaySE(shot3);
		loop(10){
			if(Obj_IsDeleted(obj)){return;}
			CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,ObjMove_GetAngle(obj),0.01,5,81,0);
			wait(1);
		}
		wait(10);
	}
	ObjShot_SetAutoDelete(obj,true);
}

//----

task fireA2(ang){
	PlaySE(shot7);
	loop(5){
		let ObjFireB = CreateShotA1(bossX,bossY,3,ang-180,30,0);
		ObjShot_SetSpellResist(ObjFireB,true);
		ObjShot_SetAutoDelete(ObjFireB,false);
		ObjFireBshot(ObjFireB);
		move_ObjFireB(ObjFireB,ang);
		ang+=360/25;
	}
}

task move_ObjFireB(obj,ID){ //credits: Sparen
	let objcount = 0;
	let trig = 0;
	let X = bossX;
	let Y = bossY;
	while(!Obj_IsDeleted(obj)){
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount--;
            trig+=0.75;
            yield;
        }
	
}

task ObjFireBshot(obj){
	wait(30);
	loop(15){
		if(Obj_IsDeleted(obj)){return;}
		PlaySE(shot3);
		loop(5){
			if(Obj_IsDeleted(obj)){return;}
			CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,ObjMove_GetAngle(obj),0.025,3,86,0);
			wait(1);
		}
		wait(15);
	}
	ObjShot_SetAutoDelete(obj,true);
}

task ObjFireAdel(obj){wait(1200);Obj_Delete(obj);}

task firefollowingshot{
	wait(60);
	loop{
	PlaySE(Eff1);
	let follow = CreateShotA2(bossX,bossY,3.5,GetAngleToPlayer(bossObj),-0.1,0,27,0);
	ObjShot_SetSpellResist(follow,true);
		ObjShot_SetAutoDelete(follow,false);
	movefollow(follow);
	wait(600);
	}
}

task movefollow(follow){
	loop(9){
		wait(60);
		if(Obj_IsDeleted(follow)){return;}
		PlaySE(Eff1);
		let ang = GetAngleToPlayer(follow);
		loop(10){
		ascent(i in -2..3){
		let Fshot = CreateShotA2(ObjMove_GetX(follow),ObjMove_GetY(follow),0.1,ang+(i*1.5)+360/10/2,0.025,5,19,0);
		ObjRender_SetBlendType(Fshot,BLEND_ADD_ARGB)
		
	}
	ang+=360/10;
	ObjMove_SetSpeed(follow,3.5);
	ObjMove_SetAngle(follow,ang);
	}
	}
	BFX_ExplosionA1(ObjMove_GetX(follow),ObjMove_GetY(follow),100,VFX_Smoke,5);
	Obj_Delete(follow);
}


task end {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	PlaySE(SpellOff);
	SetAutoDeleteObject(true);
	loop(5){
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	yield;
	}
	Obj_Delete(bossObj);
	wait(30);	
	CloseScript(GetOwnScriptID());
}