//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S8K"]
#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(350);
	}
        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);
	renderKoishiSA(objEnemy);
	SetCommonData("boss", "KOISHI");
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	ObjMove_SetDestAtFrame(objEnemy, 192, 100, 60);
	TEnd;
	TSubShot;
	TShot;
}

@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 TSubShot
{
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	loop(60){ yield; }
	ObjEnemyBossScene_StartSpell(objScene);
	SpellStart(objEnemy, "Instinct\"Release of Love\"", GetCurrentScriptDirectory() ~ "./img/CutInImage2.png", 1005, 1020, 0.38);
	loop(60){ yield; }
	Charge(objEnemy);
	loop(30){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	let angle = 270;
	let frame = 40;
	let wvel = 0.2;
	let ang = 10;
	loop
	{
		loop(24)
		{
			if(!Obj_IsDeleted(objEnemy))
			{
				let ex = ObjMove_GetX(objEnemy);
				let ey = ObjMove_GetY(objEnemy);

				let obj = CreateShotA4(ex + cos(angle) * 10, ey + sin(angle) * 10, 2.5, angle, 0, wvel, 0.75, 264, 30);
				let obj1 = CreateShotA4(ex + cos(angle) * 10, ey + sin(angle) * 10, 2.5, angle, 0, -wvel, 0.75, 267, 30);

				angle += 15;
			}
		}
		loop(frame){ yield; }
		angle += ang;
		ang += 8;
		frame = 70 + sin(angle) * 20;
		wvel = 0.2 + sin(angle) * 0.1;
	}
}

task TShot
{
	loop(120){ yield; }
	let dis = 50;
	let a = 0;
	loop
	{
		loop(2)
		{
			loop(80){ yield; }
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);
	
			let px = GetPlayerX();
			let py = GetPlayerY();
	
			let angle = atan2(py - ey, px - ex);
			loop(18)	
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					let obj = CreateShotA4(ex + cos(angle + 40) * 600, ey + sin(angle + 40) * 600, 1.5, angle + 180 - 18, 0, 0.15, 1.5, 265, 10);
					let obj1 = CreateShotA4(ex + cos(angle - 40) * 600, ey + sin(angle - 40) * 600, 1.5, angle + 180 + 18, 0, -0.15, 1.5, 265, 10);
        				ObjShot_SetAutoDelete(obj, false);
        				ObjShot_SetAutoDelete(obj1, false);
					DeleteShot(obj, 380, true, false, "purple");
					DeleteShot(obj1, 380, true, false, "purple");
					angle += 20;
				}
			}
		}
		a += 45;
		ObjMove_SetDestAtFrame(objEnemy, 192 + sin(a) * 92, 120 - sin(a) * 20, 60);
	}
}

task TEnd
{
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0){ yield; }
	Obj_Delete(objEnemy);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) != 0)
	{
		NotifyEventAll(EV_USER_SYSTEM, 0);
	}
	loop(20)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}
	CloseScript(GetOwnScriptID());
}