#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell5 Ex"]
#Text["Suncheon Stronghold Assult"]
#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(5000);
	}
	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,0,0);
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	Start;
	BossRender;
	movement;
	end;
} 


task Start{
	let BossImg = GetCurrentScriptDirectory ~ "img/Cut-ins/Yi/YSS Normal.png";
	ObjCutin_SetSpellcardS4("\"Suncheon Stronghold Assult\"", BossImg, KANAKO, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, BossImg, "Easy Extra");
}


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

FireA1_2;
wait(60);

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

ObjEnemy_SetDamageRate(bossObj,125,1);
FireA1_1;

while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 3350) {
		yield;
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	PlaySE(shot2);

//Phase2
ObjEnemy_SetDamageRate(bossObj,0,0);

BossConcentrateB1(45);
PlaySE(charge5);
BFX_FocusA1(bossX,bossY,125,VFX_Smoke,VFX_Beams,45);
wait(60);

FireB1_2;
wait(60);

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

ObjEnemy_SetDamageRate(bossObj,125,1);
FireB1_1;

while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 1650) {
		yield;
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	
//Phase 3
PlaySE(shot2);
	
ObjEnemy_SetDamageRate(bossObj,0,0);	

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

FireC1_2;
wait(60);

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

ObjEnemy_SetDamageRate(bossObj,30,1);

	
}

task FireA1_1{//----

//Phase1 -- -- -- -- -- -- -- --
	let N = 7;
	let waveN = 5;
	let A = (360/N)/2;
	let spd = 3;
	loop{
		if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<3350)){return;}
		let ang = 90;
	loop(N){
		CreateShotA2(bossX,bossY,0.1,ang+A,0.1,spd,172,0);
		ang+=360/N;
	}
	A+=(360/N)/2;
		wait(30);
	}
}

task FireA1_2{
	let ang = 0;
	loop{
		if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<3350)){return;}
	PlaySE(shot4);
		let objB = CreateShotA2(bossX,bossY,15,ang,-1,0.5,166,0);
		ObjRender_SetBlendType(objB,BLEND_ALPHA);
		ObjShot_SetSpellResist(objB,true);
		FireobjB(objB);
		move_objA1_2(objB);
		ang+=180;
	wait(120);
	}
}


task move_objA1_2(obj){
	wait(30);
	loop(120){
		if(Obj_IsDeleted(obj)){return;}
	ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
	ObjMove_SetAcceleration(obj,0.05);
	ObjMove_SetMaxSpeed(obj,5);
	yield;
	}
}

task FireobjB(objB){//----
	wait(30);
	let trig = 90;
	let ang = rand(0,360);
	loop(15){
		if(Obj_IsDeleted(objB)){return;}
		PlaySE(shot1);
		loop(5){
		CreateShotA2(ObjMove_GetX(objB)+trig*cos(ang),ObjMove_GetY(objB)+trig*sin(ang),0.5,ang,0.05,3,285,30);
		ang+=360/5;
		}
		ang+=21;
		trig-=90/15;
		wait(5);
		}
		if(Obj_IsDeleted(objB)){return;}
		PlaySE(Eff9);
	let ang2 = rand(0,360);
	let N = 10;
	let waveN = 5;
	let A = (360/N)/2;
	let spd = 0.75;
	loop(waveN){
	loop(N){
		let objBA = CreateShotA2(ObjMove_GetX(objB),ObjMove_GetY(objB),0.1,ang+A,0.1,spd,26,0);
		ang+=360/N;
	}
	spd+=0.75;
	A+=(360/N)/2;
	}
	Obj_Delete(objB);
}

//Phase2 -- -- -- --

task FireB1_1{
let N = 30;
let A = (360/N)/2;
loop{
	let ang = 90;
	if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<1650)){return;}
	
	let waveN = 3;
	
	let spd = 2;
	loop(waveN){
	loop(N){
		CreateShotA2(bossX,bossY,0.1,ang+A,0.1,spd,27,0);
		ang+=360/N;
	}
	spd+=0.5;
	A+=(360/N)/2;
	}
	wait(75);
}
}


