#include "CS_PuppyFishBubbleBeam.h"
#include "CS_PuppyFish.h"
const int CS_PuppyFishSTATENAME_BUBBLERBEAM = ++entityStateID;
CS_PuppyFishBubbleBeam::CS_PuppyFishBubbleBeam()
{
	stateID = CS_PuppyFishSTATENAME_BUBBLERBEAM;
	cd_counter = 0;
	cd_counter_max = 1;
	iconIndex = 0;
}

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

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

	auto mychar = std::static_pointer_cast<CS_PuppyFish>(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_PuppyFishBubbleBeam::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_PuppyFish>(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_PuppyFishBubbleBeam::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_PuppyFish>(wparent);
		if (mychar)
		{
			auto& amList = mychar->skillBubbleBeam.amList;
			amList.push_back(am);
		}
	}
}

void CS_PuppyFishBubbleBeam::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_PuppyFish>(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;
				}
			}
		}
	}
}
