//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S3K"]
#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();
let phase = 1;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(1);
	}
        case(EV_REQUEST_TIMER)
	{
                SetScriptResult(62);
        }
	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);
	renderKoishiSA(objEnemy);
	SetCommonData("boss", "KOISHI");
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	ObjMove_SetDestAtFrame(objEnemy, -500, -500, 60);
	TMain;
}

@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 TMain
{
	loop(60){ yield; }
	ObjEnemyBossScene_StartSpell(objScene);
	SpellStart(objEnemy, "Small Stone \"Koishi Maze\"", GetCurrentScriptDirectory() ~ "./img/CutInImage2.png", 1005, 1020, 0.38);
	loop(60){ yield; }
	Phase;
	Phone;
	loop(4)
	{
		loop(900){ yield; }
		phase++;
	}
	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());
}

task Phase
{
	let a = 1;
	while(phase == 1)
	{
		ascent(i in 0..4)
		{
			let dir = 1;
			ascent(j in 0..28)
			{
				let obj = CreateShotB1(192 - dir * 192, 448 - j * 32 - i * 4 - 16 * a,  1 * dir, 0, 14, 80 - dir * 20);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		loop(60){ yield; }
		a *= -1;
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	while(phase == 2)
	{
		ascent(i in 0..4)
		{
			let dir = 1;
			ascent(j in 0..14)
			{
				let obj = CreateShotB1(192 - dir * 192, 448 - j * 32 - i * 4, 1 * dir, 0, 11, 80 - 20 * dir);
				//let obj1 = CreateShotB1(192 + dir * 192, 448 - j * 32 - i * 4, -1.5 * dir, 0, 13, 60);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		loop(80){ yield; }
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	a = 1;
	while(phase == 3)
	{
		ascent(i in 0..2)
		{
			let dir = 1;
			ascent(j in 0..14)
			{
				let obj = CreateShotB1(192 - dir * 192, 448 - j * 32 - i * 4 - 8 * a, 1 * dir, 0, 9, 100 - dir * 25);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		ascent(i in 0..2)
		{
			let dir = 1;
			ascent(j in 0..12)
			{
				let obj = CreateShotB1(384 - j * 32 - i * 4 - 8 * a, 224 - dir * 224, 0, 1 * dir, 9, 100 - dir * 25);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		a *= -1;
		loop(100){ yield; }
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	a = 1;
	while(phase == 4)
	{
		ascent(i in 0..4)
		{
			let dir = 1;
			ascent(j in 0..7)
			{
				let obj = CreateShotB1(192 - dir * 192, 448 - j * 64 - i * 4 - 16 * a, 1 * dir, 0, 14, 100 - dir * 25);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		ascent(i in 0..4)
		{
			let dir = 1;
			ascent(j in 0..7)
			{
				let obj = CreateShotB1(384 - j * 64 - i * 4 - 16 * a, 224 - dir * 224, 0, 1 * dir, 11, 100 - dir * 25);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		loop(100){ yield; }
		a *= -1;
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
}

task Phone
{
	while(phase == 1)
	{
		SEPlay("telephone", 80);
		loop(30){ yield; }

		let px1 = GetPlayerX();
		let py1 = GetPlayerY();

		Target(px1, py1, objEnemy);

		loop(45){ yield; }

		let px2 = GetPlayerX();
		let py2 = GetPlayerY();

		let angle = atan2(py2 - py1, px2 - px1);

		ascent(i in 0..10)
		{
			CreateShotA2(px1 + cos(i * 36 + 180) * 20, py1 + sin(i * 36 + 180) * 20, 3, 36 * i, 0.01, 2, 142, 10);
		}
		ascent(i in 0..5)
		{
			CreateShotA4(px1 + cos(angle + i * 72) * 20, py1 + sin(angle + i * 72) * 20, 2, angle + 72 * i, 0.01, 0.3, 3.5, 298, 10);
			CreateShotA4(px1 + cos(angle + i * 72) * 20, py1 + sin(angle + i * 72) * 20, 2, angle + 72 * i, 0.01, -0.3, 3.5, 298, 10);
		}
		loop(40){ yield; }
	}
	while(phase == 2)
	{
		SEPlay("telephone", 80);
		loop(30){ yield; }

		let px1 = GetPlayerX();
		let py1 = GetPlayerY();

		Target(px1, py1, objEnemy);

		loop(45){ yield; }

		let px2 = GetPlayerX();
		let py2 = GetPlayerY();

		let angle = atan2(py2 - py1, px2 - px1);

		ascent(i in 0..12)
		{
			CreateShotA2(px1 + cos(i * 30 + 180) * 20, py1 + sin(i * 30 + 180) * 20, 3, 30 * i, 0.01, 2, 139, 10);
		}
		ascent(i in 0..6)
		{
			CreateShotA4(px1 + cos(angle + i * 60) * 20, py1 + sin(angle + i * 60) * 20, 2, angle + 60 * i, 0.01, 0.3, 3.5, 299, 10);
			CreateShotA4(px1 + cos(angle + i * 60) * 20, py1 + sin(angle + i * 60) * 20, 2, angle + 60 * i, 0.01, -0.3, 3.5, 299, 10);
		}
		loop(50){ yield; }
	}
	while(phase == 3)
	{
		SEPlay("telephone", 80);
		loop(30){ yield; }

		let px1 = GetPlayerX();
		let py1 = GetPlayerY();

		Target(px1, py1, objEnemy);

		loop(45){ yield; }

		let px2 = GetPlayerX();
		let py2 = GetPlayerY();

		let angle = atan2(py2 - py1, px2 - px1);

		ascent(i in 0..15)
		{
			CreateShotA2(px1 + cos(angle + i * 24 + 180) * 20, py1 + sin(angle + i * 24 + 180) * 20, 3, angle + 24 * i, 0.01, 2, 137, 10);
			CreateShotA2(px1 + cos(angle + i * 24) * 20, py1 + sin(angle + i * 24) * 20, 2, angle + 24 * i, 0.01, 3.5, 298, 10);
		}
		loop(60){ yield; }
	}
	while(phase == 4)
	{
		SEPlay("telephone", 80);
		loop(30){ yield; }

		let px1 = GetPlayerX();
		let py1 = GetPlayerY();

		Target(px1, py1, objEnemy);

		loop(45){ yield; }

		let px2 = GetPlayerX();
		let py2 = GetPlayerY();

		let angle = atan2(py2 - py1, px2 - px1);

		ascent(i in 0..12)
		{
			CreateShotA2(px1 + cos(angle + i * 30 + 180) * 20, py1 + sin(angle + i * 30 + 180) * 20, 3, angle + 30 * i, 0.01, 2, 137, 10);
		}
		ascent(i in 0..6)
		{
			CreateShotA3(px1 + cos(angle + i * 60) * 20, py1 + sin(angle + i * 60) * 20, 2, angle + 60 * i + 30, 0.01, 0.2, 3.5, 298, 10);
			CreateShotA3(px1 + cos(angle + i * 60) * 20, py1 + sin(angle + i * 60) * 20, 1.7, angle + 60 * i, 0.01, -0.2, 3, 299, 10);
		}
		loop(65){ yield; }
	}
}