#東方弾幕風[Player]
#ScriptVersion[3]	//スクリプトバージョン3が必須です
#ID["Reimu"]
#Title["霊夢"]
#Text["プレイヤーサンプルです。闇符"]
#ReplayName["Reimu"]

#include".\..\Common/Common.txt"

let objPlayer = GetPlayerObjectID();
let objOption =[];
let OptionX=[0,0];
let OptionY=[0,0];
let current = GetCurrentScriptDirectory();
let SE_graze=current ~ "../Common/seGraze.wav";
@Initialize
{
	let path = current ~ "ReimuShotData.txt";
	LoadPlayerShotData(path); //自弾画像ロード

	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 0, 30); //当たり判定登録
	SetPlayerSpeed(4.0,1.5);

	TImage(GetCurrentScriptDirectory() ~ "img/STG_Player01.png");//自機描画用タスク起動
	OptionMove;
	TRenderOption(GetCurrentScriptDirectory() ~ "img/STG_Player01.png");
	TShot();//弾発射用タスク起動
	TShot2();//弾発射用タスク起動
	SetPlayerRebirthFrame(0);
	SetPlayerDownStateFrame(120);
	LoadSound(SE_graze);
	LoadTexture(GetCurrentScriptDirectory~"../Common/Light.png");
}

@MainLoop
{
	yield;
}

@Finalize
{
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
	{
		SetScriptResult(false);//スペル発動不可
	}
	case(EV_HIT)
	{
		//被弾
		PlaySE(current ~ "se/sePlayerCollision.wav");
		TExplosion();
	}
	case(EV_PLAYER_REBIRTH)
	{
		//復帰
		SetPlayerSpell(3);
		SetPlayerInvincibilityFrame(240);
	}
	case(EV_GET_ITEM)
	{

	}
	case(EV_GRAZE)
	{
		//グレイズ通知
		GrazeEffect(GetEventArgument(1));
		Play_SE(SE_graze);
	}
}



task OptionMove
{
	let HiOpX=[24,-24];
	let HiOpY=[-8,-8];
	let LowOpX=[10,-10];
	let LowOpY=[-24,-24];
	let LowCount=0;
	loop
	{
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
		{
			LowCount+=1;
			if(LowCount>10){LowCount=10;}
		}
		else
		{
			LowCount-=1;
			if(LowCount<0){LowCount=0;}
		}
		ascent(i in 0..2)
		{
			OptionX[i]=HiOpX[i]*(10-LowCount)/10+LowOpX[i]*(LowCount)/10;
			OptionY[i]=HiOpY[i]*(10-LowCount)/10+LowOpY[i]*(LowCount)/10;
		}
		yield;
	}
}

task TShot
{
	let pShot=false;
	ShotCheck;
	let Speed=12.5;
	let MainDamage_L=9.4;
	let MainDamage=2.0;
	let DamageRate=1;
	loop
	{
		while(!pShot){yield;}
		if(IsPlayerSpellActive){DamageRate=0.5;}
		else{DamageRate=1.0;}
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
		{//単位秒威力＝9.4＊15＝141
			MainShot2(GetPlayerX()+8*cos(270), GetPlayerY()+16, Speed, 270, MainDamage_L, 1, 2);
		}
		else
		{//単位秒威力＝2.0＊5＊15≒150
			MainShot(GetPlayerX()+8*cos(270), GetPlayerY()+16, Speed, 270, MainDamage*1.00, 1, 4);
			MainShot(GetPlayerX()+8*cos(262), GetPlayerY()+16, Speed, 262, MainDamage*1.00, 1, 4);
			MainShot(GetPlayerX()+8*cos(278), GetPlayerY()+16, Speed, 278, MainDamage*1.00, 1, 4);
			MainShot(GetPlayerX()+8*cos(254), GetPlayerY()+16, Speed, 254, MainDamage*1.00, 1, 4);
			MainShot(GetPlayerX()+8*cos(286), GetPlayerY()+16, Speed, 286, MainDamage*1.00, 1, 4);
		}
		loop(4){yield;}
	}

	task ShotCheck
	{
		loop
		{
			if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot)
			{
				pShot=true;

			}
			else
			{
				pShot=false;

			}
			yield;
		}
	}

	task MainShot(x,y,speed,angle,damage,pene,id)
	{
		let obj=CreatePlayerShotA1(x,y,speed,angle,damage,pene,id);
		Obj_SetRenderPriorityI(obj,20);
		let scale=0.75;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		while(!Obj_IsDeleted(obj)){yield;}
	}

	task MainShot2(x,y,speed,angle,damage,pene,id)
	{
		let obj=CreatePlayerShotA1(x,y,speed,angle,damage,pene,id);
		Obj_SetRenderPriorityI(obj,20);
		let scale=0.75;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		while(!Obj_IsDeleted(obj)){yield;}
	}
}