task FireB1_2{
	loop{
		if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<1650)){return;}
		PlaySE(shot4);
		let ang = GetAngleToPlayer(bossObj);
		ascent(i in -1..2){
		let objB = CreateShotA2(bossX,bossY,15,ang+(i*30),-1,0.5,167,0);
		ObjRender_SetBlendType(objB,BLEND_ALPHA);
		ObjShot_SetSpellResist(objB,true);
		FireobjB2(objB);
		move_objB1_2(objB);
		}
	wait(180);
	}
}


task FireobjB2(objB){//----
	wait(30);
	let trig = 10;
	loop(10){
		if(Obj_IsDeleted(objB)){return;}
		let ang = GetAngleToPlayer(objB);
		PlaySE(shot3);
		loop(5){
		CreateShotA2(ObjMove_GetX(objB)+trig*cos(ang),ObjMove_GetY(objB)+trig*sin(ang),0.5,GetAngleToPlayer(objB)-180,0.05,3,286,30);
		ang+=360/5;
		}
		ang+=360/5/2;
		trig-=10/15;
		wait(7.5);
		}
		if(Obj_IsDeleted(objB)){return;}
	PlaySE(Eff9);
	let ang3 = GetAngleToPlayer(objB);
	loop(10){
		ascent(i in 2..9){
		let shotdest = CreateShotA2(ObjMove_GetX(objB),ObjMove_GetY(objB),0,ang3,0.025,i/2,11,0);
		}
		ang3+=360/10;
	}
	Obj_Delete(objB);
}

task move_objB1_2(obj){
	wait(30);
	ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
	ObjMove_SetAcceleration(obj,0.05);
	ObjMove_SetMaxSpeed(obj,5);
}

//Phase 3 -- --- ----
task FireC1_1{
loop{
	if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<0)){return;}
	yield;
}
}


task FireC1_2{
	loop{
		if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<0)){return;}
		PlaySE(shot4);
		let ang = 90;
		loop(6){
			if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<0)){return;}
		let objB = CreateShotA2(bossX,bossY,15,ang,-1,0.5,165,0);
		ObjRender_SetBlendType(objB,BLEND_ALPHA);
		ObjShot_SetSpellResist(objB,true);
		FireobjC2_1(objB);
		move_objC1_2(objB);
		ang+=360/6;
		}
	wait(180);
	if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<0)){return;}
	ang = rand(0,360); 
	PlaySE(shot4);
	loop(6){
		if((ObjEnemy_GetInfo(bossObj,INFO_LIFE)<0)){return;}
		let objB = CreateShotA2(bossX,bossY,15,ang,-1,0.5,171,0);
		ObjRender_SetBlendType(objB,BLEND_ALPHA);
		ObjShot_SetSpellResist(objB,true);
		FireobjC2_2(objB);
		move_objC2_2(objB);
		ang+=360/6;
		}
	wait(180);
	}
}

task move_objC1_2(obj){
	wait(30);
	ObjMove_SetAngle(obj,ObjMove_GetAngle(obj)+45);
	ObjMove_SetAcceleration(obj,0.05);
	ObjMove_SetMaxSpeed(obj,5);
}

task move_objC2_2(obj){
	wait(30);
	ObjMove_SetAngle(obj,ObjMove_GetAngle(obj)-45);
	ObjMove_SetAcceleration(obj,0.05);
	ObjMove_SetMaxSpeed(obj,5);
}

