//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S9K"]
#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(1);
	}
        case(EV_REQUEST_TIMER)
	{
                SetScriptResult(68);
        }
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(1000000);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL)
	{
		SetScriptResult( true );
	}
}

@Initialize
{
        objPlayer = GetPlayerObjectID();
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	SetCommonData("boss", "KOISHI");
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	ObjMove_SetPosition(objEnemy, 192, 224);
	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 TShot
{
	loop(90){ yield; }
	Charge(objEnemy);
	loop(60){ yield; }
	renderKoishiSA(objEnemy);
	ObjEnemyBossScene_StartSpell(objScene);

	SpellStart(objEnemy, "\"Subconcious Rose\"", GetCurrentScriptDirectory() ~ "./img/CutInImage2.png", 1005, 1020, 0.38);

	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);

	let obj1 = CreateShotA1(ex, ey, 0, 0, 298, 0);
	let obj2 = CreateShotA1(ex, ey, 0, 0, 299, 0);

	ObjMove_SetDestAtFrame(obj1, 0, 0, 60);
	ObjMove_SetDestAtFrame(obj2, 384, 448, 60);

	Move(obj1, 0, 0, 2, 1, 36, 5, 10, 26, 50);
	Move(obj2, 384, 448, 2, 1, 33, 5, 14, 30, 50);

	loop(1200){ yield; }

	obj1 = CreateShotA1(ex, ey, 0, 0, 298, 0);
	obj2 = CreateShotA1(ex, ey, 0, 0, 299, 0);

	ObjMove_SetDestAtFrame(obj1, 0, 448, 60);
	ObjMove_SetDestAtFrame(obj2, 384, 0, 60);

	Move(obj1, 0, 448, 2, 1, 33, 5, 10, 26, 60);
	Move(obj2, 384, 0, 2, -1, 36, 5, 14, 30, 60);

	loop(1200){ yield; }

	obj1 = CreateShotA1(ex, ey, 0, 0, 298, 0);
	obj2 = CreateShotA1(ex, ey, 0, 0, 299, 0);

	ObjMove_SetDestAtFrame(obj1, 384, 448, 60);
	ObjMove_SetDestAtFrame(obj2, 0, 0, 60);

	Move(obj1, 384, 448, 2, 1, 33, 4, 10, 26, 70);
	Move(obj2, 0, 0, 2, -1, 33, 4, 14, 30, 70);

	let obj3 = CreateShotA1(ex, ey, 0, 0, 298, 0);
	let obj4 = CreateShotA1(ex, ey, 0, 0, 299, 0);

	ObjMove_SetDestAtFrame(obj3, 0, 0, 60);
	ObjMove_SetDestAtFrame(obj4, 384, 448, 60);

	Move(obj3, 0, 0, 2, 1, 36, 4, 10, 26, 70);
	Move(obj4, 384, 448, 2, -1, 36, 4, 14, 30, 70);

	loop(1200){ yield; }

	obj1 = CreateShotA1(ex, ey, 0, 0, 298, 0);
	obj2 = CreateShotA1(ex, ey, 0, 0, 299, 0);

	ObjMove_SetDestAtFrame(obj1, 384, 448, 60);
	ObjMove_SetDestAtFrame(obj2, 0, 0, 60);

	Move(obj1, 384, 448, 0, 1, 60, 4, 10, 26, 70);
	Move(obj2, 0, 0, 0, -1, 60, 4, 14, 30, 70);

	obj3 = CreateShotA1(ex, ey, 0, 0, 298, 0);
	obj4 = CreateShotA1(ex, ey, 0, 0, 299, 0);

	ObjMove_SetDestAtFrame(obj3, 384, 0, 60);
	ObjMove_SetDestAtFrame(obj4, 0, 448, 60);

	Move(obj3, 384, 0, 0, 1, 60, 4, 10, 26, 40);
	Move(obj4, 0, 448, 0, -1, 60, 4, 14, 30, 40);
}

