#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 6"]
#Text["Woop"]

let objBoss;
let BossX;
let BossY;

let timer = 65;

#include "./Functions/Index.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	renderorin(objBoss);
	BossShotSound(objBoss, 100, 500);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	
	timercountdown;
	cat = true;
	BottomEnemyMarker(objBoss, objBoss);
	widehearts;
	if(GetCommonData("IsStage", 0) == 0){widescreen;}
	ObjMove_SetDestAtWeight(objBoss,384/2-24,150,5,5);
	mainTask;
	end;
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(4500);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(120);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

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

@Finalize {

}

task timercountdown{
	loop{
	wait(60);
	timer-=1;
	}
}

task SpellCall {
	bspell = true;
	TCallBg;
}

let orin = GetCurrentScriptDirectory ~ "Functions/img/orin.png";

task mainTask { 
wait(50);
ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,100,5,5);
	attack = true;
	wait(160);
	SpellBurstA1(BossX, BossY,"Cherry",[100, 00, 00]);
	cat = false;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/kitty.wav", 100);
	wait(50);
	attack = false;
	wait(30);
	ObjCutin_SetSpellcardS3("&quot;Horde of Spirit from the Hell of Blazing Fires&quot;", 200, 00, 00);
	ObjCutin_LaunchS3("KANAKO", orin, "Normal");
	SpellCall;
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	wait(160);
	WelpFinalSpell;
}

task WelpFinalSpell{
Stage1;
Stage2;
Stage3;
}

function Stage1{
wait(60);
let angle = 0;
ObjEnemy_SetDamageRate(objBoss,50,50);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		ascent(i in 0..14){
			let obj = SummonGhost(ObjMove_GetX(objBoss)-240+(i*36), ObjMove_GetY(objBoss)+15*sin(i*30),50,270, true,true);
			MoveFunction(obj, 0.5, 90, 0, 5);
			ObjMove_SetAngularVelocity(obj, -2);
			Prime(obj);
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/water.wav", 50);
			angle+=180;
			wait(6);
		}
		loop(360){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 3500){
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 100);
			DeleteShotAll(TYPE_ALL, TYPE_FADE);
			SetCommonData("NextPhase", 1);
			break;}
		yield;
		}
		if(GetCommonData("NextPhase",0)==1){break;}
	}
	task Prime(obj){
		while(ObjEnemy_GetInfo(obj, INFO_LIFE)>1){yield;}
		ascent(i in 0..6){
			ascent(j in 0..8){
			let shot = CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 2+(j/2), GetAngleToPlayer(obj)+(i*360/6),0.01, 100, SMALL_BLUE, 0);
			}
		}
	}
}

function Stage2{
wait(60);
SetCommonData("NextPhase", 0);
let angle = 0;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		ascent(i in 0..16){
			let obj = SummonGhost(ObjMove_GetX(objBoss)-260+(i*36), ObjMove_GetY(objBoss)+15*sin(i*30),50,200, true,true);
			MoveFunction(obj, 0.5, 90, 0, 5);
			ObjMove_SetAngularVelocity(obj, -1.5);
			Prime(obj);
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/water.wav", 50);
			angle+=180;
			wait(6);
		}
		loop(200){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 2500){
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 100);
			DeleteShotAll(TYPE_ALL, TYPE_FADE);
			SetCommonData("NextPhase", 1);
			break;}
		yield;
		}
		if(GetCommonData("NextPhase",0)==1){break;}
	}
	task Prime(obj){
		while(ObjEnemy_GetInfo(obj, INFO_LIFE)>1){yield;}
		ascent(i in 0..8){
			ascent(j in 0..8){
			let shot = CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 2+(j/2), GetAngleToPlayer(obj)+(i*360/8),0.01, 100, SMALL_BLUE, 0);
			}
		}
	}
}

