//博麗霊夢Ａ
#東方弾幕風[Player]
#ScriptVersion[3]	//スクリプトバージョン3が必須です
#ID["YabuBakura"]
#Title["Yabusame and Tsubakura"]
#Text["Thanks Junky [r] for allowing us to use this!"]
#ReplayName["YabTsub"]
#Image["IMG_Cut"]

let objPlayer = GetPlayerObjectID();
let PlayerPower=500;
let PlayerPower_Int=PlayerPower/100;
let objOption =[];
let OptionX=[GetPlayerX,GetPlayerX,GetPlayerX,GetPlayerX,GetPlayerX,GetPlayerX];
let OptionY=[GetPlayerY,GetPlayerY,GetPlayerY,GetPlayerY,GetPlayerY,GetPlayerY];
let RodFlag=false;
let RodFlag2=false;
let RodX;
let RodY;
let RodX2;
let RodY2;
let SpellMove=0;
let RebirthFlag=false;

let OnSpell=false;
let InvincibleSystem=true;
let AutoBombFlag=false;
let InvincibleFlag=false;
let InvincibleCount=0;

let FlashMax=50;
let FlashCharge=0;
let FlashAlready=0;
let FlashBool = false;

let xsp = 0;
let ysp = 0;

let current = GetCurrentScriptDirectory();

#include "./Option.txt"
#include "./Shot.txt"
#include "./Spell.txt"

#include "./../../Common/lib/Player.txt"
#include "./../../Common/lib/Effect.txt"
#include "./../../Common/lib/Invincible.txt"
#include "./../../Common/lib/Library.txt"
#include "./../../Common/lib/Load.txt"
#include "./../../Common/lib/Config.txt"
#include "./../../Common/lib/file.txt"

