#include "CS_MyFishAi.h"

CS_MyFishAi::CS_MyFishAi()
{
	
}

void CS_MyFishAi::Init(std::weak_ptr<Entity> self)
{
	InitControlTarget();
	auto wicon = std::make_shared<CS_MyCharAi_Icon>();
	icon = wicon;
	wicon->color = RGBA(0xff, 0xff, 0xff, 0x80);
	gameFunc->SetEntity(shared_from_this(), wicon);
	Entity::Init(self);
}

void CS_MyFishAi::Step()
{
	auto wattackParent = attackParent.lock();
	if (!wattackParent)
	{
		Destroy();
		return;
	}
	//清空操作
	wattackParent->input.key &= GameInput::keybit_switchMc;
	//跟随在目标头顶
	x = wattackParent->x;
	y = wattackParent->y - 16;
	last_x = wattackParent->last_x;
	last_y = wattackParent->last_y - 16;
	//ai图标跟随
	auto wicon = icon.lock();
	if (wicon)
	{
		wicon->x = wattackParent->x + wicon->dx;
		wicon->y = wattackParent->y - 16 + wicon->dy;
		wicon->last_x = wattackParent->last_x + wicon->dx;
		wicon->last_y = wattackParent->last_y - 16 + wicon->dy;
		//隐藏图标
		if (gameFunc->ScriptIsRunning)
		{
			if (gameFunc->ScriptIsRunning(gameFunc->gameScript))
				wicon->ani.sprite_index.clear();
			else
				wicon->ani.sprite_index = L"ai";
		}
		//隐藏了
		if (wattackParent->hide)
		{
			wicon->ani.sprite_index.clear();
		}
		//如果不是玩家1或玩家2则也隐藏图标
		if(gameFunc->GetPlayerIndex(gameFunc->game, wattackParent) == -1)
			wicon->ani.sprite_index.clear();
	}
	Control();
	//清除ok和cancel
	wattackParent->input.key &= ~GameInput::keybit_ok;
	wattackParent->input.key &= ~GameInput::keybit_cancel;
}

void CS_MyFishAi::Destroy()
{
	//找到自己的控制目标，然后从它身上移除
	auto wattackParent = attackParent.lock();
	if (wattackParent)
		wattackParent->ai.reset();
	auto wicon = icon.lock();
	if (wicon)
		wicon->Destroy();
	Entity::Destroy();
}

void CS_MyFishAi::Control()
{
	auto wattackParent = attackParent.lock();
	if (!wattackParent)
		return;
	auto mychar = std::static_pointer_cast<MyCharEntity> (wattackParent);
	bool bPickUp = false;
	/*-----------------------------移动控制-------------------------------*/
	std::weak_ptr<Entity> followTarget;
	//跟随这个目标
	if (!mychar->attackParent.expired())
		followTarget = mychar->attackParent;
	//没有没有跟随目标，则跟随玩家
	if (followTarget.expired())
		followTarget = gameFunc->GetMyChar_Player1(gameFunc->entityRes);
	auto wfollowTarget = followTarget.lock();
	if (wfollowTarget)
	{
		mychar->attackParent = wfollowTarget;
		//攻击
		mychar->input.SetAtk(wfollowTarget->input.GetAtk());
		float ySearch = 16;
		if (bPickUp)
			ySearch = 4;
		float xSearch = 24;
		if (bPickUp)
			xSearch = 4;
		if (abs(mychar->x - wfollowTarget->x) > xSearch ||
			abs(mychar->y - wfollowTarget->y) > ySearch)
		{
			//用寻路接近目标
			float dirX = 0, dirY = 0;
			Pathfinding(wfollowTarget->x, wfollowTarget->y, &dirX, &dirY);
			//尽量用直线的方式接近目标
			if (jumpInterval == 0 && dirY < 0)
			{
				jumpInterval = 1;
				//进行跳跃
				mychar->input.SetJump(true);
			}
			else if (jumpInterval > 0)
			{
				jumpInterval--;
			}
			if (dirX > 0.1f)
			{
				mychar->input.SetRight(true);
			}
			else if (dirX < -0.1f)
			{
				mychar->input.SetLeft(true);
			}
		}
	}
}

void CS_MyFishAi::InitControlTarget()
{
	//寻找控制对象
	if (gameFunc->GetGlobalEntityTable)
	{
		auto& allPlayer = *gameFunc->GetGlobalEntityTable(gameFunc->game);
		for (auto& p : allPlayer)
		{
			auto we = p.second.lock();
			if (we)
			{
				auto wplayer = std::dynamic_pointer_cast<MyCharEntity>(we);
				if (wplayer)
				{
					if (wplayer->ai.expired())
						continue;
					if (wplayer->ai.lock() == shared_from_this())
					{
						attackParent = wplayer;
						//载入跟随目标
						if (wplayer->lastAI_followTarget.length())
						{
							for (auto& t : allPlayer)
							{
								auto wtarget = t.second.lock();
								if (wtarget)
								{
									if (wplayer->lastAI_followTarget == wtarget->globalEntityID)
									{
										wplayer->attackParent = wtarget;
										break;
									}
								}
							}
						}
						break;
					}
				}
			}
		}
	}
	if (attackParent.expired())
		Destroy();
}

bool CS_MyFishAi::AttackAround()
{
	return false;
}

std::shared_ptr<Entity> CreateCS_MyFishAi()
{
	return std::make_shared<CS_MyFishAi>();
}
