#include "../system/Sound.txt"
#include "../system/functions.txt"
#include "./player_flash.txt"
#include "./player_spell.txt"
#include "./Shot_Const.txt"

let PlayerDie = false;
let PlayerID_1 = GetCommonData("PLAYER_1",0);
let PlayerID_2 = GetCommonData("PLAYER_2",0);
let PlayerID_3 = GetCommonData("PLAYER_3",0);
let PlayerPath= GetCurrentScriptDirectory ~ "pl" ~ IntToString(PlayerID_1) ~ "/Sprite.png";
let PlayerDead = false;
let HitBox = 1.66;
let GrazeBox = 12.5;
let DefaultBomb = 0;
let PlayerSpeed = [4,2];

let barrierCap = 100;
let healthCap = 100;

let ShotPermit = true;
let SpellPermit = true;
let MovePermit = true;
let HitboxForce = false;
let IsDeathBomb = false;
let IsHit = false;
let IsMoving = false;
let IsShooting = false;
let IsFocus = false;
let IsRebirth = false;
let SpellSwitch = true;
let IsFocusForce = false;
let IsUnfocusForce = false;
let GrazeEnable = true;
let GrazeMultiply = 0.8;
let IsArmorMode = false;
let IsArmorActive = false;
let shotDmgRateM = 1;
let shotDmgRate = shotDmgRateM;
let IsHitEnable = true;
let EVGraze = 0;

@Initialize
{
	LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "../system/data/CONFIG");
	LoadCommonDataAreaA2("VOLUME",GetCurrentScriptDirectory ~ "../system/data/VOLUME.dat");

	LoadTexture(GetCurrentScriptDirectory ~ "pl0/Sprite.png");
	LoadTexture(GetCurrentScriptDirectory ~ "pl1/Sprite.png");
	LoadTexture(GetCurrentScriptDirectory ~ "pl2/Sprite.png");

	if(PlayerID_2 == 2)
		{
		IsArmorMode = true;
		}

	barrierCap = trunc(barrierCap);
	shotDmgRateM = 1;	
	healthCap = trunc(healthCap);

	ObjMove_SetPosition(objPlayer,GetStgFrameWidth/2,GetStgFrameHeight-30);
	SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);	
	SetPlayerRebirthLossFrame(0);
	SetPlayerLife(100);
		
	let lnum = 1000;
	Obj_SetValue(objPlayer,"PLAYER_LIFE",GetCommonData("PLAYER_LIFE",lnum*healthCap-1));
	SetPlayerFlash(GetCommonData("PLAYER_FLASH",0));
	let bnum = 3;
	SetPlayerBarrier(bnum*barrierCap-1);
	SetPlayerPower(5);
	TInitializeBombCount;
	SetPlayerSpell(DefaultBomb);

	TMain;	
	
	SetPlayerSpeed(0,0);
	CreatePlayerHitBox;
	TPlayerResourceRender;
	Obj_SetValue(objPlayer,"PLAYER_PATH",PlayerPath);
	TImage;
	TShot;

	SetAutoDeleteObject(true);
	SetPlayerFlash(199);
	NotifyEvent(GetOwnScriptID,EV_USER_GRAZE,0);
}

@MainLoop
{	
	shotDmgRate = shotDmgRateM;
	if(GetPlayerSpell > 5)
		{
		SetPlayerSpell(5);
		}
	if(GetPlayerPower > 5)
		{
		SetPlayerPower(5);
		}	
	if(Obj_GetValueD(objPlayer,"GRAZE",0) > 0)
		{
		loop(Obj_GetValueD(objPlayer,"GRAZE",0))
			{
			NotifyEvent(GetOwnScriptID,EV_GRAZE,NULL);
			}
		Obj_SetValue(objPlayer,"GRAZE",0);
		}
	if(GetCommonData("FLASH_KEY",0) == 1 && GetVirtualKeyState(VK_USER2) == KEY_PUSH)
		{
		TFlashKey;
		}
	if(PlayerID_3 != GetCommonData("PLAYER_3",0))
		{
		PlayerID_1 = GetCommonData("PLAYER_1",0);
		PlayerID_2 = GetCommonData("PLAYER_2",0);
		PlayerID_3 = GetCommonData("PLAYER_3",0);
		PlayerPath = GetCurrentScriptDirectory ~ "pl" ~ IntToString(PlayerID_3) ~ "/Sprite.png";
		if(PlayerID_3 == 1)
			{
			ObjRender_SetScaleXYZ(objPlayer,1.125,1.125,0);
			}
		else		
			{
			ObjRender_SetScaleXYZ(objPlayer,1,1,0);
			}
		}
	yield;
}

