#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell4 Ea"]
#Text["The Man Who Seeked Death"]
#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;

let rivermove = false;
let straightup = true;

#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,GetCenterX,GetCenterY,5);
	SFXload;
	mainTask;
} 

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

@MainLoop {
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	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("\"The Man Who Seeked Death\"", BossImg, KANAKO, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, BossImg, "Easy Extra");
}

task movement {
wait(60);
BossConcentrateB1(45);
PlaySE(charge5);
BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
wait(60);
survivalcard = true;
Phase1;
wait(180);
fireA1(rand(0,360));//Phase2
wait(7*60);
Phase1_2;//Phase2 part2
wait(8*60);
Phase2;
wait(300);
Phase2Lasers;
wait(15*60);
Phase3;
}

task Phase1{
	let ang = 90;
	let N = 10;
	let waveN = 5;
	let A = (360/N)/2;
	let spd = 0.75;
	loop(waveN){
	loop(N){
		CreateShotA2(bossX,bossY,0.1,ang+A,0.1,spd,172,0);
		ang+=360/N;
	}
	spd+=0.75;
	A+=(360/N)/2;
	}
}

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

task move_ObjFireA(obj,ID){ //credits: Sparen
	let objcount = 0;
	let trig = 300;
	let X = bossX;
	let Y = bossY;
	loop(100){
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount++;
            trig-=1;
            yield;
        }
	loop(60*15-100){//Phase1
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount++;
            yield;
        }
	loop(120){//Phase1 exit
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount++;
			trig+=3;
            yield;
        }
	
}

task ObjFireAshot(obj){
	wait(30);
	loop(88*2){
		if(Obj_IsDeleted(obj)){return;}
		PlaySE(shot3);
			if(Obj_IsDeleted(obj)){return;}
			let ang = ObjMove_GetAngle(obj);
			loop(3){
			CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,ang,0.01,5,81,0);
			ang+=360/3;
			}
			wait(5);
	}
	ObjShot_SetAutoDelete(obj,true);
}

task Phase1_2{//----
	let delay = 480;
	loop(5){
		let ang = rand(0,360);
		
		loop(10){
			PlaySE(Eff1);
			let Phase1Shot = CreateShotA2(GetPlayerX+60*cos(ang),GetPlayerY+60*sin(ang),5,ang,-0.25,0,14,10); 
			ObjShot_SetSpellResist(Phase1Shot,true);
			ObjShot_SetAutoDelete(Phase1Shot,false);
			Delete_Phase1(Phase1Shot,delay);
			ang+=360/10;
			}
			delay-=420/5;
		wait(7*60/5);
	}
}
task Delete_Phase1(obj,delay){
	wait(delay);
	PlaySE(famdiefx);
	BFX_ShockwaveA1(ObjMove_GetX(obj),ObjMove_GetY(obj),VFX_Sparkle,rand(0,360),255,1,1,30,45,BLEND_ADD_ARGB,10,0,0);
	Obj_Delete(obj);
}
/*-- --- ----- END OF PHASE 1 ----- --- --*/

task Phase2{
	let ang = 90;
loop(5){
		let Phase2ShotA = CreateShotA1(bossX,bossY,3,ang-180,25,0);
		let Phase2ShotB = CreateShotA1(bossX,bossY,3,ang-180,30,0);
		ObjShot_SetSpellResist(Phase2ShotA,true);
		ObjShot_SetSpellResist(Phase2ShotB,true);
		ObjShot_SetAutoDelete(Phase2ShotA,false);
		ObjShot_SetAutoDelete(Phase2ShotB,false);
		move_Phase2A(Phase2ShotA,ang);
		Shoot_Phase2A(Phase2ShotA);
		move_Phase2B(Phase2ShotB,ang);
		Shoot_Phase2B(Phase2ShotB);
		ang+=360/(5^2);
	}
}

task move_Phase2A(obj,ID){ //credits: Sparen
	let objcount = 0;
	let trig = 300;
	let X = bossX;
	let Y = bossY;
	let N = 0.5;
	loop(100){
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount+=N;
            trig-=1;
            yield;
        }
	loop(60*20-130){//Phase2
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
             objcount+=N;
            yield;
        }
		ObjShot_SetAutoDelete(obj,true);
	loop(120){//Phase2 exit
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
             objcount+=N;
			trig+=3;
            yield;
        }
	
}

task move_Phase2B(obj,ID){ //credits: Sparen
	let objcount = 0;
	let trig = 300;
	let X = bossX;
	let Y = bossY;
	let N = 0.5;
	loop(100){
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount-= N;
            trig-=1;
            yield;
        }
	loop(60*20-130){//Phase2
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount-= N;
            yield;
        }
		ObjShot_SetAutoDelete(obj,true);
	loop(120){//Phase2 exit
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount-= N;
			trig+=3;
            yield;
        }
	
}

task Shoot_Phase2A(obj){
	wait(60);
	loop(20){
		if(Obj_IsDeleted(obj)){return;}
		PlaySE(shot3);
		loop(10){
			if(Obj_IsDeleted(obj)){return;}
			let ang = ObjMove_GetAngle(obj);
			loop(3){
			let shotA = CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,ang,0.01,5,81,0);
			ang+=360/3;
			ObjDel(shotA);
			}
			wait(2.5);
		}
		wait((900/20)-25);
	}
	ObjShot_SetAutoDelete(obj,true);
}

