//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["EikaS4R"]
#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();
//SetCommonData("Hidden", 1);

@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);
	renderEikaWBaWC(objEnemy);
	SetCommonData("boss", "EIKA");
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	ObjMove_SetDestAtFrame(objEnemy, 192, 200, 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 TShot
{
	loop(60){ yield; }
	SpellStart(objEnemy, "Children Festival \"Stone Stacking Contest\"", GetCurrentScriptDirectory() ~ "./img/CutInImage.png", 620, 1050, 0.38);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	ObjEnemyBossScene_StartSpell(objScene);
	loop(60){ yield; }
	Charge(objEnemy);
	loop(30){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 10, 12);
	if(GetCommonData("Hidden", 0) == 0)
	{
		TSpirit(1);
		TSpirit(2);
		TSubShot;
	}
	else if(GetCommonData("Hidden", 0) == 1)
	{
		TSpirit(3);
		TSpirit(4);
		//THiddenShot;
	}
}

task TSpirit(type)
{
	if(type == 1)
	{
		let num = 3;
		loop
		{
			let angle = 90;
			let objSp = [];
			SEPlay("fam", 80);
			loop(num + 2)
			{
				if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0)
				{
					let obj = ObjEnemy_Create(OBJ_ENEMY);
					objSp = objSp ~ [obj];
					Spirit(obj);
					MoveSpirit(obj, angle);
					ShotSpirit(obj, num);
					angle += 360 / (num + 2);
				}
			}
			ObjMove_SetPosition(objSp, -100, -100);
			num++;
			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 + 90 + num * 10;
					a++;
				}
				angle++;
				yield;
			}
		}

		task ShotSpirit(obj, num)
		{
			loop(30){ yield; }
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)	
			{
				SEPlay("Shot1", 80);
				let x = ObjMove_GetX(obj);
				let y = ObjMove_GetY(obj);

				ascent(i in 0..num){ CreateShotA1(x, y, 2, 90, 15, 10 + i * 4); }

				loop(40){ yield; }
			}
		}	
	}

	if(type == 2)
	{
		let num = 3;
		loop
		{
			let dir = 1;
			let objSp = [];
			SEPlay("fam", 80);
			loop(2)
			{
				if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0)
				{
					let obj = ObjEnemy_Create(OBJ_ENEMY);
					objSp = objSp ~ [obj];
					Spirit(obj);
					MoveSpirit(obj, dir);
					ShotSpirit(obj, num);
					dir *= -1;
				}
			}
			ObjMove_SetPosition(objSp, -100, -100);
			num++;
			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, num)
		{
			loop(30){ yield; }
			while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)	
			{
				SEPlay("Shot1", 80);
				let x = ObjMove_GetX(obj);
				let y = ObjMove_GetY(obj);

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

				let angle = atan2(py - y, px - x);

				ascent(i in 0..num){ CreateShotA2(x, y, 2, angle, 0.02, 3.5, 15, 10 + i * 4); }

				loop(50){ yield; }
			}
		}	
	}

	if(type == 3)
	{
		while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0)
		{
			let angle = 90;
			let objSp = [];
			let a = 0;
			SEPlay("fam", 80);
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0)
			{
				loop(7)
				{
					let obj = ObjEnemy_Create(OBJ_ENEMY);
					objSp = objSp ~ [obj];
					Spirit(obj);
					MoveSpirit(obj, angle);
					ShotSpirit(obj, angle);
					angle += 360 / 7;
				}
			}
			ObjMove_SetPosition(objSp, -100, -100);
			ascent(i in 0..length(objSp)){ while(!Obj_IsDeleted(objSp[i])){ yield; } }
			a += 30;
			ObjMove_SetDestAtFrame(objEnemy, 192 + 80 * sin(a), 200 - sin(a) * 30, 60);
			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 < 10){ dis += 2; }
				if(dis >= 10)
				{
					dis = sin(a) * 70 + 80;
					a += 2;
				}
				angle += 0.8;
				yield;
			}
		}

		task ShotSpirit(obj, ang)
		{
			loop(30){ yield; }
			SEPlay("Shot1", 80);
			let a = 1;
			while((ObjEnemy_GetInfo(obj, INFO_LIFE) > 0) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)	
			{
				let x = ObjMove_GetX(obj);
				let y = ObjMove_GetY(obj);

				ascent(i in 0..3)
				{
					let obj1 = CreateShotA2(x, y, 0.3, ang + 180 + i * 120, 0.03, 1.5, 15, 10);
					ObjMove_AddPatternA2(obj1, 30, 2.5, NO_CHANGE, -0.02, 0, 1.5);
				}

				ang = sin(a) ^ 3 * 20 + ang;
				a += 10;
				loop(15){ yield; }
			}
		}	
	}

	if(type == 4)
	{
		let dir = 1;
		let num = 50;
		while(!Obj_IsDeleted(objEnemy))
		{
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0)
			{
				let obj = ObjEnemy_Create(OBJ_ENEMY);
				Spirit(obj);
				MoveSpirit(obj, dir);
				ShotSpirit(obj, dir, num);
			}
			loop(50){ yield; }
			if(num < 30){ num = 50; }
			else{ num--; }
			dir *= -1;
		}

		task MoveSpirit(obj, dir)
		{
			let x = 0;
			let a = -90;
			ObjMove_SetPosition(obj, 192 - dir * 150, 224 - dir * 230);
			ObjMove_SetSpeed(obj, 2);
			ObjMove_SetAngle(obj, 90 * dir);
			while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(objEnemy)){ yield; }
			Obj_Delete(obj);
		}

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

				let angle = 90 - 90 * dir;

				ascent(i in 0..5){ CreateShotA2(x, y, 2, angle, 0.02, 3.5 - i * 0.3, 15, 10 + i * 4); }

				loop(num){ yield; }
			}
		}	
	}
}

