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

#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 ~ "MarisaShotData.txt";
	LoadPlayerShotData(path); //自弾画像ロード

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

	TImage(GetCurrentScriptDirectory() ~ "img/STG_Player11.png");//自機描画用タスク起動
	OptionMove;
	TRenderOption(GetCurrentScriptDirectory() ~ "img/STG_Player11.png");
	TShot();//弾発射用タスク起動
	TLaser(0);
	TLaser(1);
	TLaserEffect(0);
	TLaserEffect(1);
	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=[32,-32];
	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;
	}
}

let MainShotCount=0;//弾連射速度調整用変数

task TShot
{
	let pShot=false;
	ShotCheck;
	let Speed=12.5;
	let Damage=1.0;
	loop
	{//単位秒威力（遠）1.0＊5＊12＝60　単位秒威力（遠）1.0＊5＊15＝75　単位秒威力（遠）1.0＊5＊15＝100
		while(!pShot){yield;}
		if(IsPlayerSpellActive){Damage=0.5;}
		else{Damage=1.0;}

		MainShot(GetPlayerX()+8*cos(270), GetPlayerY()-8, Speed, 270, Damage*1.5, 1, 1);
		MainShot(GetPlayerX()+8*cos(264), GetPlayerY()-8, Speed, 266, Damage, 1, 1);
		MainShot(GetPlayerX()+8*cos(258), GetPlayerY()-8, Speed, 274, Damage, 1, 1);
		MainShot(GetPlayerX()+8*cos(276), GetPlayerY()-8, Speed, 262, Damage*0.75, 1, 1);
		MainShot(GetPlayerX()+8*cos(282), GetPlayerY()-8, Speed, 278, Damage*0.75, 1, 1);

		loop(3){yield;}
		if(MainShotCount>=10)
		{
			loop(1){yield;}
		}
		if(MainShotCount>=15)
		{
			loop(1){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,angel,damage,pene,id)
	{
		let obj=CreatePlayerShotA1(x,y,speed,angel,damage,pene,id);
		MainShotCount+=1;
		let scale=0.25;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		while(!Obj_IsDeleted(obj)){yield;}
		MainShotCount-=1;
	}
}

task TLaser(num)
{
	let obj = ObjShot_Create(OBJ_STRAIGHT_LASER);
	ObjStLaser_SetSource(obj, false);
	ObjMove_SetX(obj, ObjRender_GetX(objOption[num]));//x座標設定
	ObjMove_SetY(obj, ObjRender_GetY(objOption[num]));//y座標設定;

	let LaserLeng=0;
	let LaserDamage=1.65;
	ObjShot_SetGraphic(obj, 2);//画像設定
	ObjShot_SetDamage(obj, LaserDamage);//ダメージを設定
	ObjShot_SetPenetration(obj, 10000);//貫通力は適当に大きく
	ObjStLaser_SetAngle(obj, 270);
	ObjLaser_SetLength(obj,LaserLeng);
	ObjLaser_SetRenderWidth(obj,10);
	ObjLaser_SetIntersectionWidth(obj,0);
	ObjLaser_SetInvalidLength(obj, 0, 0);
	ObjShot_Regist(obj);
	while(!Obj_IsDeleted(obj))
	{//単位秒威力＝1.65＊2＊60＝198
		ObjMove_SetX(obj, ObjRender_GetX(objOption[num]));//x座標設定
		ObjMove_SetY(obj, ObjRender_GetY(objOption[num]));//y座標設定;
		ObjLaser_SetLength(obj,LaserLeng);

		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot) 
		{
			LaserLeng+=15;
			if(IsPlayerSpellActive){ObjShot_SetDamage(obj, LaserDamage/2);}
			else{ObjShot_SetDamage(obj, LaserDamage);}
		}
		else
		{
			LaserLeng=0;
			ObjShot_SetDamage(obj, 0);
		}
		yield;
	}

}

task TLaserEffect(num)
{//レーザーの雷？エフェクト
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../Common/Marisa_shots.png");
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,30);
	ObjRender_SetAngleZ(obj,180);

	let Alpha=0.3;
	ObjRender_SetColor(obj,200*Alpha,200*Alpha,255*Alpha);

	ObjPrim_SetVertexPosition(obj, 0, -12, 0, -0);
	ObjPrim_SetVertexPosition(obj, 1, 12, 0, -0);
	ObjPrim_SetVertexPosition(obj, 2, 12, 512, 0);
	ObjPrim_SetVertexPosition(obj, 3, -12, 512, 0);

	ObjPrim_SetVertexUVT(obj, 0, 136, 0);
	ObjPrim_SetVertexUVT(obj, 1, 160,0);
	ObjPrim_SetVertexUVT(obj, 2, 160, 512);
	ObjPrim_SetVertexUVT(obj, 3, 136, 512);

	let LaserLeng=0;
	let LaserPhase=0;

	while(!Obj_IsDeleted(obj))
	{
		ObjRender_SetX(obj, ObjRender_GetX(objOption[num]));//x座標設定
		ObjRender_SetY(obj, ObjRender_GetY(objOption[num]));//y座標設定;

		if((GetVirtualKeyState(VK_SHOT)==KEY_PUSH || GetVirtualKeyState(VK_SHOT)==KEY_HOLD) && GetPlayerState==STATE_NORMAL && IsPermitPlayerShot) 
		{
			LaserLeng+=15*2;
		}
		else
		{
			LaserLeng=0;
		}
		ObjPrim_SetVertexPosition(obj, 0, -12, 0, -0);
		ObjPrim_SetVertexPosition(obj, 1, 12, 0, -0);
		ObjPrim_SetVertexPosition(obj, 2, 12, LaserLeng, 0);
		ObjPrim_SetVertexPosition(obj, 3, -12, LaserLeng, 0);

		ObjPrim_SetVertexUVT(obj, 0, 136, LaserPhase);
		ObjPrim_SetVertexUVT(obj, 1, 160,LaserPhase);
		ObjPrim_SetVertexUVT(obj, 2, 160, LaserLeng+LaserPhase);
		ObjPrim_SetVertexUVT(obj, 3, 136, LaserLeng+LaserPhase);
		yield;
		LaserPhase+=10;
		yield;
	}
	Obj_Delete(obj);



}