@Event
{
	alternative(GetEventType())	
	case(EV_PLAYER_SHOT)
		{
		let ev = GetEventArgument(0);
		ascent(i in 0..length(ev))
			{
			let SD = ev[i];
			if(length(SD) > 0)
				{
				CreatePlayerBulletA1(SD[0],SD[1],SD[2],SD[3],SD[5],SD[6],SD[4]);
				}
			}
		}
	case(EV_REQUEST_SPELL)
		{
		let spell = GetPlayerSpell();
		if(GetCommonData("FLASH_KEY",0) == 1 || (((GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD) && IsUnfocusForce == false) || IsFocusForce == true))
			{
			if(GetPlayerSpell > 0 && (((GetPlayerInvincibilityFrame == 0 || IsDeathBomb == true) && IsHit == false) || IsRebirth == true) && GetPlayerSpellEnable == true && SpellPermit == true && IsFlashActive == false)
				{
				if(GetPlayerSpell <= 0)
					{
					SetPlayerPower(floor(GetPlayerPower)-1);
					}
				SetScriptResult(true);
				TInitializeSpell;
				}
			else
				{
				SetScriptResult(false);
				}
			}
		else
			{
			TFlashKey;
			SetScriptResult(false); 
			}
		}
	case(EV_PLAYER_REBIRTH)
		{
		SetPlayerSpell(3);
		SetPlayerInvincibilityFrame(135);
		}
	case(EV_GET_ITEM)
		{
		let itemType = GetEventArgument(0);
		alternative(itemType)
		case(ITEM_SPELL)
			{
			//WriteLog("ITEM_SPELL");
			}
		case(ITEM_SPELL_S)
			{
			//WriteLog("ITEM_SPELL_S");
			}
		case(ITEM_POWER)
			{
			//WriteLog("ITEM_POWER");
			}
		case(ITEM_POWER_S)
			{
			//WriteLog("ITEM_POWER_S");
			}
		}	
	case(EV_GRAZE)
		{
		NotifyEvent(GetOwnScriptID,EV_USER_GRAZE,[1]);
		}
	case(EV_USER_GRAZE)
		{
		let ev = GetEventArgument(0);
		let g = 1;
		if(length(ev) > 0)
			{
			g = ev[0];
			}
		PlaySound("se_Graze",100);
		EVGraze+=g;
		if(EVGraze >= 1)
			{
			AddGraze(trunc(EVGraze));
			EVGraze = 0;
			}
		let flash = GetPlayerFlash;
		SetPlayerFlash(min(200,flash+g*5*GrazeMultiply));
		if(GetPlayerFlash >= 200 && flash < 200)
			{
			PlaySound("se_Boost2",120);
			}
		}
}

