#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_3) ~ "/Sprite.png";;
let PlayerDead = false;
let HitBox = 1.66;
let GrazeBox = 20;
let DefaultBomb = 3;
let PlayerSpeed = [4,2];

let ShotPermit = true;
let SpellPermit = true;
let IsDeathBomb = false;
let IsHit = false;
let IsMoving = false;
let IsRebirth = false;
let SpellSwitch = true;
let IsFocusForce = false;
let IsUnfocusForce = false;
let GrazeEnable = true;
let GrazeMultiply = 0.8;

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

	ObjMove_SetPosition(objPlayer,GetStgFrameWidth/2,GetStgFrameHeight-30);
	SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);	
	SetPlayerRebirthLossFrame(0);
	SetPlayerLife(100);

	TInitializeBomb;
	
	let life = 500;
	if(GetCommonData("CHEAT_ENABLED",false) == true)
		{
		life = 2000;
		if(GetCommonData("STAGE_TYPE","") == "TYPE_SPELL_PRACTICE")
			{
			DefaultBomb = 0;
			}
		}
	else if(GetCommonData("STAGE_TYPE","") == "TYPE_SPELL_PRACTICE")
		{
		DefaultBomb = 0;
		life = 0;
		}
	else if(GetCommonData("STAGE_TYPE","") == "TYPE_STAGE_PRACTICE")
		{
		life = 2000;
		}
	Obj_SetValue(objPlayer,"PLAYER_LIFE",GetCommonData("PLAYER_LIFE",life));
	SetPlayerSpell(GetCommonData("PLAYER_BOMB",DefaultBomb));
	SetPlayerFlash(GetCommonData("PLAYER_FLASH",0));
	
	TMain;	

	SetPlayerSpeed(PlayerSpeed[0],PlayerSpeed[1]);
	CreatePlayerHitBox;
	TPlayerLifeRender;
	TPlayerSpellRender;
	TPlayerFlashRender;
	Obj_SetValue(objPlayer,"PLAYER_PATH",PlayerPath);
	TImage;
	TShot;
}

