//#東方弾幕風[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(300);
	}
        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;
}

@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
{
	loop(60){ yield; }
	loop(30){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	let angle = 90;
	let frame = 45;
	let wvel = 0.3;
	let ang = 8;
	loop
	{
		loop(24)
		{
			if(!Obj_IsDeleted(objEnemy))
			{
				let ex = ObjMove_GetX(objEnemy);
				let ey = ObjMove_GetY(objEnemy);

				let obj = CreateShotA4(ex + cos(angle) * 20, ey + sin(angle) * 20, 2.5, angle, -0.01, wvel, 1.5, 298, 30);
				angle += 15;
			}
		}
		loop(frame){ yield; }
		angle += ang;
		loop(24)
		{
			if(!Obj_IsDeleted(objEnemy))
			{
				let ex = ObjMove_GetX(objEnemy);
				let ey = ObjMove_GetY(objEnemy);

				let obj = CreateShotA4(ex + cos(angle) * 20, ey + sin(angle) * 20, 2.5, angle, -0.01, -wvel, 1.5, 299, 30);
				angle += 15;
			}
		}
		loop(frame){ yield; }
		angle += ang;
		ang++;
		if(frame > 30){ frame -= 1; }
		if(wvel < 0.51){ wvel += 0.03; }
	}
}

task TShot
{
	loop(60){ yield; }
	ObjEnemyBossScene_StartSpell(objScene);
	SpellStart(objEnemy, "\"Brambly Rose Garden\"", GetCurrentScriptDirectory() ~ "./img/CutInImage2.png", 1005, 1020, 0.38);
	Charge(objEnemy);
	loop(90){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	let num = 6;
	loop
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);

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

		let angle = atan2(py - ey, px - ex);
		loop(num)
		{
			if(!Obj_IsDeleted(objEnemy))
			{
				CreatePlant(ex, ey, angle, 298, 153, 20);
				CreatePlant(ex, ey, angle + 180 / num, 299, 158, 20);
				angle += 360 / num;
			}
		}
		loop(80){ yield; }
		ObjMove_SetDestAtFrame(objEnemy, rand(80, 304), 100, 60);
		loop(160){ yield; }

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

		angle = -135;
		loop(num)
		{
			if(!Obj_IsDeleted(objEnemy))
			{
				CreatePlant(px - cos(angle) * 40, py - sin(angle) * 40, angle + 36, 298, 153, 100);
				CreatePlant(px - cos(angle + 180 / num) * 40, py - sin(angle + 180 / num) * 40, angle + 180 / num + 36, 299, 158, 100);
				angle += 360 / num;
			}
		}
		loop(120){ yield; }
		ObjMove_SetDestAtFrame(objEnemy, rand(80, 304), 100, 60);
		loop(150){ yield; }
	}

	task CreatePlant(x, y, angMain, gr, gr1, de)
	{
		let ang = 54;

		CreateDelayLaserA1(x, y, angMain, 600, 60, gr1, de);
		loop(de){ yield; }

		let ox = x;
		let oy = y;

		let angle = angMain;
		flower;
		loop(56)
		{
			ox += cos(angle) * 10;
			oy += sin(angle) * 10;
			if(!Obj_IsDeleted(objEnemy))
			{
				SEPlay("shot1", 50);
				let obj = CreateShotA1(ox, oy, 0, angle, 44, 10);
				ang += 72;
				angle += sin(ang) * 25;
				DeleteShot(obj, 100, true, true, "green");
			}
			loop(2){ yield; }
		}
		task flower
		{
			loop(24){ yield; }
			let a = 10;
			loop(8)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					x += cos(angMain) * 60;
					y += sin(angMain) * 60;
					if(gr == 298)
					{
						let obj = CreateShotA1(x - cos(angMain) * 37.5, y - sin(angMain) * 37.5, 0, angMain, gr, 10);
						ObjMove_AddPatternA2(obj, 76, 2, angMain + 90 + a, 0.01, 0.3, 3);
					}
					else
					{ 
						let obj = CreateShotA1(x, y, 0, angMain, gr, 10);
						ObjMove_AddPatternA2(obj, 76, 2.5, angMain - 90 - a, 0.01, -0.2, 3.5);
					}
					loop(14){ yield; }
				}
				a += 30;
			}
		}
	}
}

task TEnd
{
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0){ yield; }
	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	SEPlay("enemoff", 60);
	Explosion(ex, ey);
	ascent(i in 0..3){ ExplosionCircle(ex, ey, 5, 0.1 + 0.1 * i); }
	Obj_Delete(objEnemy);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(100)
	{
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		yield;
	}
	CloseScript(GetOwnScriptID());
}