task TImage
{
	ObjPrim_SetTexture(objPlayer,PlayerPath);
	ObjSprite2D_SetDestRect(objPlayer, -16+0.75, -24.5+1, 16+0.75, 24.5+1);	
	Obj_SetRenderPriority(objPlayer,0.37);

	let ImageWidth = GetTextureWidth(PlayerPath)/8;
	let ImageHeight = 145/8;

	let anim = 0;
	let animm = 0;
	let move = 0;
	let obj = [ObjPrim_Create(OBJ_SPRITE_2D),ObjPrim_Create(OBJ_SPRITE_2D)];
	ascent(i in 0..2)
		{
		ObjPrim_SetTexture(obj[i], GetCurrentScriptDirectory ~ "../system/img/ZUN_Effect.png");
		ObjSprite2D_SetSourceRect(obj[i], i*64, 16, i*64+63, 79);	
		Obj_SetRenderPriority(obj[i],0.59-i*0.2);
		}
	ObjRender_SetBlendType(obj[1],BLEND_ADD_ARGB);
	let angle = 0;	
	let scale = 4;
	let alpha = 0;
	let dirX = 0;
	let dirY = 0;
	let dirZ = 0;

	let counter = 0;
	let path = PlayerPath;

	let num = 0;
	if(GetCommonData("PLAYER_1",0) == 9)
		{
		num = -2;
		}

	while(PlayerDead == false)
		{	
		IsMoving = false;
		dirX = 0;
		dirY = 0;
		dirZ = 0;
		counter+=1;
		let clip = GetPlayerClip;
		let speed = PlayerSpeed[0];
		let flip = IsEvent("EVE_FLIP");
		if(path != PlayerPath)
			{
			ObjPrim_SetTexture(objPlayer,PlayerPath);
			path = PlayerPath;
			}
		ImageWidth = GetTextureWidth(path)/8;
		if((GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD) || HitboxForce == true)
			{	
			if(scale > 1)
				{
				scale-=1/10;
				alpha+=255/10;
				}
			else
				{
				scale = 1;
				alpha = 255;
				angle+=3;
				}	
			speed = PlayerSpeed[1];
			}
		else
			{
			if(scale < 2)
				{
				scale+=1/10;
				alpha-=255/10;
				angle+=3;
				}
			else
				{
				scale = 2;
				alpha = 0;
				}
			}
		if(MovePermit == true && (((GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD) && flip == 0) || ((GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD) && flip == 1)))
			{
			anim = 0;
			if(move < 3)
				{
				move+=0.5;
				}
			else
				{
				animm += 0.25;
				move = 3;
				}
			IsMoving = true;
			dirX = -1;
			dirZ += 1;
			ObjSprite2D_SetSourceRect(objPlayer, floor(move)*ImageWidth+floor(animm)%4*ImageWidth, 48, floor(move+1)*ImageWidth+floor(animm)%4*ImageWidth, 95);
			}
		else if(MovePermit == true && (((GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD) && flip == 0) || ((GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD) && flip == 1)))
			{
			anim = 0;
			if(move < 3)
				{
				move+=0.5;
				}
			else
				{
				animm += 0.33;
				move = 3;
				}
			IsMoving = true;
			dirX = 1;
			dirZ += 1;
			ObjSprite2D_SetSourceRect(objPlayer, floor(move)*ImageWidth+floor(animm)%4*ImageWidth, 96, floor(move+1)*ImageWidth+floor(animm)%4*ImageWidth+num, 143);
			}
		else
			{
			move = 0;
			animm = 0;
			anim+=0.25;
			ObjSprite2D_SetSourceRect(objPlayer, floor(anim)%8*ImageWidth+1, 1, (1+floor(anim)%8)*ImageWidth+num, 48);
			}
		if(MovePermit == true && (GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD))
			{	
			IsMoving = true;
			dirY = 1;
			dirZ+=1;
			}
		else if(MovePermit == true && (GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD))
			{	
			IsMoving = true;
			dirY = -1;
			dirZ+=1;
			}
		if(IsMoving == true)
			{
			ObjMove_SetPosition(objPlayer,min(clip[2],max(clip[0],GetPlayerX+dirX*speed*sin(90/dirZ))),min(clip[3],max(clip[1],GetPlayerY+dirY*speed*sin(90/dirZ))));
			}
		ObjRender_SetPosition(objPlayer,GetPlayerX,GetPlayerY,0);
		ObjRender_SetScaleXYZ(obj[0],0.75+HitBox/9,0.75+HitBox/8,0);
		ascent(i in 0..2)
			{
			ObjRender_SetAngleZ(obj[i],(i*2-1)*angle);
			ObjRender_SetPosition(obj[i],GetPlayerX,GetPlayerY,0);
			ObjSprite2D_SetDestCenter(obj[i]);
			if(GetPlayerState == STATE_NORMAL && Obj_IsVisible(objPlayer) == true)
				{
				ObjRender_SetAlpha(obj[i],alpha/(i*12+1));
				}
			else
				{
				ObjRender_SetAlpha(obj[i],0);
				}
			}
		yield;
		}
	ascent(i in 0..2)
		{
		Obj_Delete(obj[i]);
		}
}

