#include "CS_CurlyControl.h"
#include "CS_Curly.h"
#include "CS_PeLittleStar.h"
const int CS_CurlySTATENAME_CONTROL = GetNextEntityStateID();
CS_CurlyControl::CS_CurlyControl()
{
	enable = true;
	SetPhysice(false);
	stateID = CS_CurlySTATENAME_CONTROL;
}

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

	auto mychar = std::static_pointer_cast<CS_Curly>(we);
	auto& faceToBack = mychar->faceToBack;
	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;
		faceToBack = false;
	}
	//下
	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 && !mychar->fishState)
	{
		we->gameFunc->PlaySound(L"CS_023_thud");
		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 (we->input.GetJump())
			faceToBack = false;
		//进入调查事件
		if (we->input.GetDown() && we->cbRect.cbBottom && !faceToBack_disable)
		{
			faceToBack = true;
		}
		//是否未移动人物
		if (stateResist.apply_resistH)
			mychar->ChangeState(mychar->stateStand.GetStateID());
		else
			mychar->ChangeState(mychar->stateMove.GetStateID());
		//是否在空中
		if (!we->cbRect.cbBottom)
			mychar->ChangeState(mychar->stateAir.GetStateID());
	}
	/*------------------事件调查------------------*/
	if (!we->input.GetDown())
		faceToBack_disable = false;
	if (!stateResist.apply_resistH)
		faceToBack = false;
	/*---------------没有按任何技能键--------------------*/
	if (!(!we->input.GetAtk() &&
		!we->input.GetSkill_1() &&
		!we->input.GetSkill_2() &&
		!we->input.GetSkill_3() &&
		!we->input.GetSkill_4() &&
		!we->input.GetSkill_5() &&
		!we->input.GetSkill_6()))
		faceToBack = false;
	//游戏事件是否运行到结尾
	if (we->faceToBackEnd)
	{
		we->faceToBackEnd = false;//使用这个标志
		faceToBack = false;//让玩家恢复调查事件的面对朝向
	}
	if (!we->cbRect.cbBottom)
	{
		faceToBack = false;
	}
	//面向里面
	if (faceToBack)
	{
		mychar->ChangeState(mychar->stateFaceToBack.GetStateID());
		we->inCheckingEvent = true;
		faceToBack_disable = true;
	}
	else
	{
		we->inCheckingEvent = false;
	}
}

void CS_CurlyControl::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_CurlyControl::MoveControl()
{
	auto we = e.lock();
	if (!we)
		return;

	auto mychar = std::static_pointer_cast<CS_Curly>(we);
	auto& stateResist = mychar->stateResist;
	bool bSetAniSpeed = false;
	if (mychar->fishState)
	{
		move = move2 = ground_move;
		max_move = ground_max_move;
	}
	//移动控制
	//左
	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;
			}
			if (mychar->fishState)
			{
				//改变动画播放速度
				we->ani.image_counter_speed = abs(we->vx) / abs(max_move);
				bSetAniSpeed = true;
			}
		}
	}
	//右
	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;
			}
			if (mychar->fishState)
			{
				//改变动画播放速度
				we->ani.image_counter_speed = abs(we->vx) / abs(max_move);
				bSetAniSpeed = true;
			}
		}
	}
	if (mychar->fishState && !bSetAniSpeed)
	{
		if (mychar->stateMove.GetEnable())
			we->ani.image_index = 0;

		we->ani.image_counter_speed = 0;
	}
}

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

	auto mychar = std::static_pointer_cast<CS_Curly>(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;
}
