//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]
#Title["MidBoss"]
#Text["First Nonspell of Mirai"]

#include "./img/All_Star_Shots/AllStarShot_Const.txt"
#include "./Sounds.txt"
#include "./Functions.txt"
#include "./Animation.txt"
#include "./Effects.txt"

let bossObj;
let sceneObj = GetEnemyBossSceneObjectID;

let TIME = 50;

@Initialize
{
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	renderSpellCardMidBoss2;
	
	DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	ObjEnemyBossScene_StartSpell(sceneObj);
	SetCommonData("animStyle", "idle");
	
	spellName(bossObj, "Signe du Neant \"Trou Noir\"");
	
	timer(TIME);
	renderBoss22;
	TFinalize;
	movement;
}

@Event
{
	alternative (GetEventType())
	case (EV_REQUEST_LIFE) {
		SetScriptResult(3500);
	}
	case (EV_REQUEST_TIMER) {
		SetScriptResult(TIME);
	}
	case (EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(50000000);
	}
}

@MainLoop
{

	ObjEnemy_SetIntersectionCircleToShot(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 32);
	SetCommonData("bossX", ObjMove_GetX(bossObj));
	SetCommonData("bossY", ObjMove_GetY(bossObj));
	yield;
}


task movement {
	bossSummonCircle(102, 3, 105, 0.1);
	wait(60);
	ObjMove_SetDestAtFrame(bossObj, getCenterX, getCenterY, 60);
	wait(120);
	TrouNoir;
	SetShotAutoDeleteClip(500, 500, 500, 500);
}

task TrouNoir {
	let dir = angleToPlayer(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
	let obj1 = CreateShotA1(ObjMove_GetX(bossObj),ObjMove_GetY(bossObj), 0.5, dir, 830, 60);
	ObjShot_SetSpellResist(obj1, true);
	wait(60);
	play(2, 30);
	bouge(obj1);
	tourbillon(obj1);
	attack1(obj1);
}

task bouge(aaa) {
	while(!Obj_IsDeleted(bossObj)) {
	ObjMove_SetAngle(aaa, angleToPlayer(ObjMove_GetX(aaa), ObjMove_GetY(aaa)));
	wait(1);
	}
}

task tourbillon(aaa) {
	let angleT = angleToPlayer(ObjMove_GetX(aaa), ObjMove_GetY(aaa));
	while(!Obj_IsDeleted(bossObj)) {
	loop(5) {
	let obj1 = CreateShotA1(ObjMove_GetX(aaa)+400*cos(angleT), ObjMove_GetY(aaa)+400*sin(angleT), 2, 180+angleT, 537, 5);
	let obj2 = CreateShotA1(ObjMove_GetX(aaa)+400*cos(angleT+24), ObjMove_GetY(aaa)+400*sin(angleT+24), 2, 180+angleT+24, 666, 5);
	let obj3 = CreateShotA1(ObjMove_GetX(aaa)+400*cos(angleT+48), ObjMove_GetY(aaa)+400*sin(angleT+48), 2, 180+angleT+48, 516, 5);
	tadaa(obj1,aaa);
	tadaa(obj2,aaa);
	tadaa(obj3,aaa);
	angleT += 360/5;
	}
	play(19, 15);
	angleT += 10;
	wait(15);
	}
}

task tadaa(bbb,ccc) {
	while(!Obj_IsDeleted(bbb)) {
	let test = atan2(ObjMove_GetY(ccc) - ObjMove_GetY(bbb), ObjMove_GetX(ccc) - ObjMove_GetX(bbb));
	ObjMove_SetAngle(bbb, test);
	if (IsIntersected_Obj_Obj(bbb,ccc)) {
	Obj_Delete(bbb);
	}
	wait(1);
	}
}

task attack1(aaa){
let suction = 1;
while(!Obj_IsDeleted(bossObj)) {
wait(1);
let angleT = atan2(GetPlayerY-ObjMove_GetY(aaa),GetPlayerX-ObjMove_GetX(aaa));
ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX-suction*cos(angleT), GetPlayerY-suction*sin(angleT));
}
}

task TFinalize
{
	while (ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {yield;}
	wait(5);
	if (ObjEnemyBossScene_GetInfo(sceneObj, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(sceneObj, INFO_PLAYER_SPELL_COUNT) == 0) {
		AddScore(ObjEnemyBossScene_GetInfo(sceneObj, INFO_SPELL_SCORE));
	}
	loop(20) {
		let radius = (randInt(0, 100) + randInt(0, 100)) / 2;
		let angle = rand(0, 360);
		NotifyEvent(GetCommonData("scriptID", GetOwnScriptID), EV_USER + 1, [ITEM_POINT_S, 1000000, ObjMove_GetX(bossObj) + radius * cos(angle), ObjMove_GetY(bossObj) + radius * sin(angle)]);
	}	
	NotifyEvent(GetCommonData("scriptID", GetOwnScriptID), EV_USER + 3, [FROG_OVERLAY]);
	SetCommonData("bossX", ObjMove_GetX(bossObj));
	SetCommonData("bossY", ObjMove_GetY(bossObj));
	
	SetShotAutoDeleteClip(64, 64, 64, 64);
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetPlayerLife(GetPlayerLife()+1);
	play(21, 75);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID());
	return;
}