IMG_PLAYER=IMG_YABUSAME;
IMG_OPTION=IMG_YABUSAME_OPTION;
let PlayerType = 1;
@Initialize
{
	SetCommonData("PlayerIconR",255);
	SetCommonData("PlayerIconG",255);
	SetCommonData("PlayerIconB",255);
	
	LoadTexture(IMG_YABUSAME);
	LoadTexture(IMG_YABUSAME_OPTION);
	LoadTexture(IMG_YABUSAME_BOMNAME);
	LoadTexture(IMG_YABUSAME_FLASHNAME);
	LoadTexture(IMG_TSUBAKURA);
	LoadTexture(IMG_TSUBAKURA_OPTION);
	LoadTexture(IMG_TSUBAKURA_LASER);
	LoadTexture(IMG_TSUBAKURA_BOMNAME);
	LoadTexture(IMG_TSUBAKURA_FLASHNAME);
	LoadTexture(IMG_PIXEL);
	LoadTexture(IMG_FLASHBAR);
	
	let path = current ~ "../../Common/lib/ShotData.txt";
	LoadPlayerShotData(path); //自弾画像ロード

	ObjPlayer_AddIntersectionCircleA1(objPlayer, 0, 0, 0.2, 27); //当たり判定登録
	SetPlayerSpeed(3.25,1.8);

	TImage();//自機描画用タスク起動
	OptionMove;//オプション移動用タスク
	TRenderOption;//オプション描画用タスク
	TShot();//弾発射用タスク起動

	FlashBomb;
	TShot2();
	SlowEffect;//低速魔方陣エフェクト用タスク
	InvincibleSystemTask;//特殊無敵用タスク
	SetPlayerRebirthFrame(18);
	SetPlayerRebirthLossFrame(0);
	SetPlayerDownStateFrame(40);
	SoundLoad;
}
task RebirthSound(deley)
{
	loop(deley){yield;}
	PlaySE(SE_BARRIER);
	loop(14)
	{
		BarrierDanger(0);
		yield;
	}
}
task ShootDown
{
	loop(240)
	{
		SetCommonData("PlayerIconR",64);
		SetCommonData("PlayerIconG",64);
		SetCommonData("PlayerIconB",64);
		if(PlayerType==1)
		{
			SetCommonData("PlayerIcon",4+1);
		}
		if(PlayerType==-1)
		{
			SetCommonData("PlayerIcon",1+1);
		}
		yield;
	}
	SetCommonData("PlayerIconR",255);
	SetCommonData("PlayerIconG",255);
	SetCommonData("PlayerIconB",255);
}
@MainLoop
{
	
	if(PlayerType==1)
	{
		if(OnSpell)
		{
			SetCommonData("PlayerIcon",4+2);
		}
		else
		{
			SetCommonData("PlayerIcon",4);
		}
	}
	if(PlayerType==-1)
	{
		if(OnSpell)
		{
			SetCommonData("PlayerIcon",1+2);
		}
		else
		{
			SetCommonData("PlayerIcon",1);
		}
	}
	if(GetPlayerState==STATE_HIT)
	{
		SetCommonData("PlayerIconR",255);
		SetCommonData("PlayerIconG",64);
		SetCommonData("PlayerIconB",64);
		if(PlayerType==1)
		{
			SetCommonData("PlayerIcon",4+1);
		}
		if(PlayerType==-1)
		{
			SetCommonData("PlayerIcon",1+1);
		}
	}
	if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH||GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD)
	{
		CollectItemsInCircle(ObjMove_GetX(GetPlayerObjectID), ObjMove_GetY(GetPlayerObjectID), 100);
	}
	if(GetPlayerState==STATE_DOWN)
	{
		CancelCollectItems;
	}
	if(GetVirtualKeyState(VK_USER1)==KEY_PUSH)
	{
		PlayerType*=-1;
		OptionX=[GetPlayerX,GetPlayerX,GetPlayerX,GetPlayerX,GetPlayerX,GetPlayerX];
		OptionY=[GetPlayerY,GetPlayerY,GetPlayerY,GetPlayerY,GetPlayerY,GetPlayerY];
	}
	if(PlayerType==1)
	{
		SetPlayerSpeed(3.25,1.8);
		IMG_PLAYER=IMG_YABUSAME;
		IMG_OPTION=IMG_YABUSAME_OPTION;
	}
	else
	{
		SetPlayerSpeed(5.5,3.2);
		IMG_PLAYER=IMG_TSUBAKURA;
		IMG_OPTION=IMG_TSUBAKURA_OPTION;
	}
	yield;
}
task FlashBomb
{

	
	let objC = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(objC,IMG_PIXEL);
	ObjPrim_SetPrimitiveType(objC, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(objC, 4);
	Obj_SetRenderPriority(objC,1.95);
	ObjPrim_SetDestCenter(objC,0,0,6,340-8);
	ObjRender_SetPosition(objC,15,310,0);
	//ObjRender_SetColor(objC,255,0,0);
	ObjRender_SetScaleXYZ(objC,0.8,0.8,1);
	
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(obj,IMG_FLASHBAR);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriority(obj,2);
	ObjPrim_SetDestCenter(obj,0,0,22,340);
	ObjRender_SetPosition(obj,15,310,0);
	ObjRender_SetScaleXYZ(obj,0.8,0.8,1);
	let cred = 0;
	FlashAlreadyHandle;
	loop
	{
		ObjRender_SetScaleXYZ(objC,0.8,0.8/(FlashMax)*FlashCharge,1);
		ObjRender_SetPosition(objC,15,310+(((340-8)/2*0.8)/FlashMax)*(FlashMax-FlashCharge),0);
		if(FlashCharge+FlashAlready==FlashMax+FlashAlready)
		{
			let red = (128*0.8)+(128*0.8)*cos(cred);
			ObjRender_SetColor(objC,255*0.8,red,red);
		}
		else
		{
			ObjRender_SetColor(objC,255,255,255);
		}
		cred+=8;
		if(FlashCharge+FlashAlready>=FlashMax+FlashAlready)
		{
			if(GetVirtualKeyState(VK_BOMB) == KEY_PUSH)
			{
				let bomb=GetPlayerSpell;
				cred = 0;
				if((GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD) )
				{
				}
				else
				{
					PlaySE(SE_FLASHBOMB);
					FlashTask(GetPlayerX,GetPlayerY);
					FlashBool = false;
					
					SetPlayerInvincibilityFrame_Special(60);
				}
			}
		}
		yield;
	}
	task FlashAlreadyHandle
	{
		loop
		{
			while(FlashCharge-FlashAlready<FlashMax-FlashAlready)
			{
				FlashCharge = GetGraze-FlashAlready;
				yield;
			}
			FlashCharge=FlashMax;
			PlaySE(SE_FLASHCRAGE);
			FlashBool = true;
			while(FlashBool==true)
			{
				yield;
			}		
			loop(60)
			{
				FlashCharge -= FlashMax/60;
				yield;
			}
			FlashAlready=GetGraze;
			FlashCharge = 0;
			
		}
	}
}
task FlashTask(x,y)
{
	let angleT = -90;
	if(PlayerType==1)
	{
		ShowBombName(IMG_YABUSAME_FLASHNAME);
		x = 0;
		y = 0;
		let am = 0.5;
		loop(2)
		{
			let obj = CreateStraightLaserA1(x-(GetStgFrameWidth*2)*cos(angleT), y-(GetStgFrameWidth*2)*sin(angleT),angleT, GetStgFrameWidth*4, 30*am,100, 1, 0);
			Grow(obj,1*am,10,30);
			ObjShot_SetDamage(obj, 0);
			ObjShot_SetEraseShot(obj, true);
			ObjShot_SetPenetration(obj, 1000000);
			ObjRender_SetBlendType(obj,BLEND_INV_DESTRGB);
			//ObjRender_SetAlpha(obj,0);
			ObjLaser_SetInvalidLength(obj,0,0);
			ObjCrLaser_SetTipDecrement(obj,0);
			ObjShot_SetAutoDelete(obj,false);
			
			angleT+=90;
		}
		x = GetStgFrameWidth;
		y = GetStgFrameHeight;
		loop(2)
		{
			let obj = CreateStraightLaserA1(x-(GetStgFrameWidth*2)*cos(angleT), y-(GetStgFrameWidth*2)*sin(angleT),angleT, GetStgFrameWidth*4, 30*am,100, 1, 0);
			Grow(obj,1*am,10,30);
			ObjShot_SetDamage(obj, 0);
			ObjShot_SetEraseShot(obj, true);
			ObjShot_SetPenetration(obj, 1000000);
			ObjRender_SetBlendType(obj,BLEND_INV_DESTRGB);
			//ObjRender_SetAlpha(obj,0);
			ObjLaser_SetInvalidLength(obj,0,0);
			ObjCrLaser_SetTipDecrement(obj,0);
			ObjShot_SetAutoDelete(obj,false);
			
			angleT+=90;
		}
		x = GetStgFrameWidth/2;
		y = GetStgFrameHeight/2;
		loop(2)
		{
			let obj = CreateStraightLaserA1(x-(GetStgFrameWidth*2)*cos(angleT), y-(GetStgFrameWidth*2)*sin(angleT),angleT, GetStgFrameWidth*4, 30*am,100, 1, 0);
			Grow(obj,1*am,10,30);
			ObjShot_SetDamage(obj, 0);
			ObjShot_SetEraseShot(obj, true);
			ObjShot_SetPenetration(obj, 1000000);
			ObjRender_SetBlendType(obj,BLEND_INV_DESTRGB);
			//ObjRender_SetAlpha(obj,0);
			ObjLaser_SetInvalidLength(obj,0,0);
			ObjCrLaser_SetTipDecrement(obj,0);
			ObjShot_SetAutoDelete(obj,false);
			
			angleT+=90;
		}
		loop(1)
		{
			if(PlayerType==1)
			{
				if(GetPlayerX==0)
				{
					ObjMove_SetX(GetPlayerObjectID,GetStgFrameWidth-1);
				}
				if(GetPlayerX==GetStgFrameWidth)
				{
					ObjMove_SetX(GetPlayerObjectID,1);
				}
				if(GetPlayerY==0)
				{
					ObjMove_SetY(GetPlayerObjectID,GetStgFrameHeight-1);
				}
				if(GetPlayerY==GetStgFrameHeight)
				{
					ObjMove_SetY(GetPlayerObjectID,1);
				}
			}
			yield;
		}
	}
	else
	{
		ShowBombName(IMG_TSUBAKURA_FLASHNAME);
		let am = 4;
		loop(2)
		{
			let obj = CreateStraightLaserA1(x-(GetStgFrameWidth*2)*cos(angleT), y-(GetStgFrameWidth*2)*sin(angleT),angleT, GetStgFrameWidth*4, 30*am,100, 152, 0);
			Grow(obj,am,10,30);
			am = 0.5;
			ObjShot_SetDamage(obj, 0);
			ObjShot_SetEraseShot(obj, true);
			ObjShot_SetPenetration(obj, 1000000);
			ObjRender_SetBlendType(obj,BLEND_SUBTRACT);
			//ObjRender_SetAlpha(obj,0);
			ObjLaser_SetInvalidLength(obj,0,0);
			ObjCrLaser_SetTipDecrement(obj,0);
			ObjShot_SetAutoDelete(obj,false);
			
			angleT+=90;
		}
	}
	task Grow(obj,sclz,stay,downtime)
	{
		Obj_SetRenderPriorityI(obj,43);
		loop(stay){yield;}
		let scl = 30;
		let Alph = 255;
		loop(downtime)
		{
			ObjRender_SetAlpha(obj,Alph);
			ObjLaser_SetRenderWidth(obj,scl*sclz);
			ObjLaser_SetIntersectionWidth(obj,scl*sclz);
			scl-=30/downtime;
			Alph-=255/downtime;
			yield;
		}
		Obj_Delete(obj);
	}
	task ShowBombName(IMAGE)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj,IMAGE);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		Obj_SetRenderPriorityI(obj,52);
		ObjPrim_SetDestCenter(obj,0,0,510,80);
		let scale = 1;
		let alpha = 0;
		ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight-(80/(1.75*2)),0);
		loop(5)
		{
			ObjRender_SetScaleXYZ(obj,scale/1.75,scale/1.75,scale);
			ObjRender_SetAlpha(obj,alpha);
			alpha+=255/5;
			yield;
		}
		ObjRender_SetAlpha(obj,alpha);
		loop(50){yield;}
		loop(5)
		{
			ObjRender_SetScaleXYZ(obj,scale/1.75,scale/1.75,scale);
			ObjRender_SetAlpha(obj,alpha);
			alpha-=255/5;
			yield;
		}
		Obj_Delete(obj);
	}
}