task CreatePlayerHitBox
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(objEnemy);
	Obj_SetValue(objEnemy,"PLAYER",true);
	Obj_SetValue(objEnemy,"STAGE_SKIP_DELETE",true);
	ObjMove_SetPosition(objEnemy,-400,-400);
	ObjEnemy_SetLife(objEnemy,0);
	let hit = HitBox;
	let graze = GrazeBox;
	Obj_SetValue(objPlayer,"PLAYER_HITBOX",hit);
	Obj_SetValue(objPlayer,"PLAYER_GRAZEBOX",graze);
	loop
		{	
		if(PlayerDead == true)
			{
			return;	
			}
		ObjEnemy_SetLife(objEnemy,Obj_GetValueD(objPlayer,"PLAYER_LIFE",0));
		if(GetPlayerLife != 100 && GetPlayerLife >= 0)
			{
			ObjEnemy_SetLife(objEnemy,ObjEnemy_GetInfo(objEnemy,INFO_LIFE)-100+GetPlayerLife);
			SetPlayerLife(100);
			}
		hit = HitBox;
		graze = GrazeBox;
		let dist = 64;
		let damage = Obj_GetValueD(objPlayer,"DAMAGE",0);
		let array = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,dist,TARGET_ENEMY);
		ascent(i in 0..length(array))
			{
			let shot = array[i];
			if(!Obj_IsDeleted(shot) && Obj_GetValueD(shot,"GRAZED",false) == false && ObjShot_GetIntersection(shot) == true && GetObjectDistance(objPlayer,shot) < graze+ObjShot_GetScale(shot)*0.8 && GrazeEnable == true)
				{
				NotifyEvent(GetOwnScriptID,EV_GRAZE,NULL);	
				Obj_SetValue(shot,"GRAZED",true);
				}
			if(!Obj_IsDeleted(shot) && ObjShot_GetIntersection(shot) == true && GetPlayerInvincibilityFrame == 0 && IsHitEnable == true && GetObjectDistance(objPlayer,shot) < dist && GetObjectDistance(objPlayer,shot) <= hit+ObjShot_GetScale(shot)*0.8 && Obj_GetValueD(shot,"LASER",false) == false)
				{
				dist = GetObjectDistance(objPlayer,shot);
				damage = ObjShot_GetStrength(shot);
				}
			}
		if(damage > 0)
			{
			THit(damage);
			Obj_SetValue(objPlayer,"DAMAGE",0);
			}
		Obj_SetValue(objPlayer,"PLAYER_LIFE",ObjEnemy_GetInfo(objEnemy,INFO_LIFE));
		yield;
		}
	function THit(damage)
		{
		let vis = Obj_IsVisible(objPlayer);
		let obj = [];
		let count = 8;
		let die = 0; //0 = nothing; 1 = barrier damage; 2 = health damage; 3 = barrier break; 4 = health break;
		let color = 0;
		let time = 5; //deathbomb time
		let life;	
		let cap;
		if(GetPlayerBarrier >= 0 && IsEvent("EVE_PIERCE") == 1)
			{
			life = GetPlayerBarrier;
			time = 0;
			die = 1;
			color = 240;
			cap = barrierCap;
			}
		else
			{
			life = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			die = 2;
			color = 0;
			cap = healthCap;
			}
		let lifeA = max(floor(life/cap)*cap,life-damage);
		if(lifeA%cap == 0 && die == 2)
			{
			time = 12;
			}
		ascent(i in 0..2)
			{
			obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
			ObjPrim_SetTexture(obj[i],GetCurrentScriptDirectory ~ "../system/img/WhiteSquare.png");
			ObjSprite2D_SetSourceRect(obj[i],0,0,256,256);
			ObjPrim_SetVertexCount(obj[i],count*2+2);
			}
		if(vis == false)
			{
			ascent(i in 0..2)
				{
				Obj_Delete(obj[i]);
				}
			}
		let alpha = 0;
		let scale = 128;
		let angle = rand(0,360);
		let x = GetPlayerX;
		let y = GetPlayerY;
		PlaySound("se_DeathBomb",120);
		SetPlayerClip(x,y,x,y);
		SetPlayerInvincibilityFrame(time+1);
		ShotPermit = false;
		GrazeMultiply = 1/5;
		IsDeathBomb = true;
		let DeathBomb = false;
		loop(time)
			{
			if(IsPlayerSpellActive == true)
				{
				ascent(i in 0..2)
					{
					Obj_Delete(obj[i]);
					}
				SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);
				SpellPermit = true;
				ShotPermit = true;
				return;
				}
			alpha+=255/time;
			scale-=128/time;
			angle-=9;	
			if(vis == true)
				{
				ascent(dir in 0..2)
					{
					ascent(i in 0..count+1)
						{
						ascent(h in 0..2)
							{
							let angleA = i*360/count+(dir*2-1)*(dir+1)*angle;
							let scaleA = scale*(1+dir)*(1-h*0.1);
							let color = 1-h*0.5;
							ObjPrim_SetVertexPosition(obj[dir],i*2+h,x+scaleA*cos(angleA),y+scaleA*sin(angleA),0);
							ObjPrim_SetVertexAlpha(obj[dir],i*2+h,alpha*(1+dir*0.5));
							ObjPrim_SetVertexColor(obj[dir],i*2+h,(205+dir*55)*color,(55)*color,(255-dir*200)*color);
							}
						}
					}
				}
			yield;
			if(IsPlayerSpellActive == true)
				{
				ascent(i in 0..2)
					{
					Obj_Delete(obj[i]);
					}
				SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);
				SpellPermit = true;
				ShotPermit = true;
				return;
				}
			}	
		if(Obj_GetValueD(objPlayer,"REPEAT_SPELL",false) == true)
			{
			ascent(i in 0..2)
				{
				Obj_Delete(obj[i]);
				}
			SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);
			SpellPermit = true;
			ShotPermit = true;
			Obj_SetValue(objPlayer,"REPEAT_SPELL_HIT",true);
			return;
			}		
		SpellPermit = false;
		IsDeathBomb = false;
		IsHit = true;
		Obj_SetValue(objPlayer,"IS_HIT",true);
		NotifyEventAll(EV_PLAYER_SHOOTDOWN,NULL);
		time = 60;
		if(lifeA%cap == 0)
			{
			life = lifeA-1;
			die+=2;
			time = floor(105);
			}
		else
			{
			life = max(lifeA,life-damage);
			}
		ascent(i in 0..2)
			{
			Obj_Delete(obj[i]);
			}	
		TFlash(color,90+floor(die/3)*115,15+floor(die/3)*60);
		if((die == 1 || die == 3) && DeathBomb == false)
			{		
			SetPlayerBarrier(max(-1,life));
			ClearShotCircle;
			}
		else if((die == 2 || die == 4) && DeathBomb == false)
			{		
			ObjEnemy_SetLife(objEnemy,life);
			ClearShotCircle;
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
				{
				PlayerDead = true;
				SetPlayerLife(-1);
				ObjPlayer_AddIntersectionCircleA1(objPlayer,0,0,0,0);
				TDie;
				return;
				}
			}
		if(die >= 3)
			{
			PlaySound("se_PlayerDie",120);
			TDie;
			}
		SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);
		if(DeathBomb == false)
			{
			ShotPermit = true;
			SetPlayerInvincibilityFrame(time+1);
			alpha = 255;
			let dir = 1;
			IsRebirth = true;
			SpellPermit = true;
			ascent(t in 0..time)
				{
				if(t%6 == 0)
					{
					alpha = dir*255/3+255/3;	
					dir*=-1;
					}
				ObjRender_SetColorHSV(objPlayer,color,alpha,255);	
				yield;
				}
			IsRebirth = false;
			if(die >= 3)
				{
				PlaySound("se_Boost",100);
				}
			ObjRender_SetColorHSV(objPlayer,0,0,255);
			}
		SpellPermit = true;
		IsHit = false;
		Obj_SetValue(objPlayer,"IS_HIT",false);
		GrazeMultiply = 1;
	
		function TDie
			{
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			if(vis == true)
				{
				Obj_SetVisible(objPlayer,false);
				ObjPrim_SetTexture(obj, PlayerPath);
				Obj_SetRenderPriority(obj,0.39);
				ObjSprite2D_SetSourceRect(obj, 0, 0, 32, 49);
				ObjSprite2D_SetDestRect(obj, -16, -24.5+2, 16, 24.5+2);
				}
			else
				{
				Obj_Delete(obj);
				}
			let alpha = 255;
			let scale = 1;
			SetPlayerInvincibilityFrame(61);
			loop(30)
				{
				scale += 0.1;
				alpha -= 15;
				if(vis == true)
					{
					ObjRender_SetAlpha(obj,alpha);
					ObjRender_SetPosition(obj, GetPlayerX, GetPlayerY, 0);
					ObjRender_SetScaleXYZ(obj, scale, scale, 1);
					}
				yield;
				}
			let x = GetStgFrameWidth/2;
			let y = GetStgFrameHeight+32;
			ObjRender_SetColorHSV(objPlayer,color,255,255);	
			if(die == 4)			
				{
				SetPlayerSpell(DefaultBomb);
				}
			ascent(t in 0..30)
				{
				y-=64/30;
				SetPlayerClip(x,y,x,y);
				ObjMove_SetPosition(objPlayer,x,y);
				if(t >= 1)
					{
					Obj_SetVisible(objPlayer,vis);	
					}
				yield;
				}
			Obj_Delete(obj);
			}
		}

	task TDeleteShot(shot)
		{
		ObjShot_ScaleDelete(shot,11);
		loop(10){yield;}
		if(Obj_IsDeleted(shot)){return;}
		ObjShot_ToItem(shot);
		}
	task ClearShotCircle
		{
		let array = GetShotIdInCircleA2(GetPlayerX,GetPlayerY,96,TARGET_ENEMY);
		ascent(i in 0..length(array))
			{
			if(!Obj_IsDeleted(array[i]) && ObjShot_IsDeathResist(array[i]) == false)
				{
				TDeleteShot(array[i]);
				}
			}
		}

	task TFlash(color,alpha,time)
		{
		let back = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(back,GetCurrentScriptDirectory ~ "../system/img/WhiteSquare.png");
		ObjSprite2D_SetSourceRect(back,0,0,1,1);
		ObjSprite2D_SetDestRect(back,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);
		ObjRender_SetColorHSV(back,color,255,255);
		ObjRender_SetAlpha(back,205);
		Obj_SetRenderPriority(back,0.65);

		descent(t in 0..time)
			{
			ObjRender_SetAlpha(back,alpha*t/time);
			yield;
			}
		Obj_Delete(back);
		}
}

