#include "CS_KingJump.h"
#include "CS_King.h"
const int CS_KingSTATENAME_JUMP = GetNextEntityStateID();
CS_KingJump::CS_KingJump()
{
	enable = true;
	SetPhysice(false);
	jump_disable = true;
	jumped = true;
	stateID = CS_KingSTATENAME_JUMP;
}

void CS_KingJump::State()
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_King>(we);
	auto& bClimb = mychar->stateControl.bClimb;
	auto& bClimb_freezeControl_counter = mychar->stateControl.bClimb_freezeControl_counter;
	auto& bClimb_freezeControl_counter_max = mychar->stateControl.bClimb_freezeControl_counter_max;

	if (climb_coyote_counter > 0)
		climb_coyote_counter--;
	if (bClimb)
		climb_coyote_counter = climb_coyote_max;
	//重置状态
	if (we->cbRect.cbBottom || we->cbRect.inWater)
		jumped = false;
	//锁键
	if (!we->input.GetJump())
		jump_disable = false;
	//通常跳跃
	bool a1 = (we->cbRect.cbBottom || we->cbRect.inWater) &&
		!mychar->InAction() &&
		!jumped;
	//攀岩跳跃
	bool a2 = !mychar->InAction() &&
		climb_coyote_counter > 0;
	bool b1 = we->input.GetJump() &&
		!we->blockJump &&
		!jump_disable;

	bool a = a1 || a2;
	bool b = b1;
	if (a && b)
	{
		if (a1)
		{
			we->vy = -jump;
		}
		else if (a2)
		{
			we->vy = -jump;
			we->vx = we->lr == 0 ? jump : -jump;
			/*if (e->input.GetUp())
				e->vx /= 2;*/
			bClimb_freezeControl_counter = bClimb_freezeControl_counter_max;
		}
		jumped = true;
		jump_disable = true;
		if (we->cbRect.inWater)
			swimJump_counter = swimJump_counter == 0 ? 1 : 0;
		else
			swimJump_counter = 0;
		we->gameFunc->PlaySound(L"CS_015_quote_jump");
		//是否在空中
		mychar->ChangeState(mychar->stateAir.GetStateID());
		mychar->action_timecounter = 0;
	}
}

void CS_KingJump::SetPhysice(bool bInWater)
{
	if (bInWater)
		jump = inwater_jump;
	else
		jump = ground_jump;
}

void CS_KingJump::SetJump()
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_King>(we);
	we->vy = -jump;
	jumped = true;
	jump_disable = true;
	if (we->cbRect.inWater)
		swimJump_counter = swimJump_counter == 0 ? 1 : 0;
	else
		swimJump_counter = 0;
}