task Shoot_Phase2B(obj){
	wait(60);
	loop(62){
		if(Obj_IsDeleted(obj)){return;}
		PlaySE(shot1);
		let ang = ObjMove_GetAngle(obj);
		loop(3){
			let shotA = CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0.1,ang,0.01,5,86,0);
			ang+=360/3;
			ObjDel(shotA);
		}
			wait(2.5);
		wait(900/61);
	ObjShot_SetAutoDelete(obj,true);
}
}

task Phase2Lasers{
	let ang = 90;
	PlaySE(laser1);
	loop(5){
		let Phase2LaserA = CreateStraightLaserA1(bossX,bossY,ang,500,10,3.75*60,286,120);
		move_Phase2LaserA(Phase2LaserA,ang);
		ang+=360/(5^2);
	}
	wait(15*30);
	PlaySE(laser1);
	loop(5){
		let Phase2LaserB = CreateStraightLaserA1(bossX,bossY,ang,500,10,7.5*60,286,120);
		move_Phase2LaserB(Phase2LaserB,ang);
		ang+=360/(5^2);
	}
}

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

task move_Phase2LaserB(obj,ID){ //credits: Sparen
		let objcount = 0;
	let trig = 0;
	let X = bossX;
	let Y = bossY;
	let N = 0.5;
	while(!Obj_IsDeleted(obj)){
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjStLaser_SetAngle(obj, (ID*30 + objcount));
            objcount+=N;
            yield;
        }
}

task ObjDel(shotA){
wait(190);
Obj_Delete(shotA);
}	

/*-- --- ----- END OF PHASE 2----- --- --*/
task Phase3{
	PlaySE(famdiefx);
	let ang = 90;
	let ang2 = 270;
	let trig = 300;
	phase3shot(trig);
	loop(120){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
loop(7){
		let Phase3ShotA = CreateShotA1(bossX+trig*cos(ang),bossY+trig*sin(ang),3,ang,25,0);
		let Phase3ShotB = CreateShotA1(bossX+trig*cos(ang2),bossY+trig*sin(ang2),3,ang2,30,0);
		ObjShot_SetSpellResist(Phase3ShotA,true);
		ObjShot_SetSpellResist(Phase3ShotB,true);
		ang+=360/7;
		ang2+=360/7;
	}
	ang+=5;
	ang2-=5;
	trig-=1;
	wait(2.5);
	}
	loop{
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	loop(7){
		let Phase3ShotA = CreateShotA1(bossX+trig*cos(ang),bossY+trig*sin(ang),3,ang,25,0);
		let Phase3ShotB = CreateShotA1(bossX+trig*cos(ang2),bossY+trig*sin(ang2),3,ang2,30,0);
		ObjShot_SetSpellResist(Phase3ShotA,true);
		ObjShot_SetSpellResist(Phase3ShotB,true);
		ang+=360/7;
		ang2+=360/7;
	}
	trig-=0.15;
	ang+=5;
	ang2-=5;
	wait(2.5);
	}
}

task phase3shot(trig){//----
	let ang = rand(0,360);
	loop(6){
		let Phase3ShotAWhite = CreateShotA1(bossX+trig*cos(ang),bossY+trig*sin(ang),3,ang-180,32,0);
		effWhite(Phase3ShotAWhite);
		ObjShot_SetSpellResist(Phase3ShotAWhite,true);
		ObjShot_SetAutoDelete(Phase3ShotAWhite,false);
		ObjMoveShotWhite(Phase3ShotAWhite,ang,trig);
		ObjShootWhite(Phase3ShotAWhite);
		ang+=360/6^2;
	}
}

task effWhite(obj){
	loop{
		if(Obj_IsDeleted(obj)){return;}
	BFX_ExplosionA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,VFX_Smoke,1);
	wait(5);
	}
}

task ObjMoveShotWhite(obj,ID,trig){
	let objcount = 0;
	let X = bossX;
	let Y = bossY;
	let N = 1.5;
	loop(120){
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount-= N;
            trig-=0.4;
            yield;
        }
	loop(13*60){//Phase2
	if(Obj_IsDeleted(obj)){return}
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount-= N;
			trig-=0.1;
            yield;
        }
	loop{//Phase2
	if(Obj_IsDeleted(obj)){return}
            ObjMove_SetPosition(obj, X + trig*cos(ID*30 + objcount), Y + trig * sin(ID*30 + objcount));
            ObjMove_SetAngle(obj, (ID*30 + objcount)-180);
            objcount-= N;
			trig-=0.3;
            yield;
        }
		ObjShot_SetAutoDelete(obj,true);
}

task ObjShootWhite(obj){
	wait(180);
	loop(5){
		PlaySE(shot9);
		ascent(i in -1..2){
		let A = CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),7,ObjMove_GetAngle(obj)+(i*10),-0.75,1,48,0);
		ObjShot_SetSpellResist(A,true);
		}
		wait(60);
	}
	loop(7){
		PlaySE(shot4);
		ascent(i in -2..3){
		let A = CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),7,ObjMove_GetAngle(obj)+(i*20),-0.75,1,48,0);
		ObjShot_SetSpellResist(A,true);
		}
		wait(60);
	}
}

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