//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["EikaN1R"]
#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(60){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 80, 80);

	let angle = 0;
	let dir = 1;
	loop
	{
		Obj_SetValue(objEnemy,"cast",1);
		loop(72)
		{
			SEPlay("Shot1", 80);
			loop(4)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					let ex = ObjMove_GetX(objEnemy);
					let ey = ObjMove_GetY(objEnemy);
					ascent(i in 0..(GetCommonData("Hidden", 0) * 3 + 4))
					{
						let obj = CreateShotA2(ex + cos(angle) * 30, ey + sin(angle) * 30 - 6 * i * dir, 3.9, angle, -0.3, 2.4, 54, 10);
						ObjRender_SetAngleZ(obj, -ObjMove_GetAngle(obj));
					}
					angle += 90;
				}
			}
			angle += 11.75 * dir;
			loop(2){ yield; }
		}
		Obj_SetValue(objEnemy,"cast",0);
		dir *= -1;
		loop(60){ yield; }
	}
}

task TSubShot
{
	loop(200){ yield; }
	let a = 0;
	if(GetCommonData("Hidden", 0) == 0)
	{
			loop(20 + 40 * cos(a)){ yield; }
			a += 30;
			ObjMove_SetDestAtFrame(objEnemy, 192 + 150 * sin(a), ObjMove_GetY(objEnemy), 60);
	}
	else if(GetCommonData("Hidden", 0) == 1)
	{
		loop
		{
			loop(2)
			{
				let ex = ObjMove_GetX(objEnemy);
				let ey = ObjMove_GetY(objEnemy);

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

				let ang = atan2(py - ey, px - ex);

				SEPlay("Shot2", 80);
				loop(15)
				{
					if(!Obj_IsDeleted(objEnemy))
					{
						ascent(i in 0..3)
						{
							let obj = CreateShotA2(ex, ey, 3, ang, -0.1, 2.5 - 0.4 * i, 225, 10 + i);
							ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
						}
						ang += 24;
					}
				}
				loop(30){ yield; }
			}
			ObjMove_SetDestAtFrame(objEnemy, ObjMove_GetX(objEnemy) + 40 * sin(a), ObjMove_GetY(objEnemy), 60);
			a += 30;
			loop(120){ yield; }
		}
	}
}