task TInitializeSpell
{
	GrazeMultiply = 1/5;
	PlaySound("se_Spell",150);
	SetPlayerSpell(GetPlayerSpell-1);
	NotifyEventAll(EV_PLAYER_SHOOTDOWN,NULL);
	alternative(PlayerID_2)
		case(0)
			{
			TMariSpell;
			}
	let time = GetPlayerInvincibilityFrame;
	let alpha = 255;
	let dir = 1;
	ascent(t in 0..time)
		{
		if(t%10 == 0)
			{
			alpha = dir*205/3+205/3;
			ObjRender_SetColorHSV(objPlayer,150,alpha,255);	
			dir*=-1;
			}		
		yield;
		}
	PlaySound("se_Boost",100);
	ObjRender_SetColorHSV(objPlayer,0,0,255);
	GrazeMultiply = 1;
}

task TInitializeFlash
{
	GrazeEnable = false;
	GrazeMultiply = 0;
	TMariFlash;
	PlaySound("se_Flashbomb",100);
	SetPlayerFlash(0);
	loop(GetPlayerInvincibilityFrame){yield;}
	GrazeEnable = true;
	GrazeMultiply = 1;
}

task TInitializeBombCount
{
	alternative(PlayerID_2)
		case(0)
			{
			DefaultBomb = 2;
			}
}