task FireobjC2_1(objB){//----
	wait(30);
	let trig = 90;
	loop(10){
		if(Obj_IsDeleted(objB)){return;}
		let ang = ObjMove_GetAngle(objB);
		PlaySE(shot3);
		loop(4){
		CreateShotA2(ObjMove_GetX(objB)+trig*cos(ang),ObjMove_GetY(objB)+trig*sin(ang),0.5,ObjMove_GetAngle(objB)-180,0.01,3,284,30);
		ang+=360/4;
		}
		trig-=90/10;
		wait(7.5);
		}
		if(Obj_IsDeleted(objB)){return;}
	PlaySE(Eff9);
	let ang3 = GetAngleToPlayer(objB);
	loop(5){
		ascent(i in 1..9){
			ascent(a in -1..2){
		let shotdest = CreateShotA2(ObjMove_GetX(objB),ObjMove_GetY(objB),0,ang3+a*5,0.025,i/2,9,0);
			
			}
		}
		ang3+=360/5;
	}
	Obj_Delete(objB);
}

task FireobjC2_2(objB){//----
	wait(30);
	let trig = 90;
	loop(10){
		if(Obj_IsDeleted(objB)){return;}
		let ang = ObjMove_GetAngle(objB);
		PlaySE(shot3);
		loop(4){
		CreateShotA2(ObjMove_GetX(objB)+trig*cos(ang),ObjMove_GetY(objB)+trig*sin(ang),0.5,ObjMove_GetAngle(objB)-180,0.01,3,290,30);
		ang+=360/4;
		}
		wait(7.5);
		trig-=90/10;
		}
		if(Obj_IsDeleted(objB)){return;}
	PlaySE(Eff9);
	let ang3 = GetAngleToPlayer(objB);
	loop(5){
		ascent(i in 1..9){
			ascent(a in -1..2){
		let shotdest = CreateShotA2(ObjMove_GetX(objB),ObjMove_GetY(objB),0,ang3+a*5,0.025,i/2,15,0);
	
			}
		}
		ang3+=360/5;
	}
	Obj_Delete(objB);
}

task end {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	PlaySE(SpellOff);
	TMotionBlur();
	BFX_FocusA1(bossX,bossY,250,VFX_Smoke,VFX_Beams,180);
	PlaySE(BossExplode);
	loop(240){
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	yield;
	}
	BFX_ExplosionA1(bossX,bossY,250,VFX_Smoke,60);
	Obj_Delete(bossObj);
	PlaySE(BossExplode);
	loop(120){
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	yield;
	}
	SetAutoDeleteObject(true);
	wait(30);	
	CloseScript(GetOwnScriptID());
}

task TMotionBlur(){//from sample scripts
	let renderTexture = [GetReservedRenderTargetName(0), GetReservedRenderTargetName(1)];
	SetInvalidRenderPriorityA1(20, 79);

	let objCurentFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objCurentFrame, 80);
	ObjRender_SetPermitCamera(objCurentFrame, false);

	let objOldFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objOldFrame, 0);
	ObjRender_SetPermitCamera(objOldFrame, false);
	ObjRender_SetAlpha(objOldFrame, 224);
	Obj_SetVisible(objOldFrame, false);

	let frame = 0;
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0)
	{
		let currentFrameTexture = renderTexture[frame % 2];

		RenderToTextureA1(currentFrameTexture, 20, 79, true);

		if(frame >= 1) 
		{
			
			let oldFrameTexture = renderTexture[(frame - 1) % 2];
			ObjPrim_SetTexture(objOldFrame, oldFrameTexture);
			ObjSprite2D_SetSourceRect(objOldFrame, 32, 16, 416, 464);
			ObjSprite2D_SetDestRect(objOldFrame, 32, 16, 416, 464);
			RenderToTextureB1(currentFrameTexture, objOldFrame, false);
		}

		ObjPrim_SetTexture(objCurentFrame, currentFrameTexture);
		ObjSprite2D_SetSourceRect(objCurentFrame, 32, 16, 416, 464);
		ObjSprite2D_SetDestRect(objCurentFrame, 32, 16, 416, 464);

		frame++;
		yield;
	}

	Obj_Delete(objCurentFrame);
	Obj_Delete(objOldFrame);
	ClearInvalidRenderPriority();
}