//#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);
	ObjMove_SetX(bossObj, GetCommonData("bossX", -32));
	ObjMove_SetY(bossObj, GetCommonData("bossY", -32));
	ObjEnemy_Regist(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	SetCommonData("animStyle", "idle");
	
	ObjMove_SetPosition(bossObj,getCenterX-50, getCenterY-300);
	
	timer(TIME);
	renderBoss22;
	TFinalize;
	movement;
}

@Event
{
	alternative (GetEventType())
	case (EV_REQUEST_LIFE) {
		SetScriptResult(2500);
	}
	case (EV_REQUEST_TIMER) {
		SetScriptResult(TIME);
	}
}

@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, 120, 60);
	wait(120);
	TMovement;
}

task fire2 {
	let angleT = rand(0,360);
	loop(2) {
	loop(45) {
	let obj1 = CreateShotA2(ObjMove_GetX(bossObj)+30*cos(angleT+90),ObjMove_GetY(bossObj)+30*sin(angleT+90), 1, angleT,0.005,2, 65, 10);
	let obj2 = CreateShotA2(ObjMove_GetX(bossObj)+30*cos(angleT),ObjMove_GetY(bossObj)+30*sin(angleT+180), 1, angleT,0.005,2, 65, 10);
	let obj3 = CreateShotA2(ObjMove_GetX(bossObj)+30*cos(angleT+180),ObjMove_GetY(bossObj)+30*sin(angleT), 1, angleT,0.005,2, 65, 10);
	angleT += 360/45;
	}
	play(19, 30);
	wait(20);
	}
}

task fire1 {
	let dir = angleToPlayer(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
	loop(8) {
	let obj1 = CreateShotA1(ObjMove_GetX(bossObj)+50*cos(dir),ObjMove_GetY(bossObj)+50*sin(dir), 1.5, dir, 785, 20);
	pique(obj1);
	dir += 360/8;
	}
}

task pique(aaa) {
	let dir = angleToPlayer(ObjMove_GetX(aaa), ObjMove_GetY(aaa));
	loop(16) {
	let obj1 = CreateShotA1(ObjMove_GetX(aaa)+10*cos(dir),ObjMove_GetY(aaa)+10*sin(dir), 0, dir, 149, 20);
	bouge(obj1,aaa,dir);
	dir += 360/16;
	}
}

task bouge(aaa,bbb,ccc) {
	let angleT = 0;
	while(!Obj_IsDeleted(bbb)) {
		let angleT = 10;
		while(angleT < 30 && !Obj_IsDeleted(bbb)) {
		ObjMove_SetPosition(aaa, ObjMove_GetX(bbb)+angleT*cos(ccc),ObjMove_GetY(bbb)+angleT*sin(ccc));
		ObjMove_SetAngle(aaa, ccc);
		wait(1);
		angleT += 1;
		ccc += 1;
		}
		while(angleT > 10 && !Obj_IsDeleted(bbb)) {
		ObjMove_SetPosition(aaa, ObjMove_GetX(bbb)+angleT*cos(ccc),ObjMove_GetY(bbb)+angleT*sin(ccc));
		ObjMove_SetAngle(aaa, ccc);
		wait(1);
		angleT -= 1;
		ccc +=1;
		}
	}
	Obj_Delete(aaa);
}

task TMovement
{
	let angleT = 0;
	while(!Obj_IsDeleted(bossObj)) {
		ObjMove_SetPosition(bossObj, getCenterX+rand(-50,50), 120+rand(-50,50));
		fire1;
		fire2;
		wait(120);
		wait(120);
		ObjMove_SetPosition(bossObj, getCenterX+rand(-50,50), 120+rand(-50,50));
		fire1;
		fire2;
		wait(120);
		wait(120);
	}
}


task TFinalize
{
	while (ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {yield;}
	wait(5);
	SetCommonData("bossX", ObjMove_GetX(bossObj));
	SetCommonData("bossY", ObjMove_GetY(bossObj));
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetPlayerLife(GetPlayerLife()+1);
	play(21, 75);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID());
	return;
}



