#include "SysPlayerIndex.h"

SysPlayerIndex::SysPlayerIndex()
{
	ani.png = L"sys_obj";
	dx = 0;
	dy = -16;
	drawBeforeOtherEntity = true;
}

void SysPlayerIndex::Step()
{
	if (existTime > 0)
		existTime--;
	if (existTime == 0)
		Destroy();
	if (gameFunc->ScriptIsRunning)
	{
		if (gameFunc->ScriptIsRunning(gameFunc->gameScript))
			ani.png = L"";
		else
			ani.png = L"sys_obj";
	}
	auto wtarget = target.lock();
	if (wtarget)
	{
		if (lr == 0)
		{
			//在玩家1头顶
			if (gameFunc->GetMyChar_Player1)
			{
				auto wplayer = gameFunc->GetMyChar_Player1(gameFunc->entityRes).lock();
				if (wplayer == wtarget)
				{
					x = wplayer->x + dx;
					y = wplayer->y + dy;
					last_x = wplayer->last_x + dx;
					last_y = wplayer->last_y + dy;
				}
				else
				{
					Destroy();
				}
			}
		}
		else
		{
			//在玩家2头顶
			if (gameFunc->GetMyChar_Player2)
			{
				auto wplayer = gameFunc->GetMyChar_Player2(gameFunc->entityRes).lock();
				if (wplayer == wtarget)
				{
					x = wplayer->x + dx;
					y = wplayer->y + dy;
					last_x = wplayer->last_x + dx;
					last_y = wplayer->last_y + dy;
				}
				else
				{
					Destroy();
				}
			}
		}
	}
	else
	{
		if (lr == 0)
		{
			//在玩家1头顶
			if (gameFunc->GetMyChar_Player1)
			{
				auto wplayer = gameFunc->GetMyChar_Player1(gameFunc->entityRes).lock();
				if (wplayer)
				{
					x = wplayer->x + dx;
					y = wplayer->y + dy;
					last_x = wplayer->last_x + dx;
					last_y = wplayer->last_y + dy;
					target = wtarget;
				}
			}
			ani.sprite_index = L"players_p1";
		}
		else
		{
			//在玩家2头顶
			if (gameFunc->GetMyChar_Player2)
			{
				auto wplayer = gameFunc->GetMyChar_Player2(gameFunc->entityRes).lock();
				if (wplayer)
				{
					x = wplayer->x + dx;
					y = wplayer->y + dy;
					last_x = wplayer->last_x + dx;
					last_y = wplayer->last_y + dy;
					target = wtarget;
				}
			}
			ani.sprite_index = L"players_p2";
		}
	}
}

std::shared_ptr<Entity> CreateSysPlayerIndex()
{
	return std::make_shared<SysPlayerIndex>();
}
