//#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");
	renderSpellCard2;
	BottomEnemyMarker(bossObj);
	bossSummonCircle(102, 3, 105, 0.1);
	
	spellName(bossObj, "Signe du Feu \"Brulure\"");
	
	timer(TIME);
	renderBoss2;
	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 {
	wait(60);
	while(!Obj_IsDeleted(bossObj)) {
	fire1;
	wait(180);
	}
}

task fire1 {
	let dir = angleToPlayer(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
	let obj1 = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 3, dir, 356, 0);
	ObjShot_SetSpellResist(obj1, true);
	play(2, 30);
	colonne(obj1);
	wait(60);
	TMovement;
}

task fire2(aaa,bbb) {
	loop(25) {
		let obj1 = CreateShotA2(ObjMove_GetX(aaa), ObjMove_GetY(aaa), 5, bbb+rand(0,30),rand(-0.03,-0.06),0.1, rand(34,36), 0);
		let obj2 = CreateShotA2(ObjMove_GetX(aaa), ObjMove_GetY(aaa), 5, bbb+rand(-30,0),rand(-0.03,-0.06),0.1, rand(34,36), 0);
		tourne2(obj1,bbb);
		tourne1(obj2,bbb);
		wait(1);
	}
}

task tourne1(aaa,bbb) {
	let tour = 0;
	wait(100);
	let angleT = ObjMove_GetAngle(aaa);
	while(tour<160) {
		ObjMove_SetAngle(aaa, angleT);
		angleT -= 2;
		tour += 2;
		wait(1);
	}
	ObjMove_SetMaxSpeed(aaa, 3);
	ObjMove_SetAcceleration(aaa, +0.05);
	ObjMove_SetAngle(aaa, bbb+180);
}

task tourne2(aaa,bbb) {
	let tour = 0;
	wait(100);
	let angleT = ObjMove_GetAngle(aaa);
	while(tour<160) {
		ObjMove_SetAngle(aaa, angleT);
		angleT += 2;
		tour += 2;
		wait(1);
	}
	ObjMove_SetMaxSpeed(aaa, 3);
	ObjMove_SetAcceleration(aaa, +0.05);
	ObjMove_SetAngle(aaa, bbb+180);
}

task colonne(aaa) {
	let currentangle;
	let NaPaToucher = 0;
	while(NaPaToucher<1){
		currentangle = ObjMove_GetAngle(aaa);
		if(ObjMove_GetX(aaa)>=GetStgFrameWidth){
		fire2(aaa,180);
		NaPaToucher += 1;
		play(19, 30);
		yield;}
		if(ObjMove_GetX(aaa)<=0){
		fire2(aaa,0);
		NaPaToucher += 1;
		play(19, 30);
		yield;}
		if(ObjMove_GetY(aaa)<=0){
		fire2(aaa,90);
		NaPaToucher += 1;
		play(19, 30);
		yield;}
		if(ObjMove_GetY(aaa)>=GetStgFrameHeight){
		fire2(aaa,270);
		NaPaToucher += 1;
		play(19, 30);
		yield;}
	yield;}
}

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;
}



