//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]
#Title["Nonspell 1"]
#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);
	
	DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	SetCommonData("animStyle", "idle");
	
	timer(TIME);
	renderBoss3;
	TFinalize;
	movement;
}

@Event
{
	alternative (GetEventType())
	case (EV_REQUEST_LIFE) {
		SetScriptResult(3500);
	}
	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, 90);
	wait(90);
	while(!Obj_IsDeleted(bossObj)) {
		fire1;
		TMovement;
		wait(420);
	}
}

task fire1 {
	let dir = angleToPlayer(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
	let obj1 = CreateShotA2(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 2, dir,-0.005,0, 824, 30);
	ObjShot_SetSpellResist(obj1, true);
	Concentration(obj1);
	wait(30);
	play(2, 25);
}

task Concentration(aaa) {
	wait(60);
	loop(50) {
	ObjMove_SetAngle(aaa, rand(0,360));
	wait(1);
	}
	play(19, 30);
	Explosion(aaa);
}

task Explosion(aaa) {
	let temps = 160;
	let temps2 = 80;
	let angleT = rand(0,360);
	loop(8) {
		let laser1 = CreateCurveLaserA1(ObjMove_GetX(aaa), ObjMove_GetY(aaa), 6, angleT, 100, 15, 287, 5);
		ObjMove_SetAngularVelocity(laser1, 8);
		ObjMove_AddPatternA2(laser1, 40, 1, NO_CHANGE, 0, -4, 0);
		ObjMove_AddPatternA2(laser1, 120, 1, NO_CHANGE, 0, 2, 0);
		ObjMove_AddPatternA2(laser1, temps2, 1, NO_CHANGE, 0, -9, 0);
		ObjMove_AddPatternA2(laser1, temps, 1, NO_CHANGE, 0, 0, 0);
		angleT += 360/8;
		attendre(laser1,temps);
		profiteur(laser1,angleT);
	}
	Obj_Delete(aaa);
}

task profiteur(aaa,bbb) {
	loop(75) {
	let obj1 = CreateShotA1(ObjMove_GetX(aaa), ObjMove_GetY(aaa), 2.5, bbb, 23, 0);
	bbb += 15;
	wait(2);
	}
	loop(75) {
	let obj1 = CreateShotA1(ObjMove_GetX(aaa), ObjMove_GetY(aaa), 1.5, bbb, 24, 0);
	bbb += 15;
	wait(2);
	}
}

task attendre(aaa,bbb) {
	wait(bbb);
	while(!Obj_IsDeleted(aaa)) {
	ObjMove_SetAngularVelocity(aaa, -4);
	wait(20);
	ObjMove_SetAngularVelocity(aaa, 4);
	wait(20);
	}
}

task TMovement
{
	let angle;
	let radius;
	if (cos(bossAngleToPlayer) > 0) {
		angle = (randInt(-90, 90) + randInt(-90, 90)) / 2;
	} else {
		angle = (randInt(90, 270) + randInt(90, 270)) / 2;
	}
	if (absolute(distancePoints(GetPlayerX, GetPlayerY, ObjMove_GetX(bossObj), ObjMove_GetY(bossObj)) * cos(bossAngleToPlayer)) > GetStgFrameHeight / 4) {
		radius = (randInt(100, 150) + randInt(100, 150)) / 2;
	} else {
		radius = (randInt(0, 100) + randInt(0, 100)) / 2;
	}
	let x = radius * cos(angle) + ObjMove_GetX(bossObj);
	let y = radius * sin(angle) + ObjMove_GetY(bossObj);
	ObjMove_SetDestAtSpeed(bossObj, x, max(min(y, 100), 30), 1.5);
	wait(145);
}


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);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID());
	return;
}