task TPlayerResourceRender
{
	let xRate = 17.5;
	let xDistL = healthCap/100;
	let xDistB = barrierCap/100;
	let yRateL = (GetScreenHeight-GetStgFrameHeight-GetStgFrameTop)/2.75;

	let objL = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objL, GetCurrentScriptDirectory() ~ "../system/img/Default_System.png");
	Obj_SetRenderPriority(objL, 0.18);

	let spellL = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(spellL, GetCurrentScriptDirectory() ~ "../system/img/Default_System.png");
	Obj_SetRenderPriority(spellL,0.19);

//-----------------------------------------

	let remStepRateS = 32;

	let objS = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objS, GetCurrentScriptDirectory() ~ "../system/img/RSS_Item.png");
	Obj_SetRenderPriority(objS, 0.15);

	let spellS = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(spellS, GetCurrentScriptDirectory() ~ "../system/img/RSS_Item.png");
	Obj_SetRenderPriority(spellS, 0.15);

//-----------------------------------------

	let xF = [GetScreenWidth-116,GetScreenWidth-100];
	let yF = [GetScreenHeight/2+20,GetScreenHeight-20];
	let distF = yF[1]-yF[0];
	let objScaleF = [];
	ascent(i in 0..2)
		{
		objScaleF = objScaleF ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(objScaleF[i],GetCurrentScriptDirectory ~ "../system/img/WhiteSquare.png");
		if(i == 1)
			{
			ObjPrim_SetTexture(objScaleF[i],GetCurrentScriptDirectory ~ "../system/img/System_BarF.png");
			}
		ObjRender_SetAlpha(objScaleF[i],255);
		ObjRender_SetColorHSV(objScaleF[i],0,0,100);
		Obj_SetRenderPriority(objScaleF[i],0.11);
		ObjSprite2D_SetSourceRect(objScaleF[i],0,0,16,distF);
		ObjSprite2D_SetDestRect(objScaleF[i],xF[0],yF[0],xF[1],yF[1]);
		Obj_SetRenderPriority(objScaleF[i],0.17+i*0.02);
		}
	let barF = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(barF,GetCurrentScriptDirectory ~ "../system/img/WhiteSquare.png");
	ObjSprite2D_SetSourceRect(barF,0,0,1,1);
	Obj_SetRenderPriority(barF,0.19);
	ObjRender_SetBlendType(barF,BLEND_ADD_ARGB);
	let colorF = 180;
	let sound1F = false;
	let sound2F = false;
	xF[0] = xF[0]+3;
	xF[1] = xF[1]-3;
	yF[0] = yF[0]+3;
	yF[1] = yF[1]-3;
	distF = yF[1]-yF[0];

