//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["EikaS3R"]
#Text[" "]
#Background["./background/Default_Background.txt"]
#System["./system/Default_System.txt"]
#BGM["./BGM.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(61);
        }
	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);
	renderEikaWBaWC(objEnemy);
	SetCommonData("boss", "EIKA");
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	ObjMove_SetDestAtFrame(objEnemy, -500, -500, 60);
	if(GetCommonData("Hidden", 0) == 0){ TMain; }
	if(GetCommonData("Hidden", 0) == 1){ THidden; }
}

@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, "Stone Trap \"Stone Maze\"", GetCurrentScriptDirectory() ~ "./img/CutInImage.png", 620, 1050, 0.38);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	Phase;
	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..8)
		{
			let dir = 1;
			ascent(j in 0..7)
			{
				let obj = CreateShotB1(192 - dir * 192, 448 - j * 64 - i * 4 - 16 * a, 1 * dir, 0, 13, 36 - dir * 9);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		loop(36){ yield; }
		a *= -1;
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	while(phase == 2)
	{
		ascent(i in 0..7)
		{
			let dir = 1;
			ascent(j in 0..16)
			{
				let obj = CreateShotB1(192 - dir * 192, 448 - j * 32 - i * 4, 1.5 * dir, 0, 9, 60 - 15 * 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(60){ yield; }
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	a = 1;
	while(phase == 3)
	{
		ascent(i in 0..4)
		{
			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..4)
		{
			let dir = 1;
			ascent(j in 0..12)
			{
				let obj = CreateShotB1(384 - j * 32 - i * 4 - 8 * a, 224 - dir * 224, 0, 1 * dir, 13, 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..8)
		{
			let dir = 1;
			ascent(j in 0..7)
			{
				let obj = CreateShotB1(192 - dir * 192, 448 - j * 64 - i * 4 - 16 * a, 1 * dir, 0, 13, 100 - dir * 25);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		ascent(i in 0..8)
		{
			let dir = 1;
			ascent(j in 0..7)
			{
				let obj = CreateShotB1(384 - j * 64 - i * 4 - 16 * a, 224 - dir * 224, 0, 1 * dir, 9, 100 - dir * 25);
				dir *= -1;
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
			}
		}
		loop(100){ yield; }
		a *= -1;
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
}

task THidden
{
	loop(60){ yield; }
	ObjEnemyBossScene_StartSpell(objScene);

	SpellStart(objEnemy, "Stone Trap \"Stone Maze\"", GetCurrentScriptDirectory() ~ "./img/CutInImage.png", 620, 1050, 0.38);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	StoneFall;

	Stone(0);
	loop(900){ yield; }
	Stone(2);
	loop(900){ yield; }
	Stone(1);
	loop(900){ yield; }
	Stone(3);
	loop(900){ 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());

	task Stone(dir)
	{
		while(!Obj_IsDeleted(objEnemy))
		{
			let px = GetPlayerX();
			let py = GetPlayerY();

			let angle = 90 * dir;

			ascent(i in 0..5)
			{
				if(dir == 0 || dir == 2)
				{
					let obj = CreateShotA1(192 * dir, py + 10 - i * 5, 2, angle, 9, 10);
        				ObjShot_SetSpellResist(obj, true); 
        				ObjShot_SetAutoDelete(obj, false);
				}
				if(dir ==  1|| dir == 3)
				{
					let obj = CreateShotA1(px + 10 - i * 5, 224 * (dir - 1), 2, angle, 9, 10);
        				ObjShot_SetSpellResist(obj, true); 
        				ObjShot_SetAutoDelete(obj, false);
				}
			}
			loop(15){ yield; }
		}
	}

	task StoneFall
	{
		let num = 150;
		while(!Obj_IsDeleted(objEnemy))
		{
			loop(num){ yield; }
			let py = GetPlayerY();
			StoneShot(-20, py, 0);
			loop(num){ yield; }
			let px = GetPlayerX();
			StoneShot(px, -20, 1);
			loop(num){ yield; }
			py = GetPlayerY();
			StoneShot(403, py, 2);
			loop(num){ yield; }
			px = GetPlayerX();
			StoneShot(px, 468, 3);
			if(num > 90){ num -= 20; }
			else{ num += 10; }
			
		}
		task StoneShot(x, y, dir)
		{
			let obj = CreateShotA2(x, y, 1.5, 90 * dir, 0.04, 3.5, 13, 20);
        		ObjShot_SetSpellResist(obj, true); 
        		ObjShot_SetAutoDelete(obj, false);
			ObjRender_SetScaleXYZ(obj, 300 / 17, 300 / 17, 1);
			loop(10){ yield; }
			SEPlay("Shot1", 80);
			while(!Obj_IsDeleted(obj))
			{
				ObjShot_SetIntersectionCircleA1(obj, 300 / 17 * 3.5);
				yield;
			}
		}
	}
}