//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S2K"]
#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(325);
	}
        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;
	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);

	yield;
}

task TShot
{
	loop(60){ yield; }
	ObjEnemyBossScene_StartSpell(objScene);
	SpellStart(objEnemy, "Small Stone \"Koishi Wave\"", GetCurrentScriptDirectory() ~ "./img/CutInImage2.png", 1005, 1020, 0.38);
	loop(60){ yield; }
	Charge(objEnemy);
	loop(30){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 10, 12);
	let num = 6;
	loop(60){ yield; }
	loop
	{
		let dis = 0;
		loop(2)
		{
			ascent(i in 0..(num + 1))
			{
				CreateStone(dis + i * 384 / (num - 1), i + 1);
			}
			loop(200){ yield; }
			dis += 192 / num
		}
	}
}

task CreateStone(x, a)
{
	let angle = 270;
	CreateDelayLaserA1(x, 450, angle, 800, 30, 160, 30);
	loop(30){ yield; }

	SEPlay("BulletWave", 80);
	ascent(i in 0..26)
	{
		if(!Obj_IsDeleted(objEnemy))
		{
			let obj = CreateShotA1(x + i * (448 / 24) * cos(angle), 448 - (448 / 24) * i * sin(angle + 180), 0, 90 + i ^ 3 * a, round(rand(17, 24)), 20);
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			ObjMove_AddPatternA2(obj, (26 - i) * 3 + 30, 0.5, NO_CHANGE, 0.01, 0, 1.5);
			loop(3){ yield; }
		}
	}
}

task TSubShot
{
	loop(210){ yield; }
	let num = round(rand(10, 16));
	let dir = 1;
	let a = 0;
	loop
	{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);

			let angle = rand(1, 360);
			SEPlay("Shot2", 80);
			Obj_SetValue(objEnemy,"cast",1);
			if(!Obj_IsDeleted(objEnemy))
			{
				loop(num)
				{
					ascent(i in 0..4)
					{
						let obj = CreateShotA4(ex + cos(angle + i * 7 * dir) * 50, ey + sin(angle + i * 7 * dir) * 50, 2, angle + i * 7 * dir, 0.01, -0.2 * dir, 3, 265, 10 + i * 4);
					}
					angle += 360 / num;
				}
			}
			loop(30){ yield; }
			dir *= -1;
			Obj_SetValue(objEnemy,"cast",0);
			a += 45;
			ObjMove_SetDestAtFrame(objEnemy, 192 + sin(a) * 142, 120 - sin(a) * 20, 60);
			loop(90){ yield; }
	}
}

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