task TShot2
{
	let pShot=false;
	ShotCheck;
	let HomingDamage_L=3.5;
	let HomingDamage=2.5;
	let HomingAngle=[-75,-45,-15];
	let HomingNum=0;
	let DamageRate=1;
	loop
	{
		while(!pShot){yield;}
		if(IsPlayerSpellActive){DamageRate=0.5;}
		else{DamageRate=1.0;}

		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
		{//単位秒威力＝3.5＊2＊12＝84
			HomingShot_L(GetPlayerX+OptionX[0], GetPlayerY+OptionY[0],270+15,HomingDamage_L);
			HomingShot_L(GetPlayerX+OptionX[1], GetPlayerY+OptionY[1],270-15,HomingDamage_L);
		}
		else
		{//単位秒威力＝2.5＊2＊12＝60
			HomingShot_S(GetPlayerX+OptionX[0], GetPlayerY+OptionY[0],0+HomingAngle[HomingNum%3],HomingDamage);
			HomingShot_S(GetPlayerX+OptionX[1], GetPlayerY+OptionY[1],180-HomingAngle[HomingNum%3],HomingDamage);
		}
		HomingNum+=1;
		loop(5){yield;}
	}

	task ShotCheck
	{
		loop
		{
			if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot)
			{
			pShot=true;

			}
			else
			{
			pShot=false;

			}
			yield;
		}
	}
}

task HomingShot_S(x,y,angle,damage)
{
	let EnemyObjectArray=GetIntersectionRegistedEnemyID;
	let Dis=1000000;
	let nearObj;
	if(length(EnemyObjectArray)!=0)
	{
		ascent(i in 0..length(EnemyObjectArray))
		{
		let dis=(ObjRender_GetY(EnemyObjectArray[i])-y)^2+(ObjRender_GetX(EnemyObjectArray[i])-x)^2;
		if(dis<Dis)
		{
			nearObj=EnemyObjectArray[i];
		}
		}
	}

	let speed=7;
	let obj=CreatePlayerShotA1(x,y,speed,angle,damage,1,3);
	Obj_SetRenderPriorityI(obj,20);
	let scale=0.4;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	loop(240)
	{
		if(length(EnemyObjectArray)>0)
		{
			let ToPlayerAngle=atan2(ObjRender_GetY(nearObj)-ObjRender_GetY(obj),ObjRender_GetX(nearObj)-ObjRender_GetX(obj));
			let judgangle=sin(ToPlayerAngle-angle);
	//		let ShiftAngle=absolute(ToPlayerAngle-angle);

			if(judgangle>0)
			{
				angle+=6.0;
			}
			else
			{
				angle-=6.0;
			}
			ObjMove_SetAngle(obj,angle);
		}
		yield;
	}
	while(!Obj_IsDeleted(obj))
	{
		yield;
	}
	Obj_Delete(obj);
}

task HomingShot_L(x,y,angle,damage)
{
	let EnemyObjectArray=GetIntersectionRegistedEnemyID;
	let Dis=1000000;
	let nearObj;
	if(length(EnemyObjectArray)!=0)
	{
		ascent(i in 0..length(EnemyObjectArray))
		{
			let dis=(ObjRender_GetY(EnemyObjectArray[i])-y)^2+(ObjRender_GetX(EnemyObjectArray[i])-x)^2;
			if(dis<Dis)
			{
				nearObj=EnemyObjectArray[i];
			}
		}
	}

	let speed=10;
	let obj=CreatePlayerShotA1(x,y,speed,angle,damage,1,3);
	Obj_SetRenderPriorityI(obj,20);
	let scale=0.8;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	loop(120)
	{
		if(length(EnemyObjectArray)>0)
		{
			let ToPlayerAngle=atan2(ObjRender_GetY(nearObj)-ObjRender_GetY(obj),ObjRender_GetX(nearObj)-ObjRender_GetX(obj));
			let judgangle=sin(ToPlayerAngle-angle);
		//	let ShiftAngle=absolute(ToPlayerAngle-angle);

			if(judgangle>0)
			{
				angle+=1.6;
			}
			else
			{
				angle-=1.6;
			}
			ObjMove_SetAngle(obj,angle);
		}

		yield;
	}
	while(!Obj_IsDeleted(obj))
	{
		yield;
	}
	Obj_Delete(obj);
}



