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

let objBoss;
let BossX;
let BossY;

#include "./Functions/Index.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	renderorin(objBoss);
	BossShotSound(objBoss, 100, 600);
	BottomEnemyMarker(objBoss, objBoss);
	
	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(90);
	}
	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 SpellCall {
	bspell = true;
	TCallBg;
}

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

task mainTask { 
	wait(30);
	ObjCutin_SetSpellcardS3("Zombie Fairies &quot;Zerg Rush Formation&quot;", 200, 00, 00);
	ObjCutin_LaunchS3("KANAKO", orin, "Normal");
	SpellCall;
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	
wait(130);
let reverse = 1;

	loop{
	let angle = rand(0,360);
	attack = true;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/chargelong.wav", 90);
	wait(100);
	
	loop(7){
	let obj = ZombieFairy(ObjMove_GetX(objBoss)+24,ObjMove_GetY(objBoss)-35,200,600, true);
	ObjMove_SetDestAtWeight(obj,ObjMove_GetX(objBoss)+120*cos(angle)+24,ObjMove_GetY(objBoss)+120*sin(angle)-35,10,5);
	behavior(obj);
	angle += 360/7;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
	wait(5);
	}
	
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/chargelong.wav", 90);
	wait(90);
	angle += 360/16;
	
	loop(7){
	let obj = ZombieFairy(ObjMove_GetX(objBoss)+24,ObjMove_GetY(objBoss)-35,200,600, true);
	ObjMove_SetDestAtWeight(obj,ObjMove_GetX(objBoss)+160*cos(angle)+24,ObjMove_GetY(objBoss)+160*sin(angle)-35,10,5);
	behavior(obj);
	angle += 360/7;
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
	wait(5);
	}
	
	wait(60);
	attack = false;
	move;
	wait(1000);
	}
}

task move{
	loop(4){
	ObjMove_SetDestAtWeight(objBoss,GetPlayerX+rand(-25,25),150+rand(-25,25),15,5);
	wait(245);
	}
}

task behavior(obj){
	wait(60);
	death(obj);
	while(!Obj_IsDeleted(obj)){
	if(ObjEnemy_GetInfo(obj, INFO_LIFE)!=1){
	wait(120);
	let X = ObjMove_GetX(obj);
	let Y = ObjMove_GetY(obj);
	ObjMove_SetDestAtWeight(obj,X+120*cos(GetAngleToPlayer(obj)),Y+120*sin(GetAngleToPlayer(obj))-35,5,1);
		while(ObjMove_GetSpeed(obj)!=0){
		if(ObjEnemy_GetInfo(obj, INFO_LIFE)==1){
		ObjMove_SetDestAtWeight(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),0,0);
		}
		yield;
	}
		if(ObjEnemy_GetInfo(obj, INFO_LIFE)!=1){
		let shot = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,GetAngleToPlayer(obj),ARROWHEAD_GRAY,5);
		SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/smbullet.wav", 60);
		ObjRender_SetBlendType(shot, BLEND_ADD_ARGB);
		ObjRender_SetScaleXYZ(shot, 0.7,1.8,1);
		}
	}
	yield;
	}
		task death(obj){
		let dead = 0;
		while(!Obj_IsDeleted(obj)){
			if(ObjEnemy_GetInfo(obj, INFO_LIFE)==1 && dead == 0){
			loop(6){
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(1,3),rand(0,360),PELLET_GRAY,5);
			}
			dead = 1;
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 70);
			SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/die.wav", 50);
			}
			if(ObjEnemy_GetInfo(obj, INFO_LIFE)!=1){dead = 0;}
			yield;
		}
		}
	while(!Obj_IsDeleted(obj)){
	}
}

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());
} 
