//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["EikaN3R"]
#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(2000);
	}
        case(EV_REQUEST_TIMER)
	{
                SetScriptResult(30);
        }
}

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

	ObjEnemy_SetDamageRate(objEnemy, 30, 30);
	ObjMove_SetDestAtFrame(objEnemy, 192, 100, 60);
	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);

	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0)
	{
		Obj_Delete(objEnemy);
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		CloseScript(GetOwnScriptID());
        }
	yield;
}

task TShot
{
	loop(60){ yield; }
	Charge(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	loop(90){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 80, 80);
	let dir = 1;
	loop
	{
		let angle = 90;
		loop(90)
		{
			SEPlay("Shot1", 80);
			loop(7)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					let ex = ObjMove_GetX(objEnemy);
					let ey = ObjMove_GetY(objEnemy);
					let obj = CreateShotA2(ex + cos(angle) * 30, ey + sin(angle) * 30, 3.9, angle + 180 / 7, -0.02, 1.5, 54, 10);
					if(GetCommonData("Hidden", 0) == 1){ let obj2 = CreateShotA2(ex + cos(angle) * 30, ey + sin(angle) * 30, 1, -angle, 0.01, 3, 54, 10); }
					let obj1 = CreateShotA2(ex + cos(angle) * 30, ey + sin(angle) * 30, 1, angle, 0.01, 3, 54, 10);
					angle += 360 / 7;
				}
			}
			angle += 5.75 * dir;
			dir *= 1;
			loop(5){ yield; }
		}
		loop(60){ yield; }
	}
}

task TSubShot
{
	loop(200){ yield; }
	let a = 90;
	loop
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);

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

		let ang = atan2(py - ey, px - ex);
		loop(2 + GetCommonData("Hidden", 0))
		{
			SEPlay("Shot2", 80);
			loop(20 + GetCommonData("Hidden", 0) * 4)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					ascent(i in 0..3)
					{
						let obj = CreateShotA2(ex, ey, 3, ang, -0.1, 2.5 - 0.5 * i, 229, 10 + i);
					}
					ang += 360 / (20 + GetCommonData("Hidden", 0) * 4);
				}
			}
			loop(50){ yield; }
			ang += 360 / (20 + GetCommonData("Hidden", 0) * 4) / (GetCommonData("Hidden", 0) + 2);
		}
		ObjMove_SetDestAtFrame(objEnemy, ObjMove_GetX(objEnemy) + 40 * sin(a), ObjMove_GetY(objEnemy), 60);
		a += 30;
		loop(120){ yield; }
	}
}