//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]
#Title["Signe de l'Automne \"Feuilles tombantes\""]
#Text["The first spellcard of Suwako+"]

#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 = 60;

@Initialize
{
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	ObjEnemyBossScene_StartSpell(sceneObj);
	SetCommonData("animStyle", "idle");
	renderSpellCard1;
	BottomEnemyMarker(bossObj);
	bossSummonCircle(102, 3, 105, 0.1);
	
	spellName(bossObj, "Signe des feuilles mortes \"Air d'Automne\"");
	
	timer(TIME);
	renderBoss1;
	TFinalize;
	movement;
}

@Event
{
	alternative (GetEventType())
	case (EV_REQUEST_LIFE) {
		SetScriptResult(4500);
	}
	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 {
	ObjMove_SetDestAtFrame(bossObj, getCenterX, 120, 60);
	SetShotAutoDeleteClip(200, 200, 200, 200);
	wait(60);
	fire1;
	wait(240);
	loop {
	TMovement;
	wait(240);
	}
}

task fire1 {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
	let angleT = rand(225,270);
	let angleT2 = rand(270,315);
	let obj1 = CreateShotA2(ObjMove_GetX(bossObj)+40*cos(angleT), ObjMove_GetY(bossObj)+40*sin(angleT), 2, angleT,-0.02,0.5, rand(61,64), 5);
	let obj2 = CreateShotA2(ObjMove_GetX(bossObj)+40*cos(angleT2), ObjMove_GetY(bossObj)+40*sin(angleT2), 2, angleT2,-0.02,0.5, rand(61,64), 5);
	Changement1(obj1,angleT);
	Changement2(obj2,angleT2);
	play(19, 15);
	wait(6);
	}
}

task Changement1(aaa,bbb) {
	let angletZ = rand(30,150);
	wait(60);
	while(bbb > angletZ) {
		ObjMove_SetAngle(aaa, bbb);
		bbb -= 1;
		wait(1);
		if (bbb < 180) {
		ObjMove_SetMaxSpeed(aaa, 2);
		ObjMove_SetAcceleration(aaa, 0.02);
		}
	}
}

task Changement2(aaa,bbb) {
	let angletZ = rand(390,510);
	wait(60);
	while(bbb < angletZ) {
		ObjMove_SetAngle(aaa, bbb);
		bbb += 1;
		wait(1);
		if (bbb > 360) {
		ObjMove_SetMaxSpeed(aaa, 2);
		ObjMove_SetAcceleration(aaa, 0.02);
		}
	}
}

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);
	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));
	
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID());
	return;
}



