//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S4K"]
#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(350);
	}
        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, 180, 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, "Koishi Festival \"Koishi Contest\"", GetCurrentScriptDirectory() ~ "./img/CutInImage2.png", 1005, 1020, 0.38);
	loop(60){ yield; }
	Charge(objEnemy);
	loop(30){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 10, 12);
	TSpirit(1);
	TSpirit(2);
}

task TSpirit(type)
{
	if(type == 1)
	{
		loop
		{
			let num = rand(4, 6);
			let angle = 90;
			let objSp = [];
			SEPlay("fam", 80);
			loop(num)
			{
				let obj = ObjEnemy_Create(OBJ_ENEMY);
				objSp = objSp ~ [obj];
				Spirit(obj, 284);
				MoveSpirit(obj, angle);
				ShotSpirit(obj);
				angle += 360 / num;
			}
			ObjMove_SetPosition(objSp, -100, -100);
			ascent(i in 0..length(objSp)){ while(!Obj_IsDeleted(objSp[i])){ yield; } }
			loop(30){ yield; }
		}

		task MoveSpirit(obj, angle)
		{
			let dis = 0;
			let a = -90;
			while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(objEnemy))
			{
				ObjMove_SetDestAtFrame(obj, ObjMove_GetX(objEnemy) + cos(angle) * dis, ObjMove_GetY(objEnemy) + sin(angle) * dis, 1);
				if(dis < 90){ dis += 2; }
				if(dis >= 90)
				{
					dis = sin(a) * 30 + 120;
					a++;
				}
				angle++;
				yield;
			}
		}

		task ShotSpirit(obj)
		{
			loop(30){ yield; }
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)	
			{
				let num = round(rand(3, 5));
				let x = ObjMove_GetX(obj);
				let y = ObjMove_GetY(obj);

				let ex = ObjMove_GetX(objEnemy);
				let ey = ObjMove_GetY(objEnemy);
				let angle = atan2(ey - y, ex - x) + num * 10;

				SEPlay("Shot1", 80);
				ascent(i in 0..num){ let objShot = CreateShotA1(x, y, 2, angle, 14, 10 + i * 6); reflect(objShot); }

				loop(30){ yield; }
			}
		}	
	}

	if(type == 2)
	{
		loop
		{
			let dir = 1;
			let objSp = [];
			SEPlay("fam", 80);
			loop(2)
			{
				let obj = ObjEnemy_Create(OBJ_ENEMY);
				objSp = objSp ~ [obj];
				Spirit(obj, 289);
				MoveSpirit(obj, dir);
				ShotSpirit(obj);
				dir *= -1;
			}
			ObjMove_SetPosition(objSp, -100, -100);
			ascent(i in 0..length(objSp)){ while(!Obj_IsDeleted(objSp[i])){ yield; } }
			loop(30){ yield; }
		}

		task MoveSpirit(obj, dir)
		{
			let x = 0;
			let a = -90;
			while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(objEnemy))
			{
				ObjMove_SetDestAtFrame(obj, x, 10, 1);
				x = 192 + sin(a) * 180 * dir;
				a++;
				yield;
			}
		}

		task ShotSpirit(obj)
		{
			loop(30){ yield; }
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)	
			{
				let x = ObjMove_GetX(obj);
				let y = ObjMove_GetY(obj);
				let num = round(rand(3, 5));
				
				let ex = ObjMove_GetX(objEnemy);
				let ey = ObjMove_GetY(objEnemy);
				let angle = atan2(ey - y, ex - x);

				SEPlay("Shot1", 80);
				ascent(i in 0..num){ let objShot = CreateShotA2(x, y, 2, angle, 0.02, 3.5, 9, 10 + i * 6); reflect(objShot); }

				loop(30){ yield; }
			}
		}	
	}
}

task TSubShot
{
	loop(150){ yield; }
	let a = 0;
	loop
	{
		loop(2)
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);

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

task Spirit(obj, gra)
{
	let objSpirit = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, 90, gra, 10);
        ObjShot_SetSpellResist(objSpirit, true); 
        ObjShot_SetAutoDelete(objSpirit, false);

	ObjEnemy_Regist(obj);
	ObjEnemy_SetLife(obj, 35);
	ObjEnemy_SetDamageRate(obj, 10, 12);
        while(ObjEnemy_GetInfo(obj, INFO_LIFE) > 0 && ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		ObjEnemy_SetIntersectionCircleToShot(obj, ObjMove_GetX(obj), ObjMove_GetY(obj), 32);
		ObjMove_SetDestAtFrame(objSpirit, ObjMove_GetX(obj), ObjMove_GetY(obj), 1);
		ObjShot_SetIntersectionCircleA1(objSpirit, 0);
		ObjRender_SetScaleXYZ(objSpirit, 1.2, 1.2, 1);
		yield;
	}
	if(!Obj_IsDeleted(objSpirit))
	{
		SEPlay("famdie", 80);
		let x = ObjMove_GetX(obj);
		let y = ObjMove_GetY(obj);
		ExplosionCircle(x, y, 8, 0.05);
		Obj_Delete(objSpirit);
		Obj_Delete(obj);
	}
}

task reflect(obj)
{
	while(ObjMove_GetX(obj) > 0 && ObjMove_GetX(obj) < 384 && ObjMove_GetY(obj)>0){ yield; }

	if( ObjMove_GetY(obj) <= 0)
	{
		ObjMove_SetAngle(obj, 0 - ObjMove_GetAngle(obj));
	}
	if( ObjMove_GetX(obj) <= 0)
	{
		ObjMove_SetAngle(obj, 180 - ObjMove_GetAngle(obj));
	}
	if( ObjMove_GetX(obj) >= 384)
	{
		ObjMove_SetAngle(obj, 180 - ObjMove_GetAngle(obj));
	}
}

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);
	}
	SetCommonData("dia", 1);
	loop(100)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}
	CloseScript(GetOwnScriptID());
}