#include "CS_PuppyControl.h"
#include "CS_Puppy.h"
#include "CS_PeLittleStar.h"
const int CS_PuppySTATENAME_CONTROL = ++entityStateID;
CS_PuppyControl::CS_PuppyControl()
{
	enable = true;
	SetPhysice(false);
	stateID = CS_PuppySTATENAME_CONTROL;
}

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

	auto mychar = std::static_pointer_cast<CS_Puppy>(we);
	auto& stateResist = mychar->stateResist;
	//重置状态
	if (we->cbRect.cbBottom)
	{
		if (we->ud == 1 && !mychar->InAction())
			we->ud = 0;
	}
	if (!we->input.GetJump())
		we->blockJump = false;
	stateResist.apply_resistH = true;
	//KEY禁止玩家操作
	if (we->gameFunc->IsLockKeyboard && we->gameFunc->IsLockKeyboard(we->gameFunc->gameScript))
	{
		we->input.key = 0;
		for (auto& p : we->axis)
			p = 0;
	}
	//上	
	if (we->input.GetUp() || we->axis[0] < 0)
	{
		if (!mychar->InAction())
			we->ud = -1;
	}
	//下
	else if (we->input.GetDown() || we->axis[1] > 0)
	{
		if (!mychar->InAction() && !we->cbRect.cbBottom)
			we->ud = 1;
	}
	//什么都不按
	else
	{
		if (!mychar->InAction())
			we->ud = 0;
	}
	JumpControl();
	//落地声音
	if (we->cbRect.cbBottom && playLandingSound)
	{
		we->gameFunc->PlaySound(L"CS_puppy_landing");
		playLandingSound = false;
	}
	if (!we->cbRect.cbBottom)
		playLandingSound = true;
	//撞头声音
	if (!we->cbRect.cbTop)
		playBonkHeadSound = true;
	if (we->cbRect.cbTop && playBonkHeadSound)
	{
		we->gameFunc->PlaySound(L"CS_003_quote_bonkhead");
		playBonkHeadSound = false;
		SetCS_PeLittleStar(we, we->x, we->y - we->cbRect.top);
		SetCS_PeLittleStar(we, we->x, we->y - we->cbRect.top);
	}
	//移动控制
	if (!mychar->InAction())
	{
		MoveControl();
		//左
		if (we->input.GetLeft())
			we->lr = 0;
		else if (we->input.GetRight())
			we->lr = 1;
		//是否未移动人物
		if (stateResist.apply_resistH)
			mychar->ChangeState(mychar->stateStand.GetStateID());
		else
			mychar->ChangeState(mychar->stateMove.GetStateID());
		//是否在空中
		if (!we->cbRect.cbBottom)
			mychar->ChangeState(mychar->stateAir.GetStateID());
	}
}

void CS_PuppyControl::SetPhysice(bool bInWater)
{
	if (bInWater)
	{
		max_move = inwater_max_move;
		move = inwater_move;
		move2 = inwater_move2;
	}
	else
	{
		max_move = ground_max_move;
		move = ground_move;
		move2 = ground_move2;
	}
}

void CS_PuppyControl::MoveControl()
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_Puppy>(we);
	auto& stateResist = mychar->stateResist;
	//移动控制
	//左
	if (we->input.GetLeft() || we->axis[2] < 0)
	{
		stateResist.apply_resistH = false;
		//是否有手柄
		float joyMax_move;
		if (we->axis[2] < 0)
		{
			joyMax_move = max_move * -we->axis[2];
			//改变动画播放速度
			we->ani.image_counter_speed = -we->axis[2];
		}
		else
		{
			joyMax_move = max_move;
			we->ani.image_counter_speed = 1;
		}
		if (we->vx > -joyMax_move)
		{
			if (we->cbRect.cbLeft)
				we->vx = -joyMax_move / 2;
			else if (we->cbRect.cbBottom)
				if (we->vx - move > -joyMax_move)
					we->vx -= move;
				else
					we->vx = -joyMax_move;
			else
				if (we->vx - move2 > -joyMax_move)
					we->vx -= move2;
				else
					we->vx = -joyMax_move;
		}
	}
	//右
	else if (we->input.GetRight() || we->axis[3] > 0)
	{
		stateResist.apply_resistH = false;
		//是否有手柄
		float joyMax_move;
		if (we->axis[3] > 0)
		{
			joyMax_move = max_move * we->axis[3];
			//改变动画播放速度
			we->ani.image_counter_speed = we->axis[3];
		}
		else
		{
			joyMax_move = max_move;
			we->ani.image_counter_speed = 1;
		}
		if (we->vx < joyMax_move)
		{
			if (we->cbRect.cbRight)
				we->vx = joyMax_move / 2;
			else if (we->cbRect.cbBottom)
			{
				if (we->vx + move < joyMax_move)
					we->vx += move;
				else
					we->vx = joyMax_move;
			}
			else
			{
				if (we->vx + move2 < joyMax_move)
					we->vx += move2;
				else
					we->vx = joyMax_move;
			}
		}
	}
}

void CS_PuppyControl::JumpControl()
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_Puppy>(we);
	auto& stateGravity = mychar->stateGravity;
	//辅助跳跃
	if (we->input.GetJump() &&
		we->vy < 0)
		stateGravity.gravity = stateGravity.gravity_default * 0.4f;
	else
		stateGravity.gravity = stateGravity.gravity_default;
}
