//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["N2K"]
#Text[" "]
#Background["./background/Default_Background.txt"]
#System["./system/Default_System.txt"]
#BGM["./BGM1.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);
	renderKoishiSA(objEnemy);
	SetCommonData("boss", "KOISHI");

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

	loop
	{
		Obj_SetValue(objEnemy,"cast",1);
		let angle = rand(1, 360);
		let angle1 = -angle;
		loop(20)
		{
			SEPlay("Shot1", 80);
			loop(8)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					let ex = ObjMove_GetX(objEnemy);
					let ey = ObjMove_GetY(objEnemy);
					ascent(i in 0..5)
					{
						CreateShotA1(ex, ey, 3 + i * 0.25, angle + i * 2, 9, 10 + i * 3);
					}
					angle += 360 / 8;
				}
			}
			angle += 16;
			loop(10){ yield; }
		}
		Obj_SetValue(objEnemy,"cast",0);
		loop(60){ yield; }
	}
}

task TSubShot
{
	loop(120){ yield; }
	let ang = 0;
	loop
	{
		loop(2)
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);

			SEPlay("Shot2", 80);
			loop(18)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					ascent(i in 0..3)
					{
						let obj = CreateShotA2(ex, ey, 3 - i * 0.3, ang, -0.1, 2.5 - 0.5 * i, 266, 10 + i);
					}
					ang += 20;
				}
			}
			loop(80){ yield; }
			ang += 10;
		}
		loop(40){ yield; }
		ang += 30;
		ObjMove_SetDestAtFrame(objEnemy, 192 + sin(ang) * 90, 100 + sin(ang) * 20, 60);
		loop(60){ yield; }
	}
}