#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 5"]
#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);
	widescreen;
	ObjMove_SetDestAtWeight(objBoss,384/2-24,150,5,5);
	mainTask;
	end;
} 

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

@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,180,5,5);
	ObjCutin_SetSpellcardS3("&quot;A Fiery Path to the Depths Below&quot;", 200, 00, 00);
	ObjCutin_LaunchS3("KANAKO", orin, "Normal");
	SpellCall;
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	widehearts;
	attack = true;
	wait(160);
	ObjMove_SetDestAtWeight(objBoss,-285,180,5,5);
	wait(140);
	STARTTHESURIVIVAL;
}

task STARTTHESURIVIVAL{
				ZombieSeeker(GetStgFrameWidth/2+300, -20, 100000, 0, false);
			ZombieSeeker(GetStgFrameWidth/2+200, -20, 100000, 0, false);
		ZombieSeeker(GetStgFrameWidth/2+100, -20, 100000, 0, false);
	ZombieSeeker(GetStgFrameWidth/2, -20, 100000, 0, false);
		ZombieSeeker(GetStgFrameWidth/2-100, -20, 100000, 0, false);
			ZombieSeeker(GetStgFrameWidth/2-200, -20, 100000, 0, false);
				ZombieSeeker(GetStgFrameWidth/2-300, -20, 100000, 0, false);
	wait(190);
	
	let move = 0;
	let sinner = 0;
	let mult = 2.5;
	let rampup = 60;
	let unlocker = 0;
	loop{
		loop{
		ZombieSeeker(GetStgFrameWidth/2-50-150+move, -20, 100000, 0, false);
		ZombieSeeker(GetStgFrameWidth/2-50+150+move, -20, 100000, 0, false);
		if(timer<45){ZombieFighter(GetStgFrameWidth/2-50+move, -20, 50, 1000, false);}
		
		move += (20*mult)*sin(sinner);
		sinner+=50;
		mult += 0.04;
		if(rampup>2){rampup -= 0.3;}
		if(timer==30 && unlocker == 0){Stage2; unlocker = timer}
		if(timer==20 && unlocker == 0){Gogogogo; unlocker = timer}
		
		if(unlocker != timer){unlocker = 0}
		wait(truncate(rampup));
		}
	}
	
	task Stage2{
		loop{
			ascent(i in 0..2){
			ZombieBlaster(15+(i*600), -20, 100000, 0, false);
			wait(120);
			}
		}
	}
	
	task Gogogogo{
	ObjMove_SetDestAtWeight(objBoss,-285,100,5,20);
	FireTheCats;
	wait(10);
		while(!Obj_IsDeleted(objBoss)){
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth+285,100,5,20);
		wait(60*3);
		ObjMove_SetDestAtWeight(objBoss,-285,100,5,20);
		wait(60*3);
		}
	}

	task FireTheCats{
		while(!Obj_IsDeleted(objBoss)){
		CatSpread(28, ICE_BLUE, 1.8);
		if(ObjMove_GetX(objBoss)<GetStgFrameWidth && ObjMove_GetX(objBoss)>0){
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 50);}
		wait(14);
		}
	}
}


function ZombieSeeker(x,y,health,respawn, explode){
let obj = ZombieFairy(x,y,health,respawn, explode);
behaviour(obj);
deletecontrol(obj);
	task behaviour(obj){
	ObjMove_SetAngle(obj, 90);
	ObjMove_SetSpeed(obj, 1);
		while(!Obj_IsDeleted(obj)){
		let dist = GetObjectDistance(GetPlayerObjectID,obj);
			if(dist<50){break;}
		yield;
		}
		if(Obj_IsDeleted(obj)){return}
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 90);
		ObjMove_AddPatternA3(obj, 0, ObjMove_GetSpeed(obj), NO_CHANGE, -0.01, 0, 0, NO_CHANGE);
		wait(60);
		ObjMove_AddPatternA3(obj, 0, ObjMove_GetSpeed(obj), GetAngleToPlayer(obj), 0.02, 0, 8, NO_CHANGE);
		while(!Obj_IsDeleted(obj)){
		let obj2 = CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 1, GetAngleToPlayer(obj),0.02, 10, FIRE_BLUE, 10);
		ObjRender_SetBlendType(obj2, BLEND_ADD_RGB);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 90);
		wait(50);
		}
	}
}

function ZombieBlaster(x,y,health,respawn, explode){
let obj = ZombieFairy(x,y,health,respawn, explode);
behaviour(obj);
deletecontrol(obj);
FlameAuraBase(obj,[122,0,0],2);
	task behaviour(obj){
	ObjMove_SetAngle(obj, 90);
	ObjMove_SetSpeed(obj, 1);
		while(!Obj_IsDeleted(obj)){
			let obj2 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1.8, GetAngleToPlayer(obj), BUBBLE_GRAY, 10);
		ObjRender_SetBlendType(obj2, BLEND_ADD_RGB);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 90);
		wait(210);
		}
	}
}

function ZombieFighter(x,y,health,respawn, explode){
let obj = ZombieFairy(x,y,health,respawn, explode);
behaviour(obj);
deletecontrol(obj);
FlameAuraBase(obj,[255,0,0],2);
	task behaviour(obj){
	ObjMove_SetAngle(obj, 90);
	ObjMove_SetSpeed(obj, 1);
		let angle = 0;
		deathchecker(obj);
		while(!Obj_IsDeleted(obj)){
			if(ObjEnemy_GetInfo(obj, INFO_LIFE)!=1){
				loop(6){
				let obj2 = CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 0.5, GetAngleToPlayer(obj)+angle,0.005, 10, FIRE_RED, 10);
				ObjRender_SetBlendType(obj2, BLEND_ADD_RGB);
				angle += 360/6
				}
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbuller.wav", 90);
			}
		wait(180);
		}
	}
	task deathchecker(obj){
	let count = 0;
		while(!Obj_IsDeleted(obj)){
			if(ObjEnemy_GetInfo(obj, INFO_LIFE)==1 && count == 0){
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/die.wav", 90);
			shockwave(obj);
			count=1;
			}
		yield;
		}
	}
}


task deletecontrol(obj){
	wait(210);
	while(!Obj_IsDeleted(obj)){
		if(trunc(ObjRender_GetX(obj))>GetStgFrameWidth){Obj_Delete(obj);}
		if(trunc(ObjRender_GetX(obj))<0){Obj_Delete(obj);}
		if(trunc(ObjMove_GetY(obj))>GetStgFrameHeight){Obj_Delete(obj);}
		if(trunc(ObjRender_GetY(obj))<0){Obj_Delete(obj);}
		yield;
		}
}

task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
	SetCommonData("deathsound",1);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 