//-----------------------------------------
	
	loop
		{
		ObjSpriteList2D_ClearVertexCount(objL);
		ObjSpriteList2D_ClearVertexCount(spellL);
		ObjSpriteList2D_ClearVertexCount(objS);
		ObjSpriteList2D_ClearVertexCount(spellS);
		RenderLife();

		yield;
		}

	function RenderLife()
		{
		ObjSpriteList2D_SetSourceRect(objL, 130, 25, 150, 35);
		ObjSpriteList2D_SetSourceRect(spellL, 130, 25, 150, 35);
		
	//--------------------------------------

		let countLifeS = GetPlayerSpell;
		ObjSpriteList2D_SetSourceRect(objS, 32, 0, 64, 32);
		ObjSpriteList2D_SetSourceRect(spellS, 96, 0, 128, 32);
		ascent(iStep in 0 .. 5)
			{
			ObjSpriteList2D_SetDestRect(objS, GetScreenWidth-88, GetScreenHeight-30-(iStep+1) * remStepRateS,GetScreenWidth-90 + remStepRateS, GetScreenHeight-30-(iStep) * remStepRateS);
			ObjRender_SetColor(objS,175,175,175);
			ObjSpriteList2D_AddVertex(objS);
			}
		ascent(iStep in 0 .. min(countLifeS,5))
			{
			ObjSpriteList2D_SetDestRect(spellS, GetScreenWidth-88, GetScreenHeight-30-(iStep+1) * remStepRateS,GetScreenWidth-90 + remStepRateS, GetScreenHeight-30-(iStep) * remStepRateS);
			ObjRender_SetColor(spellS,255,255,255);
			ObjSpriteList2D_AddVertex(spellS);
			}

	//--------------------------------------

		let flash = GetPlayerFlash;
		if(flash == 200)
			{	
			colorF+=1.25;
			if(sound1F == false)
				{
				PlaySound("se_Boost2",100);
				sound1F = true;
				}
			}
		else
			{
			colorF = 180;
			sound1F = false;
			}
		ObjRender_SetColorHSV(barF,0,105+105*cos(colorF),225);
		ObjSprite2D_SetDestRect(barF,xF[0],yF[1]-distF*flash/200,xF[1],yF[1]);
	//--------------------------------------
		}

}

