//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["EikaS1R"]
#Text[" "]
#Background["./background/Default_Background.txt"]
#System["./system/Default_System.txt"]
#BGM["./BGM.mp3"]

#include"./lib/All.txt"

let objEnemy;
let objPlayer;
let objScene = GetEnemyBossSceneObjectID();

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(300);
	}
        case(EV_REQUEST_TIMER)
	{
                SetScriptResult(60);
        }
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(1000000);
	}
}

@Initialize
{
        objPlayer = GetPlayerObjectID();
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	renderEikaWBaWC(objEnemy);
	SetCommonData("boss", "EIKA");
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);

	ObjMove_SetDestAtFrame(objEnemy, 192, 100, 60);
	TEnd;
	TMain;
}

@MainLoop
{
	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);

	ObjEnemy_SetIntersectionCircleToShot(objEnemy, ex, ey, 32);
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ex, ey, 24);

	yield;
}

task TMain
{
	if(GetCommonData("Hidden", 0) == 1){ TSubShot; }
	TShot;
}

task TShot
{
	loop(60){ yield; }
	SpellStart(objEnemy, "Stone Sign \"Stone Cage\"", GetCurrentScriptDirectory() ~ "./img/CutInImage.png", 620, 1050, 0.38);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	ObjEnemyBossScene_StartSpell(objScene);
	loop(60){ yield; }
	Charge(objEnemy);
	loop(30){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 10, 12);
	let num = 6;
	let a = 0;
	loop(60){ yield; }
	loop
	{
		let dis = 0;
		loop(2)
		{
			ascent(i in 0..(num + 1))
			{
				CreateStone(dis + i * 384 / (num - 1));
			}
			if(GetCommonData("Hidden", 0) == 0)
			{
				a += 30;
				ObjMove_SetDestAtFrame(objEnemy, 192 + 130 * sin(a), ObjMove_GetY(objEnemy), 30);
			}
			loop(230){ yield; }
			dis += 192 / num;
		}
	}
}

task CreateStone(x)
{
	CreateDelayLaserA1(x, 450, 270, 800, 50, 160, 30);
	loop(30){ yield; }
	SEPlay("BulletWave", 100);

	ascent(i in 0..26)
	{
		if(!Obj_IsDeleted(objEnemy))
		{
			let obj = CreateShotA1(x, 448 - 17 * i, 0, 270, 25, 20);
			DeleteShot(obj, (26 - i) * 6 + 80, true, true, "red");
			loop(3){ yield; }
		}
	}
	loop(20){ yield; }
	if(GetCommonData("Hidden", 0) == 0)
	{
		let px = GetPlayerX();
		let py = GetPlayerY();

		let angle = atan2(py - 17, px - x);

		SEPlay("Eff1", 80);
		loop(10)
		{
			CreateShotA1(x, 17, 2, angle - 18, 18, 17);
			CreateShotA1(x, 17, 2.5, angle - 9, 19, 17);
			CreateShotA1(x, 17, 3, angle, 20, 17);
			CreateShotA1(x, 17, 2.5, angle + 9, 19, 17);
			angle += 36;
		}
	}
}

task TSubShot
{
	loop(210){ yield; }
	let angle = 90;
	let num = 6;
	let a = 90;
	loop
	{	
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);

			if(!Obj_IsDeleted(objEnemy))
			{
				SEPlay("Shot1", 80);
				loop(num)
				{
					ascent(i in 0..4)
					{
						let obj = CreateShotA2(ex + cos(angle) * 130, ey + sin(angle) * 20, 1.5, angle + i * 10 + 5, 0.01, 3, 20 - i, 10 + i * 4);
						let obj1 = CreateShotA2(ex + cos(angle) * 130, ey + sin(angle) * 20, 1.5, angle - i * 10 + 5, 0.01, 3, 20 - i, 10 + i * 4);
						let obj2 = CreateShotA2(ex + cos(angle) * 130, ey + sin(angle) * 20, 2.5, angle + i * 10, 0.01, 3, 20 - i, 10 + i * 4);
						let obj3 = CreateShotA2(ex + cos(angle) * 130, ey + sin(angle) * 20, 2.5, angle - i * 10, 0.01, 3, 20 - i, 10 + i * 4);
					}
					angle += 360 / num;
				}
				angle += 30;
			}
			if(num < 9){ num++; }
			else if(num == 9){ num = 6; }
			ObjMove_SetDestAtFrame(objEnemy, ObjMove_GetX(objEnemy) + 20 * sin(a), ObjMove_GetY(objEnemy), 30);
			a += 30;
			loop(30){ yield; }
	}
}

task TEnd
{
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0){ yield; }
	Obj_Delete(objEnemy);
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) != 0)
	{
		NotifyEventAll(EV_USER_SYSTEM, 0);
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	loop(10)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}
	CloseScript(GetOwnScriptID());
}