function Stage3{
wait(60);
SecondarySpirits;
SetCommonData("NextPhase", 0);
let angle = 0;
let life = 1500;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
		ascent(i in 0..16){
			let obj = SummonGhost(ObjMove_GetX(objBoss)-260+(i*36), ObjMove_GetY(objBoss)+15*sin(i*30),50,200, true,true);
			MoveFunction(obj, 0.5, 90, 0, 5);
			ObjMove_SetAngularVelocity(obj, -1.5);
			Prime(obj);
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/water.wav", 80);
			angle+=180;
			wait(6);
		}
		loop(200){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < life){
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 100);
			DeleteShotAll(TYPE_ALL, TYPE_FADE);
			SetCommonData("NextPhase", 1);
			break;}
		yield;
		}
		if(GetCommonData("NextPhase",0)==1){
		ThirdcondarySpiirts;
		SetCommonData("NextPhase", 0);
		life=0;}
	}
	task Prime(obj){
		while(ObjEnemy_GetInfo(obj, INFO_LIFE)>1){yield;}
		ascent(i in 0..8){
			ascent(j in 0..8){
			let shot = CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 2+(j/2), GetAngleToPlayer(obj)+(i*360/8),0.01, 100, SMALL_BLUE, 0);
			}
		}
	}
	task SecondarySpirits{
		while(!Obj_IsDeleted(objBoss)){
		let obj = SummonGhost(GetStgFrameWidth, ObjMove_GetY(objBoss)+50, 5,350, true,true);
		let obj2 = SummonGhost(GetStgFrameWidth-GetStgFrameWidth, ObjMove_GetY(objBoss)+50,5,350, true,true);
		FlameAuraBase(obj,[100,5,0],2);
		FlameAuraBase(obj2,[100,5,0],2);
		ObjMove_SetAngle(obj, 180);
		ObjMove_SetAngle(obj2, 0);
		ObjMove_SetSpeed(obj, 2);
		ObjMove_SetSpeed(obj2, 2);
		Shoot(obj);
		Shoot(obj2);
		Prime(obj);
		Prime(obj2);
		wait(60);
		}
		task Shoot(tar){
			while(!Obj_IsDeleted(tar)){
			let angle = 270;
				loop(3){
				let shot = CreateShotA1(ObjMove_GetX(tar), ObjMove_GetY(tar), 2, angle, SMALL_RED, 0);
				angle+=360/3
				}
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 80);
			wait(60);
			}
		}
		task Prime(obj){
		while(ObjEnemy_GetInfo(obj, INFO_LIFE)>1){yield;}
			ascent(i in 0..8){
				let shot = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, GetAngleToPlayer(obj)+(i*360/8), SMALL_RED, 0);
			}
		}
	}
	task ThirdcondarySpiirts{
		while(!Obj_IsDeleted(objBoss)){
		let obj = SummonGhost(GetStgFrameWidth-20, GetStgFrameHeight-10, 50,350, true,true);
		let obj2 = SummonGhost(GetStgFrameWidth-GetStgFrameWidth+20, GetStgFrameHeight-10,50,350, true,true);
		FlameAuraBase(obj,[0,100,5],2);
		FlameAuraBase(obj2,[0,100,5],2);
		ObjMove_SetAngle(obj, 270);
		ObjMove_SetAngle(obj2, 270);
		ObjMove_SetSpeed(obj, 2);
		ObjMove_SetSpeed(obj2, 2);
		Shoot(obj);
		Shoot(obj2);
		wait(60);
		}
		task Shoot(tar){
			while(!Obj_IsDeleted(tar)){
			let shot = CreateShotA1(ObjMove_GetX(tar), ObjMove_GetY(tar), 1, GetAngleToPlayer(tar), SMALL_GREEN, 0);
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 80);
			wait(120);
			}
		}
	}
}

task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	StartSlow(TARGET_ALL, 30);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death.wav", 100);
	loop(9){
	SpellBurstA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),"Cherry",[200, 00, 00]);
	wait(7);
	}
	StopSlow(TARGET_ALL);
	wait(1);
	SetCommonData("BossX", ObjMove_GetX(objBoss));
	SetCommonData("BossY", ObjMove_GetY(objBoss));
    Obj_Delete(objBoss);
	SetCommonData("Died",1);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 
