//#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 de la Chaleur \"Brasier\"");
	
	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 {
	ObjMove_SetDestAtFrame(bossObj, getCenterX, getCenterY-40, 60);
	SetShotAutoDeleteClip(300, 300, 300, 300);
	pouvoirs(1);
	wait(60);
	fire1;
	wait(160);
	while(!Obj_IsDeleted(bossObj)) {
	fire2;
	wait(360);
	}
}

task fire1 {
	let dir = angleToPlayer(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
	let angleT = rand(0,360);
	loop {
		loop(20) {
			let obj1 = CreateShotA1(ObjMove_GetX(bossObj)+20*cos(angleT), ObjMove_GetY(bossObj)+20*sin(angleT), 2, angleT, rand(34,36), 0);
			ObjMove_SetAngularVelocity(obj1, 0.6);
			destroy(obj1);
			angleT += 360/20;
			}
			play(19, 30);
			angleT +=3;
			wait(60);
	}
	
}

task destroy(aaa) {
	wait(240);
	Obj_Delete(aaa);
}

task fire2 {
	let dir = angleToPlayer(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
	let obj1 = CreateShotA1(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj), 1, dir, 823, 45);
	ObjShot_SetSpellResist(obj1, true);
	let angleT = 0;
	loop(20) {
	tornade(obj1,angleT);
	angleT += 360/20;
	}
	wait(45);
	play(2, 30);
}

task tornade(aaa,bbb) {
	let angleT1 = bbb;
	let angleT2 = bbb;
	let angleT3 = bbb;
	let angleT4 = bbb;
	let angleT5 = bbb;
	let one = -5;
	let two = -10;
	let three = -15;
	let four = -20;
	let five = -25;
	let obj2 = CreateShotA1(ObjMove_GetX(aaa)+one*cos(angleT1), ObjMove_GetY(aaa)+one*sin(angleT1), 0, angleT1, 832, 45);
	let obj3 = CreateShotA1(ObjMove_GetX(aaa)+two*cos(angleT2), ObjMove_GetY(aaa)+two*sin(angleT2), 0, angleT2, 832, 45);
	let obj4 = CreateShotA1(ObjMove_GetX(aaa)+three*cos(angleT3), ObjMove_GetY(aaa)+three*sin(angleT3), 0, angleT3, 832, 45);
	let obj5 = CreateShotA1(ObjMove_GetX(aaa)+four*cos(angleT4), ObjMove_GetY(aaa)+four*sin(angleT4), 0, angleT4, 832, 45);
	let obj6 = CreateShotA1(ObjMove_GetX(aaa)+five*cos(angleT5), ObjMove_GetY(aaa)+five*sin(angleT5), 0, angleT5, 832, 45);
	while(!Obj_IsDeleted(aaa)) {
		angleT1 += 2;
		angleT2 += 3;
		angleT3 += 4;
		angleT4 += 5;
		angleT5 += 6;
		one += 0.3;
		two += 0.25;
		three += 0.2;
		four += 0.15;
		five += 0.1;
		ObjMove_SetDestAtFrame(obj2, ObjMove_GetX(aaa)+one*cos(angleT1), ObjMove_GetY(aaa)+one*sin(angleT1), 1);
		ObjMove_SetDestAtFrame(obj3, ObjMove_GetX(aaa)+two*cos(angleT2), ObjMove_GetY(aaa)+two*sin(angleT2), 1);
		ObjMove_SetDestAtFrame(obj4, ObjMove_GetX(aaa)+three*cos(angleT3), ObjMove_GetY(aaa)+three*sin(angleT3), 1);
		ObjMove_SetDestAtFrame(obj5, ObjMove_GetX(aaa)+four*cos(angleT4), ObjMove_GetY(aaa)+four*sin(angleT4), 1);
		ObjMove_SetDestAtFrame(obj6, ObjMove_GetX(aaa)+five*cos(angleT5), ObjMove_GetY(aaa)+five*sin(angleT5), 1);
		wait(1);
		}
		Obj_Delete(obj2);
		Obj_Delete(obj3);
		Obj_Delete(obj4);
		Obj_Delete(obj5);
		Obj_Delete(obj6);
}

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