task TSubShot
{
	loop(90){ yield; }
	let num = 10;
	let a = 90;
	loop
	{
		loop(2)
		{
			let ex = ObjMove_GetX(objEnemy);
			let ey = ObjMove_GetY(objEnemy);

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

			let angle = atan2(py - ey, px - ex);
			if(!Obj_IsDeleted(objEnemy))
			{
				SEPlay("Shot2", 80);
				loop(num)
				{
					ascent(i in 0..4)
					{
						let obj = CreateShotA2(ex + cos(angle) * 50, ey + sin(angle) * 50 - i * 10, 2, angle, 0.02, 3.5, 65, 10);
						ObjRender_SetAngleZ(obj, 90);
					}
					angle += 360 / num;
				}
				angle += 30;
			}
			if(num < 16){ num++; }
			else if(num == 16){ num = 10; }
			loop(30){ yield; }
		}
		loop(30){ yield; }
		ObjMove_SetDestAtFrame(objEnemy, ObjMove_GetX(objEnemy) + 16 * sin(a), ObjMove_GetY(objEnemy), 60);
		a += 30;
		loop(60){ yield; }
	}
}

task THiddenShot
{
	loop(90){ yield; }

	let angle1 = 90;
	let a = 0;
	let gra = 49;

	loop
	{
		let angle = angle1;

		loop(30)
		{
			if(!Obj_IsDeleted(objEnemy))
			{
				let ex = ObjMove_GetX(objEnemy);
				let ey = ObjMove_GetY(objEnemy);
				loop(10)
				{
					CreateShotA2(ex, ey, 2, angle1, -0.005, 1.5, gra, 10);
					CreateShotA2(ex, ey, 2, angle, -0.005, 1.5, gra, 10);
					angle1 += 360 / 10;
					angle += 360 / 10;
				}
				angle1 += 7;
				angle -= 7;
			}
			if(gra == 55){ gra = 49; }
			else{ gra++; }
			loop(15){ yield; }
		}
		a += 45;
		ObjMove_SetDestAtFrame(objEnemy, 192 + sin(a) * 100, 200, 30);
		loop(30){ yield; }
		angle1 += 45;
	}
}

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

	ObjEnemy_Regist(obj);
	ObjEnemy_SetLife(obj, 80);
	ObjEnemy_SetDamageRate(obj, 10, 12);
	ObjMove_SetPosition(objSpirit, -100, -100);
        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_SetIntersectionEnable(objSpirit, false);
		ObjRender_SetScaleXYZ(objSpirit, 1.2, 1.2, 1);
		yield;
	}
	if(!Obj_IsDeleted(objSpirit))
	{
		let x = ObjMove_GetX(obj);
		let y = ObjMove_GetY(obj);
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) > 0 && GetCommonData("Hidden", 0) == 1)
		{
			ascent(i in 0..10)
			{
				CreateShotA1(x, y, 2, 90 + i * 36, 113, 10);
			}
		}
		ExplosionCircle(x, y, 8, 0.05);
		SEPlay("famdie", 80);
		Obj_Delete(objSpirit);
		Obj_Delete(obj);
	}
}

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_IMMEDIATE);
		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_IMMEDIATE);
	loop(180)
	{
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
	}
	CloseScript(GetOwnScriptID());
}