//#東方弾幕風[Single]
#ScriptVersion[3]
#Title["S6K"]
#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 direction = 1;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(1);
	}
        case(EV_REQUEST_TIMER)
	{
                SetScriptResult(60);
        }
	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, 192, 224, 60);
	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; }
	ObjEnemyBossScene_StartSpell(objScene);
	SpellStart(objEnemy, "Record \"Two-way Polygraph\"", GetCurrentScriptDirectory() ~ "./img/CutInImage2.png", 1005, 1020, 0.38);
	loop(60){ yield; }
	Charge(objEnemy);
	loop(30){ yield; }
	Polygraph(1);
	loop(30){ yield; }
	Charge(objEnemy);
	loop(60){ yield; }
	direction *= -1;
	loop(90){ yield; }
	direction *= -1;
	Polygraph(-1);
	loop(6)
	{
		loop(420){ yield; }
		Charge(objEnemy);
		loop(60){ yield; }
		direction *= -1;
	}
	loop(390){ 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 Polygraph(dir)
{
	let angle = 90 - 15 * dir;
	Shot;
	loop(6)
	{
		let obj = CreateStraightLaserA1(192, 224, angle, 400, 10, 3600, 223 - dir, 0);
		angle += dir * 60;
		Laser(obj);
	}
	task Laser(obj)
	{
		while(!Obj_IsDeleted(objEnemy))
		{
			ObjStLaser_SetAngle(obj, ObjStLaser_GetAngle(obj) - 0.25 * dir);
			if(direction != dir)
			{
				ObjShot_SetIntersectionEnable(obj, false);
				ObjRender_SetAlpha(obj, 100);
			}
			else
			{
				ObjShot_SetIntersectionEnable(obj, true);
				ObjRender_SetAlpha(obj, 255);
			}
			yield;
		}
	}
	task Shot
	{
		let x = 0;

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

		let rad = ((px - 192) ^ 2 + (py - 224) ^ 2) ^ 0.5;
		if(cos(angle) < 0){ if(rad > (190 / -cos(angle))){ rad = 190 / -cos(angle); } }
		else{ if(rad > (190 / cos(angle))){ rad = 190 / cos(angle); } }

		while(!Obj_IsDeleted(objEnemy) && x < rad)
		{
			let px = GetPlayerX();
			let py = GetPlayerY();

			let rad = x;

			if(dir == direction){ SEPlay("Shot1", 80); }
			loop(6)
			{
				let obj = CreateShotA1(192 + cos(angle - 3 * dir) * rad, 224 + sin(angle - 3 * dir) * rad, 0, angle + 90 - 3 * dir, 51.5 - 2.5 * dir, 2);
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
				DeleteShot(obj, 238, false, false, 0);
				TransparentShot(obj);
				angle += dir * 60;
			}
			angle -= 2.5 * dir;
			x += 20;
			loop(10){ yield; }
		}
		while(!Obj_IsDeleted(objEnemy))
		{
			let px = GetPlayerX();
			let py = GetPlayerY();

			let rad = ((px - 192) ^ 2 + (py - 224) ^ 2) ^ 0.5;
			if(cos(angle) < 0){ if(rad > (190 / -cos(angle))){ rad = 190 / -cos(angle); } }
			else{ if(rad > (190 / cos(angle))){ rad = 190 / cos(angle); } }

			if(dir == direction){ SEPlay("Shot1", 80); }
			loop(6)
			{
				let obj = CreateShotA1(192 + cos(angle - 3 * dir) * rad, 224 + sin(angle - 3 * dir) * rad, 0, angle + 90 - 3 * dir, 51.5 - 2.5 * dir, 2);
        			ObjShot_SetSpellResist(obj, true); 
        			ObjShot_SetAutoDelete(obj, false);
				DeleteShot(obj, 240, false, false, 0);
				TransparentShot(obj);
				angle += dir * 60;
			}
			angle -= 2.5 * dir;
			loop(10){ yield; }
		}
	}
	task TransparentShot(obj)
	{
		while(!Obj_IsDeleted(objEnemy))
		{
			if(direction != dir)
			{
				ObjRender_SetAlpha(obj, 50);
				ObjShot_SetIntersectionEnable(obj, false);
			}
			else
			{
				ObjRender_SetAlpha(obj, 255);
				ObjShot_SetIntersectionEnable(obj, true);
			}
			yield;
		}
	}
}