@MainLoop
{
	if(GetPlayerHealth > 2000)
		{
		SetPlayerHealth(2000);
		}
	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",false) == true && 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_REQUEST_SPELL)
		{
		let spell = GetPlayerSpell();
		if(GetCommonData("FLASH_KEY",false) == true || (((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)
				{
				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)
		{
		PlaySound("se_Graze",100);
		AddGraze(1);
		let flash = GetPlayerFlash;
		SetPlayerFlash(min(200,flash+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+2, 16+0.75, 24.5+2);	
	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.75-i*0.4);
		}
	ObjRender_SetBlendType(obj[1],BLEND_ADD_ARGB);
	let angle = 0;	
	let scale = 4;
	let alpha = 0;

	let counter = 0;
	let path = PlayerPath;

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

	while(PlayerDead == false)
		{	
		counter+=1;
		if(path != PlayerPath)
			{
			ObjPrim_SetTexture(objPlayer,PlayerPath);
			path = PlayerPath;
			}
		ImageWidth = GetTextureWidth(path)/8;
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)
			{	
			if(scale > 1)
				{
				scale-=1/10;
				alpha+=255/10;
				}
			else
				{
				scale = 1;
				alpha = 255;
				angle+=3;
				}	
			}
		else
			{
			if(scale < 2)
				{
				scale+=1/10;
				alpha-=255/10;
				angle+=3;
				}
			else
				{
				scale = 2;
				alpha = 0;
				}
			}
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
			{
			anim = 0;
			if(move < 3)
				{
				move+=0.5;
				}
			else
				{
				animm += 0.25;
				move = 3;
				}
			IsMoving = true;
			ObjSprite2D_SetSourceRect(objPlayer, floor(move)*ImageWidth+floor(animm)%4*ImageWidth, 48, floor(move+1)*ImageWidth+floor(animm)%4*ImageWidth, 95);
			}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
			{
			anim = 0;
			if(move < 3)
				{
				move+=0.5;
				}
			else
				{
				animm += 0.25;
				move = 3;
				}
			IsMoving = true;
			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;
			if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD || GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD)
				{	
				IsMoving = true;
				}
			else
				{
				IsMoving = false;
				}
			ObjSprite2D_SetSourceRect(objPlayer, floor(anim)%8*ImageWidth+1, 1, (1+floor(anim)%8)*ImageWidth+num, 48);
			}
		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);
	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",false);
		Obj_SetValue(objPlayer,"DAMAGE",false);
		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 && GetObjectDistance(objPlayer,shot) < dist && GetObjectDistance(objPlayer,shot) <= hit+ObjShot_GetScale(shot)*0.8)
				{
				dist = GetObjectDistance(objPlayer,shot);
				damage = true;
				}
			}
		if(damage == true)
			{
			THit;
			}
		Obj_SetValue(objPlayer,"PLAYER_LIFE",ObjEnemy_GetInfo(objEnemy,INFO_LIFE));
		yield;
		}
	function THit
		{
		let vis = Obj_IsVisible(objPlayer);
		let obj = [];
		let count = 8;
		let die = 0;
		let color = 0;
		let deathbombtime = 10;
		let life;	
		local
			{
			life = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
			die = 3;
			life = floor(life/100)*100;
			color = 0;
			}
		let time = deathbombtime;
		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)
			{
			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);
							}
						}
					}
				}
			if(IsPlayerSpellActive == true)
				{
				IsDeathBomb = false;
				DeathBomb = true;
				ShotPermit = true;
				ascent(i in 0..2)
					{
					Obj_Delete(obj[i]);
					}
				break;
				}
			yield;
			}	
		SpellPermit = false;
		IsDeathBomb = false;
		IsHit = true;
		Obj_SetValue(objPlayer,"IS_HIT",true);
		NotifyEventAll(EV_PLAYER_SHOOTDOWN,NULL);
		if(die >= 2 && DeathBomb == false)
			{
			ObjEnemy_SetLife(objEnemy,life);
			}
		ascent(i in 0..2)
			{
			Obj_Delete(obj[i]);
			}
		time = 15;	
		if((die == 1 || die == 3) && DeathBomb == false)
			{		
			ObjEnemy_SetLife(objEnemy,life-1);
			PlaySound("se_PlayerDie",120);
			ClearShotCircle;
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
				{
				PlayerDead = true;
				SetPlayerLife(-1);
				ObjPlayer_AddIntersectionCircleA1(objPlayer,0,0,0,0);
				TDie;
				return;
				}
			TDie;
			time = 30;
			}
		else
			{
			ClearShotCircle;
			}
		local
			{
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "../system/img/WhiteCircle.png");
			ObjSprite2D_SetSourceRect(obj,0,0,1,1);
			ObjSprite2D_SetDestRect(obj,-30-time,-30-time,30+time,30+time);
			ObjRender_SetPosition(obj,x,y,0);
			Obj_SetRenderPriority(obj,0.35);
			ObjRender_SetColorHSV(obj,0,255,175);
			Obj_DelayDelete(obj,time/3);
			}
		SetPlayerClip(8,8,GetStgFrameWidth-8,GetStgFrameHeight-8);
		if(DeathBomb == false)
			{
			ShotPermit = true;
			SetPlayerInvincibilityFrame(time*6+1);
			alpha = 255;
			let dir = 1;
			IsRebirth = true;
			SpellPermit = true;
			loop(time)
				{
				alpha = dir*255/3+255/3;
				loop(6)	
					{
					ObjRender_SetColorHSV(objPlayer,color,alpha,255);	
					yield;
					}	
				dir*=-1;
				}
			IsRebirth = false;
			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 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,0,255,255);
			ObjRender_SetAlpha(back,205);
			Obj_SetRenderPriority(back,0.65);
			let alpha = 255;
			let alphaB = 205;
			let scale = 1;
			SetPlayerInvincibilityFrame(61);
			loop(30)
				{
				scale += 0.1;
				alpha -= 15;
				alphaB-=205/52;
				ObjRender_SetAlpha(back,alphaB);
				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);	
			SetPlayerSpell(DefaultBomb);
			ascent(t in 0..30)
				{
				y-=64/30;
				alphaB-=205/52;
				ObjRender_SetAlpha(back,alphaB);
				SetPlayerClip(x,y,x,y);
				ObjMove_SetPosition(objPlayer,x,y);
				if(t >= 1)
					{
					Obj_SetVisible(objPlayer,vis);	
					}
				yield;
				}
			Obj_Delete(back);
			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_IsSpellResist(array[i]) == false)
				{
				TDeleteShot(array[i]);
				}
			}
		}
}

task TInitializeSpell
{
	GrazeMultiply = 1/5;
	PlaySound("se_Spell",150);
	SetPlayerSpell(GetPlayerSpell-1);
	NotifyEventAll(EV_PLAYER_SHOOTDOWN,NULL);
	alternative(PlayerID_2)
		case(0)
			{
			TNueSpell;
			}
	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;
	let armor = false;
	GrazeMultiply = 0;
	alternative(PlayerID_2)
		case(0)
			{
			TNueFlash;
			}
	if(GetPlayerFlash > 0)
		{
		PlaySound("se_Flashbomb",100);
		SetPlayerFlash(0);
		}
	loop(GetPlayerInvincibilityFrame){yield;}
	GrazeEnable = true;
	GrazeMultiply = 1;
}

task TInitializeBomb
{
	alternative(PlayerID_2)
		case(0)
			{
			DefaultBomb = 3;
			}
		case(1)
			{
			DefaultBomb = 2;
			}
		case(2)
			{
			DefaultBomb = 4;
			}
}