function TFlashKey
	{
	if(GetPlayerFlash >= 200  && GetPlayerInvincibilityFrame == 0 && IsHit == false && GetPlayerFlashEnable == true && IsFlashActive == false)
		{
		TInitializeFlash;
		return true;
		}
	else
		{
		return false;
		}
	}

function GetFlashManageObject
	{
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	Obj_SetValue(obj,"PLAYER",true);
	TMove;
	return obj;

	task TMove
		{
		while(!Obj_IsDeleted(obj))
			{
			Obj_SetValue(objPlayer,"FLASH_ACTIVE",true);
			yield;
			}
		Obj_SetValue(objPlayer,"FLASH_ACTIVE",false);
		}
	}

function IsFlashActive
	{
	return Obj_GetValueD(objPlayer,"FLASH_ACTIVE",false);
	}


function FHomingEnemy(x,y)
{
	let enemy = ID_INVALID;
	ascent(i in 0..length(GetEnemyBossObjectID))
		{
		enemy = GetEnemyBossObjectID[i];	
		}	
	let enemy1 = enemy;
	let array = GetAllEnemyID;
	let dist = GetStgFrameHeight*1.5;
	if(enemy == ID_INVALID || Obj_IsDeleted(enemy) || ObjMove_GetY(enemy) > y)
		{
		ascent(i in 0..length(array))
			{
			let obj = array[i];
			if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"PLAYER",false) == false && GetDistance(x,y,ObjMove_GetX(obj),ObjMove_GetY(obj)) < dist && ObjMove_GetY(obj) < y)
				{
				enemy = obj;
				dist = GetDistance(x,y,ObjMove_GetX(obj),ObjMove_GetY(obj));
				}
			}
		}	
	if(enemy == ID_INVALID || Obj_IsDeleted(enemy))
		{
		ascent(i in 0..length(array))
			{
			let obj = array[i];
			if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"PLAYER",false) == false && GetDistance(x,y,ObjMove_GetX(obj),ObjMove_GetY(obj)) < dist && ObjMove_GetY(obj) < y)
				{
				enemy = obj;
				dist = GetDistance(x,y,ObjMove_GetX(obj),ObjMove_GetY(obj));
				}
			}
		}	
	if(enemy == ID_INVALID || Obj_IsDeleted(enemy))
		{
		ascent(i in 0..length(array))
			{
			let obj = array[i];
			if(!Obj_IsDeleted(obj) && Obj_GetValueD(obj,"PLAYER",false) == false && GetDistance(x,y,ObjMove_GetX(obj),ObjMove_GetY(obj)) < dist)
				{
				enemy = obj;
				dist = GetDistance(x,y,ObjMove_GetX(obj),ObjMove_GetY(obj));
				}
			}
		}
	return enemy;	
}

function FAngleToHomingEnemy(enemy,x,y,angle,type)
{	
	if(enemy != ID_INVALID)
		{
		let angleA = atan2(ObjMove_GetY(enemy)-y,ObjMove_GetX(enemy)-x);
		if(type == 1)
			{
			angle+=0.5*sin(angleA-angle);
			}
		else
			{
			angle = angleA;
			}
		}
	return angle;
}