@Finalize
{
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_SPELL)
	{
		//スペルカード要求
		let spell = GetPlayerSpell();//残りスペル数
		if((spell >= 1 && !RebirthFlag&&(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)) || AutoBombFlag)
		{
			SetScriptResult(true);//スペル発動可能
			if(!AutoBombFlag || GetVirtualKeyState(VK_BOMB)==KEY_PUSH && !OnSpell&&InvincibleCount==0)
			{
				SetPlayerSpell(spell-1);//スペル数を1減らす
				TSpell();//スペルタスク起動
				
			}
			else
			{
				DummySpell();//スペルタスク起動
			}
		}

		else
		{
			SetScriptResult(false);//スペル発動不可
		}
		AutoBombFlag=false;
	}
	case(EV_HIT)
	{
		//被弾
		
		xsp = GetPlayerX;
		ysp = GetPlayerY;
		if(InvincibleCount>0)
		{
			AutoBombFlag=true;
			CallSpell;
		}
		else
		{
			TExplosion();
		}
	}
	
	case(EV_PLAYER_REBIRTH)
	{
		//復帰
		ShootDown;
		ObjMove_SetPosition(GetPlayerObjectID,xsp,ysp);
		SetPlayerSpell(2);
		SetPlayerInvincibilityFrame_Special(240);
		RebirthSound(240);
	}
	case(EV_GET_ITEM)
	{
		//アイテム取得通知
		let itemType = GetEventArgument(0);
		alternative(itemType)
		
		case(ITEM_1UP)
		{
			WriteLog("ITEM_1UP");
			PlaySE(LIFEGET);
		}
		case(ITEM_1UP_S)
		{
			WriteLog("ITEM_SPELL_S");
			PlaySE(LIFEGET);
		}
		
		case(ITEM_SPELL)
		{
			WriteLog("ITEM_SPELL");
			PlaySE(SPELLGET);
		}
		case(ITEM_SPELL_S)
		{
			WriteLog("ITEM_SPELL_S");
			PlaySE(SPELLGET);
		}
		case(ITEM_POWER)
		{
			WriteLog("ITEM_POWER");
			PlaySE(ITEMGET);
		}
		case(ITEM_POWER_S)
		{
			WriteLog("ITEM_POWER_S");
			PlaySE(ITEMGET);
		}
		case(ITEM_POINT)
		{
			WriteLog("ITEM_POINT");
			PlaySE(ITEMGET);
		}
		case(ITEM_POINT_S)
		{
			WriteLog("ITEM_POINT_S");
			PlaySE(ITEMGET);
		}
	}
	case(EV_GRAZE)
	{
		//グレイズ通知
		
        PlaySE(SE_GRAZE);
		
	}
}

