#include "CS_CurlyEnergyShield.h"
#include "CS_Curly.h"
const int CS_CurlySTATENAME_SHIELD = GetNextEntityStateID();
CS_CurlyEnergyShield::CS_CurlyEnergyShield()
{
	stateID = CS_CurlySTATENAME_SHIELD;
	cd_counter = 0;
	cd_counter_max = 30;
	iconIndex = 2;
	mp_cost = 1;
}

const wchar_t* CS_CurlyEnergyShield::GetSaveName()
{
	return L"EnergyShield";
}

const wchar_t* CS_CurlyEnergyShield::GetFuncName()
{
	return L"CurlySkill_EnergyShield";
}

const wchar_t* CS_CurlyEnergyShield::GetCheckFuncName()
{
	return L"CurlySkill_EnergyShield_Check";
}

void CS_CurlyEnergyShield::SubStep()
{
	auto we = e.lock();
	if (!we)
		return;
	auto mychar = std::static_pointer_cast<CS_Curly>(we);
	bUIShowDisableSkill = !(
		we->mp >= mp_cost &&
		!mychar->fishState &&
		!lock
		);

	bool a1 = !lock;
	bool b1 = (mychar->input.key & keyMask);
	bool c1 = cd_counter == 0 && mychar->mp >= mp_cost && !mychar->fishState;

	bool a = a1;
	bool b = b1;
	bool c = c1;
	if (a && b && c)
	{
		mychar->lastSkill = this;
		mychar->ChangeState(GetStateID());
	}
	//维持护盾
	if (bEnableShield)
	{
		if (shield.expired())//如果切换房间则重新生成一个护盾图案【25/7/20bug修复】
		{
			auto wshield = std::make_shared<CS_CurlyEnergyShieldCircle>(we);
			shield = wshield;
			we->gameFunc->SetEntity(we, wshield);
		}
		if (we->gameFunc->ScriptIsRunning)
		{
			if (!we->gameFunc->ScriptIsRunning(we->gameFunc->gameScript))
			{
				if (shield_counter > 0)
					shield_counter--;
			}
		}
		else
		{
			if (shield_counter > 0)
				shield_counter--;
		}
		if (shield_counter == 0)
		{
			if (we->mp > 0)
			{
				//维持护盾
				we->mp -= mp_cost;
				shield_counter = shield_counter_max;
			}
			else
			{
				//关闭护盾
				cd_counter = cd_counter_max;
				auto wshield = shield.lock();
				if (wshield)
					wshield->SetLeave();
				shield.reset();
				bEnableShield = false;
			}
		}
	}
	if (cd_counter > 0)
		cd_counter--;
	if (cd_counter == 0)
	{
		int cdSrc = cd_counter;
		cd_counter = bEnableShield ? cd_counter_max : 0;
		EntitySkillState::SubStep();
		cd_counter = cdSrc;
	}
	else
	{
		EntitySkillState::SubStep();
	}
}

void CS_CurlyEnergyShield::ChangeStateMsg(int stateID)
{
	if (stateID == GetStateID())
	{
		auto we = e.lock();
		if (!we)
			return;
		if (bEnableShield)
		{
			//关闭护盾
			//取消护盾后会有一段冷却时间
			cd_counter = cd_counter_max;
			auto wshield = shield.lock();
			if (wshield)
				wshield->SetLeave();
			shield.reset();
		}
		else
		{
			//打开护盾
			//设置防御圈
			auto wshield = std::make_shared<CS_CurlyEnergyShieldCircle>(we);
			shield = wshield;
			we->gameFunc->SetEntity(we, wshield);
			//设置护盾能量
			we->mp -= mp_cost;
			shield_counter = shield_counter_max;
		}
		//切换护盾
		bEnableShield = !bEnableShield;
		//锁住按键
		we->inputLock.key |= keyMask;
	}
	else
		enable = false;
}

bool CS_CurlyEnergyShield::Hurt(std::weak_ptr<Entity> src, int* pGetDamage, float* pAngle, bool* pTakedown, int* pElemType, float* pShockForce, float* pHeavy, int* outRealDamage, bool* outRetVal)
{
	auto we = e.lock();
	if (!we)
		return false;
	auto mychar = std::static_pointer_cast<CS_Curly>(we);

	if (mychar->skillEnergyShield.bEnableShield)
	{
		//成功格挡
		*outRealDamage = 0;
		return true;
	}
	return false;
}
