//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["N3K"]
#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(2000);
	}
        case(EV_REQUEST_TIMER)
	{
                SetScriptResult(30);
        }
}

@Initialize
{
        objPlayer = GetPlayerObjectID();
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	renderKoishiSA(objEnemy);
	SetCommonData("boss", "KOISHI");

	ObjEnemy_SetDamageRate(objEnemy, 30, 30);
	ObjMove_SetDestAtFrame(objEnemy, 192, 100, 60);
	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);

	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0)
	{
		Obj_Delete(objEnemy);
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		CloseScript(GetOwnScriptID());
        }
	yield;
}

task TShot
{
	loop(60){ yield; }
	Charge(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	loop(60){ yield; }
	ObjEnemy_SetDamageRate(objEnemy, 80, 80);
	TSubShot1;
	TSubShot2;
	TSubShot3;
}

task TSubShot1
{
	let angle = 282;
	let angle1 = -angle;

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

		let x = ex + cos(angle) * 100;
		let y = ey + sin(angle) * 20;

		let dis = ((x - ex) ^ 2 + (y - ey) ^ 2) ^ 0.5;
		let ang = atan2(y - ey, x - ex);

		let x1 = ex + cos(angle1) * 100;
		let y1 = ey + sin(angle1) * 20;

		let dis1 = ((x1 - ex) ^ 2 + (y1 - ey) ^ 2) ^ 0.5;
		let ang1 = atan2(y1 - ey, x1 - ex);

		if(!Obj_IsDeleted(objEnemy))
		{
			SEPlay("Shot1", 80);
			let obj = CreateShotA1(ex + cos(ang - 45) * dis, ey + sin(ang - 45) * dis, 0.4, angle - 45 - 270, 22, 10);
			ObjMove_AddPatternA1(obj, 30, 2, NO_CHANGE);
			let obj1 = CreateShotA1(ex + cos(ang1 - 135) * dis, ey + sin(ang1 - 135) * dis, 0.4, angle1 - 135 + 275, 22, 10);
			ObjMove_AddPatternA1(obj1, 30, 2, NO_CHANGE);
			angle += 12;
			angle1 -= 12;
		}
		loop(3){ yield; }
	}
}

task TSubShot2
{
	loop(60){ yield; }
	let angle = 276;
	let angle1 = -angle;
	loop
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);

		let x = ex + cos(angle) * 80;
		let y = ey + sin(angle) * 15;

		let dis = ((x - ex) ^ 2 + (y - ey) ^ 2) ^ 0.5;
		let ang = atan2(y - ey, x - ex);

		let x1 = ex + cos(angle1) * 100;
		let y1 = ey + sin(angle1) * 20;

		let dis1 = ((x1 - ex) ^ 2 + (y1 - ey) ^ 2) ^ 0.5;
		let ang1 = atan2(y1 - ey, x1 - ex);

		if(!Obj_IsDeleted(objEnemy))
		{
			SEPlay("Shot1", 80);
			let obj = CreateShotA1(ex + cos(ang - 20) * dis, ey + sin(ang - 20) * dis, 0.4, angle - 20 - 300, 20, 10);
			ObjMove_AddPatternA1(obj, 30, 2.5, NO_CHANGE);
			let obj1 = CreateShotA1(ex + cos(ang1 - 160) * dis, ey + sin(ang1 - 160) * dis, 0.4, angle1 - 160 + 300, 20, 10);
			ObjMove_AddPatternA1(obj1, 30, 2.5, NO_CHANGE);
			angle += 12;
			angle1 -= 12;
		}
		loop(3){ yield; }
	}
}

task TSubShot3
{
	loop(130){ yield; }
	let angle = 276;
	let angle1 = -angle;
	loop
	{
		let ex = ObjMove_GetX(objEnemy);
		let ey = ObjMove_GetY(objEnemy);

		let x = ex + cos(angle) * 100;
		let y = ey + sin(angle) * 5;

		let dis = ((x - ex) ^ 2 + (y - ey) ^ 2) ^ 0.5;
		let ang = atan2(y - ey, x - ex);

		let x1 = ex + cos(angle1) * 100;
		let y1 = ey + sin(angle1) * 5;

		let dis1 = ((x1 - ex) ^ 2 + (y1 - ey) ^ 2) ^ 0.5;
		let ang1 = atan2(y1 - ey, x1 - ex);

		if(!Obj_IsDeleted(objEnemy))
		{
			SEPlay("Shot1", 80);
			let obj = CreateShotA1(ex + cos(ang - 0) * dis, ey + sin(ang - 0) * dis, 0.4, angle, 23, 10);
			ObjMove_AddPatternA1(obj, 30, 2.5, NO_CHANGE);
			let obj1 = CreateShotA1(ex + cos(ang1 - 90) * dis, ey + sin(ang1 - 90) * dis, 0.4, angle1 - 90, 23, 10);
			ObjMove_AddPatternA1(obj1, 30, 2.5, NO_CHANGE);
			angle += 12;
			angle1 -= 12;
		}
		loop(3){ yield; }
	}
}

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

			let ang = rand(1, 360);

			SEPlay("Shot2", 80);
			loop(20)
			{
				if(!Obj_IsDeleted(objEnemy))
				{
					ascent(i in 0..3)
					{
						let obj = CreateShotA2(ex, ey, 3, ang, -0.1, 2.5 - 0.5 * i, 267, 10 + i);
					}
					ang += 18;
				}
			}
			loop(50){ yield; }
		}
		a += 90;
		ObjMove_SetDestAtFrame(objEnemy, 192 + sin(a) * 82, 120 - sin(a) * 20, 60);
		loop(120){ yield; }
	}
}