#include "SysLocalCamera.h"
#include "SysNull.h"
void SysLocalCamera::Step()
{
	if (!gameFunc->SetLocalCamera)
		return;
	if (lr == 0)
	{
		//检查是否有玩家在这块区域内
		bool bSet = false;
		auto& allPlayer = *gameFunc->GetGlobalEntityTable(gameFunc->game);
		for (auto& p : allPlayer)
		{
			auto wplayer = p.second.lock();
			if (wplayer && !wplayer->enableAI && !wplayer->hide && wplayer->el.name != SYSNULL_CREATENAME)
			{
				if (Touch(wplayer))
				{
					bSet = true;
					break;
				}
			}
		}
		if (bSet)
		{
			//聚焦到该Npc
			gameFunc->SetLocalCamera(gameFunc->game, shared_from_this(), 16);
		}
		else
		{
			//取消该单位的聚焦
			gameFunc->UnsetLocalCamera(gameFunc->game, shared_from_this());
		}
	}
}

void SysLocalCamera::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"disable_focus")
	{
		//取消该单位的聚焦
		gameFunc->UnsetLocalCamera(gameFunc->game, shared_from_this());
		this->lr = 1;
	}
	else if(stateName == L"enable_focus")
	{
		this->lr = 0;
	}
}

void SysLocalCamera::Init(std::weak_ptr<Entity> self)
{
	if (el.arg.length() && ParseInts(el.arg.c_str(), argbuff))
	{
		if (argbuff.size() == 4)
		{
			cbRect.left = 16.0f * argbuff[0];
			cbRect.right = 16.0f * argbuff[1];
			cbRect.top = 16.0f * argbuff[2];
			cbRect.bottom = 16.0f * argbuff[3];
		}
	}
	Entity::Init(self);
}

std::shared_ptr<Entity> CreateSysLocalCamera()
{
	return std::make_shared<SysLocalCamera>();
}