task TPlayerLifeRender
{
	let remStepRate = 19;
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory() ~ "../system/img/RSS_Item.png");
	Obj_SetRenderPriority(obj, 0.18);

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

	loop
		{
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(spell);
		RenderLife();
		yield;
		}

	function RenderLife()
		{
		let life = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
		let countLife = floor(life/100);
		ObjSpriteList2D_SetSourceRect(obj, 0, 0, 32, 32);
		ObjSpriteList2D_SetSourceRect(spell, 64, 0, 96, 32);
		ascent(iStep in 0 .. 20)
			{
			ObjSpriteList2D_AddVertex(obj);
			ObjSpriteList2D_SetDestRect(obj, GetScreenWidth-225 + (4-iStep) * remStepRate + 2, GetScreenHeight-remStepRate-1,GetScreenWidth-225 + (5-iStep) * remStepRate, GetScreenHeight-1);
			ObjSpriteList2D_AddVertex(obj);
			ObjRender_SetColor(obj,175,175,175);
			}
		ascent(iStep in 0 .. min(countLife,20))
			{
			ObjSpriteList2D_AddVertex(spell);
			ObjSpriteList2D_SetDestRect(spell, GetScreenWidth-225 + (4-iStep) * remStepRate + 2, GetScreenHeight-remStepRate-1,GetScreenWidth-225 + (5-iStep) * remStepRate, GetScreenHeight-1);
			ObjSpriteList2D_AddVertex(spell);
			ObjRender_SetColor(spell,255,255,255);
			}
		}

}

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

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

	let remStepRate = 32;

	loop
		{
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(spell);
		RenderLife();
		yield;
		}

	function RenderLife()
		{
		let countLife = GetPlayerSpell;
		ObjSpriteList2D_SetSourceRect(obj, 32, 0, 64, 32);
		ObjSpriteList2D_SetSourceRect(spell, 96, 0, 128, 32);
		ascent(iStep in 0 .. 5)
			{
			ObjSpriteList2D_AddVertex(obj);
			ObjSpriteList2D_SetDestRect(obj, GetScreenWidth-88, GetScreenHeight-30-(iStep+1) * remStepRate,GetScreenWidth-90 + remStepRate, GetScreenHeight-30-(iStep) * remStepRate);
			ObjSpriteList2D_AddVertex(obj);
			ObjRender_SetColor(obj,175,175,175);
			}
		ascent(iStep in 0 .. min(countLife,5))
			{
			ObjSpriteList2D_AddVertex(spell);
			ObjSpriteList2D_SetDestRect(spell, GetScreenWidth-88, GetScreenHeight-30-(iStep+1) * remStepRate,GetScreenWidth-90 + remStepRate, GetScreenHeight-30-(iStep) * remStepRate);
			ObjSpriteList2D_AddVertex(spell);
			ObjRender_SetColor(spell,255,255,255);
			}
		}
}

task TPlayerFlashRender
{
	let x = [GetScreenWidth-116,GetScreenWidth-100];
	let y = [GetScreenHeight/2+20,GetScreenHeight-20];
	let dist = y[1]-y[0];
	let objScale = [];
	ascent(i in 0..2)
		{
		objScale = objScale ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(objScale[i],GetCurrentScriptDirectory ~ "../system/img/WhiteSquare.png");
		if(i == 1)
			{
			ObjPrim_SetTexture(objScale[i],GetCurrentScriptDirectory ~ "../system/img/System_BarF.png");
			}
		ObjRender_SetAlpha(objScale[i],255);
		ObjRender_SetColorHSV(objScale[i],0,0,100);
		Obj_SetRenderPriority(objScale[i],0.11);
		ObjSprite2D_SetSourceRect(objScale[i],0,0,16,dist);
		ObjSprite2D_SetDestRect(objScale[i],x[0],y[0],x[1],y[1]);
		Obj_SetRenderPriority(objScale[i],0.17+i*0.02);
		}
	let bar = [];
	ascent(i in 0..1)
		{
		bar = bar ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(bar[i],GetCurrentScriptDirectory ~ "../system/img/WhiteSquare.png");
		ObjSprite2D_SetSourceRect(bar[i],0,0,1,1);
		Obj_SetRenderPriority(bar[i],0.19);
		}
	ObjRender_SetBlendType(bar[0],BLEND_ADD_ARGB);
	x[0] = x[0]+3;
	x[1] = x[1]-3;
	y[0] = y[0]+3;
	y[1] = y[1]-3;
	dist = y[1]-y[0];
	let color = 180;
	let sound1 = false;
	let sound2 = false;
	loop
		{
		let flash = GetPlayerFlash;
		if(flash == 200)
			{	
			color+=1.25;
			if(sound1 == false)
				{
				PlaySound("se_Boost2",100);
				sound1 = true;
				}
			}
		else
			{
			color = 180;
			sound1 = false;
			}
		ObjRender_SetColorHSV(bar[0],0,105+105*cos(color),225);
		ObjSprite2D_SetDestRect(bar[0],x[0],y[1]-dist*flash/200,x[1],y[1]);
		yield;
		}
}

function TFlashKey
	{
	if(GetPlayerFlash >= 200  && GetPlayerInvincibilityFrame == 0 && IsHit == false && GetPlayerSpellEnable == 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);
	}