#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 5"]
#Text["Final showdown"]

let objBoss;
#include "./Functions/Index.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 90);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtSpeed(objBoss,384/2,180,4);
	Mega = true;
	FlameAura(objBoss,[255,150,255],3);
	mainTask;	
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(6000);
        }
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(2000000);
	}
} 

let BossX = 0;
let BossY = 0;

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,24);
	
	yield;
} 

@Finalize {

} 

task mainTask { 
	renderdiancie(objBoss);
	BottomEnemyMarker(objBoss, objBoss);
	end;
	action;
	BossShotSound(objBoss, 75, 300);
} 

task SpellCall {
	bspell = true;
	TCallBg2;
}
let Diancie = GetCurrentScriptDirectory ~ "Functions/img/Cutin/MDiancieCutin.png";
task action{
wait(60);
ObjCutin_SetSpellcardS3("&quot;Royal Diamond Princess&quot;", 255, 15, 200);
ObjCutin_LaunchS3("KANAKO", Diancie,"Normal");
SpellCall;
let objScene = GetEnemyBossSceneObjectID();
ObjEnemyBossScene_StartSpell(objScene);
wait(100);
Attack;
wait(50);
ObjEnemy_SetDamageRate(objBoss,50,30);
}

task Attack{
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/charge.wav", 100);
ConcentrationA1(objBoss,"MAGIC",30,2,120,[255,150,255]);
wait(30);
stage1();
stage2();
stage3();
}

function stage1{
CrystalSpawner(50);
wait(60);
let angle = 0;
while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
	loop(2){
    let obj = CreateShotA1(BossX+15*cos(angle),BossY+15*sin(angle),1,angle,ORB_L_RED,10);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
	Fire(obj);
	ObjShot_SetSpellResist(obj, 1);
    angle+=360/2;
    }
	angle += 22;
	wait(60);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 4500){
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 100);
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	break;}
}
task Fire(tar){
	while(!Obj_IsDeleted(tar)){
	let ang = ObjMove_GetAngle(tar)+90;
	loop(2){
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),1.5,ang,ICE_RED,10);
	ang += 180;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(30);
	}
}
}

function stage2{
CrystalSpawner(70);
wait(40);
while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
	let angle = 90+45;
	let angle2 = 90-45;
	loop(5){
	Shoot1(angle,angle2);
	angle += 10;
	angle2 -= 10;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 3000){break;}
	wait(60);
	}
	wait(60);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 3000){
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 100);
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	break;}
}
task Shoot1(ang,ang2){
	let obj = CreateShotA1(BossX+15*cos(ang),BossY+15*sin(ang),1,ang,ORB_L_PINK,10);
	Fire2(obj);
	let obj2 = CreateShotA1(BossX+15*cos(ang2),BossY+15*sin(ang2),1,ang2,ORB_L_RED,10);
	Fire(obj2);
}
task Fire(tar){
	while(!Obj_IsDeleted(tar)){
	let ang = ObjMove_GetAngle(tar)+90;
	loop(2){
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),1.5,ang,ICE_RED,10);
	ang += 180;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(30);
	}
}
task Fire2(tar){
	while(!Obj_IsDeleted(tar)){
	let ang = ObjMove_GetAngle(tar)+90;
	loop(2){
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),1.5,ang,ICE_PINK,10);
	ang += 180;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(30);
	}
}
}

function stage3{
CrystalSpawner(120);
wait(30);
let angle = 0;
while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
	loop(3){
    let obj = CreateShotA1(BossX+15*cos(angle),BossY+15*sin(angle),1,angle,ORB_L_PINK,10);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
	FireCrystal(obj);
	ObjShot_SetSpellResist(obj, 1);
    angle+=360/3;
    }
	angle -= 22;
	wait(60);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 1500){
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 100);
	CrystalSpawner(150);
	wait(20);
	ObjEnemy_SetDamageRate(objBoss,45,30);
	FinalStage;
	break;
	}
