//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["EikaS2R"]
#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(310);
	}
        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;
	TShot;
	TSubShot;
}

@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 TShot
{
	loop(60){ yield; }
	SpellStart(objEnemy, "Colorful Stone \"Stone Wave\"", 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 - GetCommonData("Hidden", 0);
	let dir = 1;
	let angle = 90;
	loop(60){ yield; }
	loop
	{
		let dis = 0;
		loop(2)
		{
			ascent(i in 0..(num + 1))
			{
				CreateStone(dis + i * 384 / (num - 1), dir, angle);
			}
			loop(200){ yield; }
			dir *= -1;
			dis += 192 / num
		}
		angle *= -1;
	}
}

task CreateStone(x, dir, angle)
{
	let graphic = 9;
	CreateDelayLaserA1(x, 450, 270, 800, 30, 160, 30);
	loop(30){ yield; }
	SEPlay("BulletWave", 100);
	ascent(i in 0..(26 + GetCommonData("Hidden", 0) * 13))
	{
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0)
		{
			let obj = CreateShotA1(x, 448 - (448 / (24 + GetCommonData("Hidden", 0) * 12)) * i, 0, angle, graphic, 20);
			ObjMove_AddPatternA2(obj, (26 + GetCommonData("Hidden", 0) * 13 - i) * (3 - GetCommonData("Hidden", 0)) + 25, 1.5, NO_CHANGE, 0.01, 0, 1.5);
			loop(3 - GetCommonData("Hidden", 0)){ yield; }
			if(graphic < 15){ graphic++; }
			else{ graphic = 9; }			
			angle += (540 + 540 * GetCommonData("Hidden", 0)) / (24 + GetCommonData("Hidden", 0) * 12) * dir;
		}
	}
}

task TSubShot
{
	if(GetCommonData("Hidden", 0) == 0)
	{
		loop(120){ yield; }
		let a = 0;
		loop
		{
			loop(30 + cos(a) * 60){ yield; }
			ObjMove_SetDestAtFrame(objEnemy, 192 + 100 * sin(a), ObjMove_GetY(objEnemy), 60);
			a += 30;
		}
	}
	else if(GetCommonData("Hidden", 0) == 1)
	{
		loop(120){ yield; }
		let a = 90;
		let dir = 1;
		loop
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);

			let px = GetPlayerX();
			let py = GetPlayerY();

			let angle = atan2(py - ey, px - ex);
			SEPlay("Shot2", 80);
			if(!Obj_IsDeleted(objEnemy))
			{
				loop(5)
				{
					ascent(i in 0..6)
					{
						ascent(k in 0..4)
						{
							let obj = CreateShotA1(ex + cos(angle) * 10, ey + sin(angle) * 10, 2, angle + 40 * i * dir, 65 + i, 10 + k * 5 + i * 5);
        						ObjShot_SetAutoDelete(obj, false);
							ObjRender_SetAngleZ(obj, 90);
							ObjMove_AddPatternA2(obj, 60 - i, NO_CHANGE, NO_CHANGE, -2 / (40 - k * 6), 0, 0);
							ObjMove_AddPatternA2(obj, 90 + 3 * k, 2, angle + (150 + i * 7) * dir, 0, 0, 1.5);
						}
					}
					angle += 72;
				}
			}
			loop(30){ yield; }
			ObjMove_SetDestAtFrame(objEnemy, ObjMove_GetX(objEnemy) + 40 * sin(a), ObjMove_GetY(objEnemy), 60);
			a += 30;
			dir *= -1;
			loop(90){ 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(20)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}
	CloseScript(GetOwnScriptID());
}