#include "CS_JackFishBubbleBeam.h"
#include "CS_JackFish.h"
const int CS_JackFishSTATENAME_BUBBLERBEAM = ++entityStateID;
CS_JackFishBubbleBeam::CS_JackFishBubbleBeam()
{
	stateID = CS_JackFishSTATENAME_BUBBLERBEAM;
	cd_counter = 0;
	cd_counter_max = 1;
	iconIndex = 0;
}

const wchar_t* CS_JackFishBubbleBeam::GetSaveName()
{
	return L"BubblerBeam";
}

void CS_JackFishBubbleBeam::SubStep()
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_JackFish>(we);
	bool a1 = !lock;
	bool b1 = (mychar->input.key & keyMask);
	bool c1 = cd_counter == 0 && mychar->mp >= mp_cost;

	bool a = a1;
	bool b = b1;
	bool c = c1;

	if (a && b && c)
	{
		mychar->lastSkill = this;
		mychar->ChangeState(GetStateID());
	}
	//技能冷却时间
	if (cd_counter > 0)
		cd_counter--;
	//等级
	if (mychar->mp >= 30)
		level = 2;
	else if (mychar->mp >= 10)
		level = 1;
	else
		level = 0;
}

void CS_JackFishBubbleBeam::ChangeStateMsg(int stateID)
{
	if (stateID == GetStateID() && amList.size() < 2 && bubbleCharge.expired())
	{
		//初始化技能
		auto we = e.lock();
		if (!we)
			return;
		auto mychar = std::static_pointer_cast<CS_JackFish>(we);
		enable = false;
		//开火
		auto am = std::make_shared<CS_AmBubbleBeamCharge>(we, keyMask, level, AddToAmList, RemoveFromAmList);
		we->gameFunc->SetEntity(we, am);
		bubbleCharge = am;
		mychar->gameFunc->PlaySound(L"CS_bubblebeam_charge");
	}
	else
		enable = false;
}

void CS_JackFishBubbleBeam::AddToAmList(std::weak_ptr<Entity> parent, std::weak_ptr<CS_AmBubbleBeam> am)
{
	auto wparent = parent.lock();
	if (wparent)
	{
		auto mychar = std::dynamic_pointer_cast<CS_JackFish>(wparent);
		if (mychar)
		{
			auto& amList = mychar->skillBubbleBeam.amList;
			amList.push_back(am);
		}
	}
}

void CS_JackFishBubbleBeam::RemoveFromAmList(std::weak_ptr<Entity> parent, std::weak_ptr<CS_AmBubbleBeam> am)
{
	auto wparent = parent.lock();
	if (wparent)
	{
		auto mychar = std::dynamic_pointer_cast<CS_JackFish>(wparent);
		if (mychar)
		{
			auto& amList = mychar->skillBubbleBeam.amList;
			for (auto iter = amList.begin(); iter != amList.end(); iter++)
			{
				if ((*iter).lock() == am.lock())
				{
					amList.erase(iter);
					break;
				}
			}
		}
	}
}