function FinalStage{
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 1501) {
	let angle = 90+45;
	let angle2 = 90-45;
	loop(12){
	Shoot1(angle,angle2);
	angle += 10;
	angle2 -= 10;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
	wait(3);
	}
	wait(120);
	loop(12){
	Shoot1(angle,angle2);
	angle -= 10;
	angle2 += 10;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 60);
	wait(3);
	}
	wait(120);
}
}		
task FireCrystal(tar){
	while(!Obj_IsDeleted(tar)){
	let ang = ObjMove_GetAngle(tar)+90;
	loop(2){
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),1.5,ang,ICE_PINK,10);
	ang += 180;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(30);
	}
}
task Shoot1(ang,ang2){
	let obj = CreateShotA1(BossX+35*cos(ang),BossY+35*sin(ang),1,ang,ORB_L_PINK,10);
	Fire2(obj);
	let obj2 = CreateShotA1(BossX+35*cos(ang2),BossY+35*sin(ang2),1,ang2,ORB_L_RED,10);
	Fire(obj2);
}
task Fire(tar){
	while(!Obj_IsDeleted(tar)){
	let ang = ObjMove_GetAngle(tar)+90;
	loop(2){
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),1.5,ang,ICE_RED,10);
	ang += 180;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(30);
}
}
task Fire2(tar){
	while(!Obj_IsDeleted(tar)){
	let ang = ObjMove_GetAngle(tar)+90;
	loop(2){
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),1.5,ang,ICE_PINK,10);
	ang += 180;
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(30);
	}
}
}
}

task Orbit(obj,tar,angle,rad,spd){
    let radius = 0;
    let speed = 0;
    let accel = 3;
    while(!Obj_IsDeleted(obj)){
	let tarX = ObjMove_GetX(tar);
	let tarY = ObjMove_GetY(tar);
    ObjMove_SetPosition(obj,tarX+radius*cos(angle+speed*spd),tarY+radius*sin(angle+speed*spd));
	ObjMove_SetAngle(obj, GetAngleTo(obj,tar)+180);
    if(radius<rad){radius+=3;}
    speed+=accel;
    if(accel>0.5){accel -= 0.03;}
    yield;
    }
}

task FireEpically(tar){
wait(60);
let ang = ObjMove_GetAngle(tar)+90;
	while(!Obj_IsDeleted(tar)){
	loop(3){
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),1.5,ang,ORB_M_GRAY,10);
	ang += 360/3;
	}
	ang += 11;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);
	wait(10);
	}
}

task CrystalSpawner(len){
	let angle = 0;
	wait(20);
	loop(2){
	let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, angle, ICEBREAKER_PINK, 20);
	ObjShot_SetSpellResist(obj, 1);
	IndicationLaser(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),angle,40);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/barrage.wav", 90);
	angle += 180;
	Crystals(obj,len);
	task Crystals(obj,lengh){
    while(!Obj_IsDeleted(obj))
    {
	let spd = ObjMove_GetSpeed(obj)/2;
	if(trunc(ObjMove_GetX(obj))>384)
	{
	let woop = ObjMove_GetX(obj);
	let woop2 = ObjMove_GetY(obj);
	let angle = ObjMove_GetAngle(obj)+180;
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laser2.wav", 50);
		ascent(i in 0..110){
		let las1 = CreateStraightLaserA1(woop,woop2+(i*2.5),angle+rand(-5,5), lengh+rand(-20,20), 15,15000, ICE_ROSE, 0);
		ObjRender_SetColor(las1,253,75,234);
		let las2 = CreateStraightLaserA1(woop,woop2-(i*2.5),angle+rand(-5,5), lengh+rand(-20,20), 15,15000, ICE_ROSE, 0);
		ObjRender_SetColor(las2,253,75,234);
		wait(1);
		}
	}
	if(trunc(ObjMove_GetX(obj))<0)
    {      	
	let woop = ObjMove_GetX(obj);
	let woop2 = ObjMove_GetY(obj);
	let angle = ObjMove_GetAngle(obj)+180;
	ascent(i in 0..130){
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/laser2.wav", 50);
		let las1 = CreateStraightLaserA1(woop,woop2+(i*2.5),angle+rand(-5,5), lengh+rand(-20,20), 15,15000, ICE_ROSE, 0);
		ObjStLaser_SetSource(las1, 0);
		ObjRender_SetColor(las1,253,75,234);
		let las2 = CreateStraightLaserA1(woop,woop2-(i*2.5),angle+rand(-5,5), lengh+rand(-20,20), 15,15000, ICE_ROSE, 0);
		ObjStLaser_SetSource(las2, 0);
		ObjRender_SetColor(las2,253,75,234);
		wait(1);
	}
    }
yield;
}
}
	}
}


task IndicationLaser(x,y,angle,g){
	let obj = CreateStraightLaserA1(x,y,angle,1024,50,0,416,1111);
	ObjStLaser_SetSource(obj, 0);
	ObjRender_SetAlpha(obj, 122);
	wait(g);
	Obj_Delete(obj);
}

task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
}