task Move(obj, x, y, speed, dir, num, delay, graphic1, graphic2, delayframe)
{
	ObjShot_SetSpellResist(obj, true); 
	loop(60){ yield; }
	ObjMove_SetPosition(obj, x, y);
	shot(obj);
	loop(1080)
	{
		if(dir == 1)
		{
			if(ObjMove_GetX(obj) >= 0  && ObjMove_GetY(obj) == 0 && ObjMove_GetX(obj) < 384){ ObjMove_SetPosition(obj, ObjMove_GetX(obj) + speed, ObjMove_GetY(obj)); }
			else if(ObjMove_GetX(obj) > 0 && ObjMove_GetY(obj) == 448 && ObjMove_GetX(obj) <= 384){ ObjMove_SetPosition(obj, ObjMove_GetX(obj) - speed, ObjMove_GetY(obj)); }
			else if(ObjMove_GetY(obj) > 0 && ObjMove_GetX(obj) == 0 && ObjMove_GetY(obj) <= 448){ ObjMove_SetPosition(obj, ObjMove_GetX(obj), ObjMove_GetY(obj) - speed); }
			else if(ObjMove_GetY(obj) >= 0 && ObjMove_GetX(obj) == 384 && ObjMove_GetY(obj) < 448){ ObjMove_SetPosition(obj, ObjMove_GetX(obj), ObjMove_GetY(obj) + speed); }
		}
		else if(dir == -1)
		{
			if(ObjMove_GetX(obj) >= 0  && ObjMove_GetY(obj) == 448 && ObjMove_GetX(obj) < 384){ ObjMove_SetPosition(obj, ObjMove_GetX(obj) + speed, ObjMove_GetY(obj)); }
			else if(ObjMove_GetX(obj) > 0 && ObjMove_GetY(obj) == 0 && ObjMove_GetX(obj) <= 384){ ObjMove_SetPosition(obj, ObjMove_GetX(obj) - speed, ObjMove_GetY(obj)); }
			else if(ObjMove_GetY(obj) > 0 && ObjMove_GetX(obj) == 384 && ObjMove_GetY(obj) <= 448){ ObjMove_SetPosition(obj, ObjMove_GetX(obj), ObjMove_GetY(obj) - speed); }
			else if(ObjMove_GetY(obj) >= 0 && ObjMove_GetX(obj) == 0 && ObjMove_GetY(obj) < 448){ ObjMove_SetPosition(obj, ObjMove_GetX(obj), ObjMove_GetY(obj) + speed); }
		}
		yield;
	}
	DeleteShot(obj, 0, true, true, "green");
	task shot(obj)
	{
		while(!Obj_IsDeleted(obj))
		{
			let ex = ObjMove_GetX(obj);
			let ey = ObjMove_GetY(obj);

			let px = ObjMove_GetX(objEnemy);
			let py = ObjMove_GetY(objEnemy);

			let angle = atan2(py - ey, px - ex);

			SEPlay("shot1", 80);
			ascent(i in 0..num)
			{
				let obj = CreateShotA1(ex, ey, 0.5 * speed + 1, angle + 360 / num * i * dir - 90, graphic1, 0);
				bloom(obj, i);
				task bloom(obj, i)
				{
					ObjMove_AddPatternA3(obj, i * delay, NO_CHANGE, NO_CHANGE, 0, 0, 2, graphic2);
					ObjMove_AddPatternA3(obj, i * delay + num * delay / 2, NO_CHANGE, NO_CHANGE, 0, 0, 2, graphic1);
					ObjMove_AddPatternA3(obj, i * delay + num * delay, NO_CHANGE, NO_CHANGE, 0, 0, 2, graphic2);
					ObjMove_AddPatternA3(obj, i * delay + num * delay / 2 + num * delay, NO_CHANGE, NO_CHANGE, 0, 0, 2, graphic1);
					ObjMove_AddPatternA3(obj, i * delay + num * delay * 2, NO_CHANGE, NO_CHANGE, 0, 0, 2, graphic2);
					ObjMove_AddPatternA3(obj, i * delay + num * delay / 2 + num * delay * 2, NO_CHANGE, NO_CHANGE, 0, 0, 2, graphic1);
				}
			}
			loop(delayframe){ yield; }
		}
	}
}

task TEnd
{
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0){ yield; }
	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	StartSlow(TARGET_ALL,30);
	loop(10)
	{
		ExplosionCircle(ex+rand(-50,50),ey+rand(-50,50),10,0.6);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		loop(10){ yield; }
	}
	StopSlow(TARGET_ALL);
	Explosion(ex, ey);
	ascent(i in 0..3){ ExplosionCircle(ex, ey, 5, 0.1 + 0.1 * i); }
	Obj_Delete(objEnemy);
	SEPlay("enemoff", 80);
	SetCommonData("dia", 1);
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) != 0)
	{
		NotifyEventAll(EV_USER_SYSTEM, 0);
	}
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(180)
	{
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
	}
	CloseScript(GetOwnScriptID());
}