//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["N1K"]
#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
{
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	loop(60){ yield; }
	Charge(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	loop(90){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 80, 80);
	let angle = rand(1, 360);

	loop
	{
		Obj_SetValue(objEnemy,"cast",1);
		let angle = rand(1, 360);
		let a = 1;
		loop(72)
		{
			SEPlay("Shot1", 80);
			loop(7)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					let ex = ObjMove_GetX(objEnemy);
					let ey = ObjMove_GetY(objEnemy);
					ascent(i in 0..3)
					{
						let obj = CreateShotA2(ex + cos(angle) * 30, ey + sin(angle) * 30 + i * 6, 3.5, angle, -0.3, 2.4, 20, 10);
						ObjRender_SetAngleZ(obj, -ObjMove_GetAngle(obj));
					}
					angle += 360 / 7;
				}
			}
			loop(2){ yield; }
			angle += sin(a) ^ 3 * 30;
			if(sin(a) < 0.1){ a += 20;}
			else{ a += 5; }
		}
		Obj_SetValue(objEnemy,"cast",0);
		loop(60){ yield; }
	}
}

task TSubShot
{
	loop(200){ yield; }
	let n = 1;
	let a = 0;
	loop
	{
		loop(2)
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);

			let ang = rand(1, 360);
			let num = round(rand(20, 25));
			SEPlay("Shot2", 80);
			loop(num)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					ascent(i in 0..n)
					{
						let obj = CreateShotA2(ex, ey, 3, ang, -0.1, 2.5 - 0.4 * i, 265, 10 + i);
						ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
					}
					ang += 360 / num;
				}
			}
			loop(30){ yield; }
		}
		if(n < 3){ n++; }
		a += 45;
		ObjMove_SetDestAtFrame(objEnemy, 192 + sin(a) * 142, 120 - sin(a) * 20, 60);
		loop(120){ yield; }
	}
}