#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Midboss Spell"]
#Text["Wooooooop"]

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


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	ObjEnemy_SetDamageRate(objBoss,0,0);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/death2.wav", 90);
	ObjMove_SetDestAtSpeed(objBoss,384/2,200,2);
	
	mainTask;	
} 

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

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 { 
	rendercarbink(objBoss);
	BottomEnemyMarker(objBoss, objBoss);
	end;
	action;
	BossShotSound(objBoss, 75, 300);
} 

task SpellCall {
	bspell = true;
	TCallBg;
}
let CarbinkCut = GetCurrentScriptDirectory ~ "Functions/img/Cutin/Carbink.png";
task action{
wait(60);
ObjCutin_SetSpellcardS3("Power Gem &quot;Diamond in the Rough&quot;", 75, 200, 255);
ObjCutin_LaunchS3("KANAKO", CarbinkCut,"Normal");
SpellCall;
let objScene = GetEnemyBossSceneObjectID();
ObjEnemyBossScene_StartSpell(objScene);
wait(120);
Fire;
wait(180);
ObjEnemy_SetDamageRate(objBoss,50,40);
}

task Fire{
let angle = 45;
let delay = 80;
while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) {
loop(4){
let orb = CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3, angle, -0.05, 0, ICEBREAKER_CYAN,10);
Rotate(orb);
ObjShot_SetSpellResist(orb, 1);
angle += 90;
Fire2(orb,delay);
delay += 90;
}
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/barrage.wav", 65);
delay = 80;
wait(90*6);
}
}

task Rotate(tar){
let angle = 0;
loop{
ObjRender_SetAngleXYZ(tar, 0,0,angle);
angle+=2;
yield;
}
}

task Fire2(tar,del) {
wait(del);
ObjMove_SetDestAtWeight(objBoss,GetPlayerX+rand(-15,15),200+rand(-30,20),10,2);
while(!Obj_IsDeleted(tar)){
let wat = 0;
let angle = 90;
ascent(i in 0..12){
	CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),2-(i/12), angle+(i*8),BALL_S_BLUE,10);
	CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),2-(i/12), angle-(i*8),BALL_S_BLUE,10);
	let obj = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),2-(i/12), angle+(i*8)+180,BALL_S_BLUE,10);
	let obj2 = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),2-(i/12), angle-(i*8)+180,BALL_S_BLUE,10);
	Ceiling(obj);Ceiling(obj2);
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/labullet.wav", 65);
}
wait(90);
ObjMove_AddPatternA4(tar, 0, 0, GetAngleToPlayer(tar), 0.03, 0, 5, 0, NO_CHANGE);
SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/barrage2.wav", 65);
while(!Obj_IsDeleted(tar)){
let trail = CreateShotA1(ObjMove_GetX(tar),ObjMove_GetY(tar),0,0,BALL_S_AZURE,15);
ObjShot_SetDeleteFrame(trail, 0);
yield;
}
}
}

task Ceiling(tar){
while(!Obj_IsDeleted(tar)){		
	if(trunc(ObjMove_GetY(tar))<0)
	{
	ascent(i in 0..2){
		CreateShotA1(ObjMove_GetX(tar)+(i*4),ObjMove_GetY(tar)-(i*3),2, 90,ICE_AZURE,10);
		CreateShotA1(ObjMove_GetX(tar)-(i*4),ObjMove_GetY(tar)-(i*3),2, 90,ICE_AZURE,10);
	}
	SE_Play(GetCurrentScriptDirectory ~ "Functions/sfx/spirit.wav", 65);
	Obj_Delete(tar);
	}
yield;
}
}

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

