#include "CS_FishHook.h"
#include "CS_PeWaterDrop.h"

CS_FishHook::CS_FishHook()
{
	//初始动画设置
	ani.png = L"CS_CaveStoryMod";
	ani.sprite_index = L"am_fishhook_90";
	//启用碰撞
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	cbRect.fullWaterBlock = true;
	//设置碰撞框
	cbRect.left = cbRect.right = cbRect.top = cbRect.bottom = 4;
}

void CS_FishHook::Init(std::weak_ptr<Entity> self)
{
	stateGravity.Init(shared_from_this());
	Entity::Init(self);
}

void CS_FishHook::Step()
{
	input.key = 0;
	switch (stateID)
	{
	case 0:
		break;
	case 1://移动到正在调查事件的玩家
		if(gameFunc->GetCheckEventPlayer)
		{
			auto wplayer = gameFunc->GetCheckEventPlayer(gameFunc->game).lock();
			if (wplayer)
			{
				if (abs(x - wplayer->x) <= 2 && abs(y - wplayer->y) <= 2)
				{
					stateID = 0;
					vx = 0;
					vy = 0;
				}
				else
				{
					if (x > wplayer->x)
						input.SetLeft(true);
					else
						input.SetRight(true);
					if (y > wplayer->y)
						input.SetUp(true);
					else
						input.SetDown(true);
				}
			}
		}
		else
		{
			stateID = 0;
		}
		break;
	}
	
	if (cbRect.inWater)
	{
		Move();
	}
	else
	{
		setWaterSplash = true;
		//在地上只能掉落
		stateGravity.Step();
	}
	//落水水花
	if (setWaterSplash && cbRect.inWater)
	{
		setWaterSplash = false;
		//生成水花
		SetCS_PeWaterDrop(shared_from_this(), shared_from_this());
	}
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
}

void CS_FishHook::SetState(int stateID, int lr)
{
	this->lr = lr;
	this->stateID = stateID;
	switch (stateID)
	{
	case 0:
		break;
	case 1://移动到正在调查事件的玩家
		break;
	}
}

void CS_FishHook::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"normal")
		SetState(0, lr);
	else if (stateName == L"move_to_check_event_player")
		SetState(1, lr);
}

void CS_FishHook::Move()
{
	bool apply_resistH = true;
	bool apply_resistV = true;
	bool opUp = false;
	bool opDown = false;
	bool opLeft = false;
	bool opRight = false;
	//移动控制
	//上
	if (input.GetUp() || axis[0] < 0)
	{
		apply_resistV = false;
		ud = -1;
		//是否有手柄
		float joyMax_move;
		if (axis[0] < 0)
		{
			joyMax_move = max_move * -axis[0];
			//改变动画播放速度
			ani.image_counter_speed = -axis[0];
		}
		else
		{
			joyMax_move = max_move;
			ani.image_counter_speed = 1;
		}
		if (vy > -joyMax_move)
		{
			if (vy - move > -joyMax_move)
				vy -= move;
			else
				vy = -joyMax_move;
		}
		opUp = true;
	}
	//下
	else if (input.GetDown() || axis[1] > 0)
	{
		apply_resistV = false;
		ud = 1;
		//是否有手柄
		float joyMax_move;
		if (axis[1] > 0)
		{
			joyMax_move = max_move * axis[1];
			//改变动画播放速度
			ani.image_counter_speed = axis[1];
		}
		else
		{
			joyMax_move = max_move;
			ani.image_counter_speed = 1;
		}
		if (vy < joyMax_move)
		{
			if (vy + move < joyMax_move)
				vy += move;
			else
				vy = joyMax_move;
		}
		opDown = true;
	}
	else
	{
		ud = 0;
	}
	//左
	if (input.GetLeft() || axis[2] < 0)
	{
		apply_resistH = false;
		lr = 0;
		//是否有手柄
		float joyMax_move;
		if (axis[2] < 0)
		{
			joyMax_move = max_move * -axis[2];
			//改变动画播放速度
			ani.image_counter_speed = -axis[2];
		}
		else
		{
			joyMax_move = max_move;
			ani.image_counter_speed = 1;
		}
		if (vx > -joyMax_move)
		{
			if (cbRect.cbLeft)
				vx = -joyMax_move / 2;
			else
			{
				if (vx - move > -joyMax_move)
					vx -= move;
				else
					vx = -joyMax_move;
			}
		}
		opLeft = true;
	}
	//右
	else if (input.GetRight() || axis[3] > 0)
	{
		apply_resistH = false;
		lr = 1;
		//是否有手柄
		float joyMax_move;
		if (axis[3] > 0)
		{
			joyMax_move = max_move * axis[3];
			//改变动画播放速度
			ani.image_counter_speed = axis[3];
		}
		else
		{
			joyMax_move = max_move;
			ani.image_counter_speed = 1;
		}
		if (vx < joyMax_move)
		{
			if (cbRect.cbRight)
				vx = joyMax_move / 2;
			else
			{
				if (vx + move < joyMax_move)
					vx += move;
				else
					vx = joyMax_move;
			}
		}
		opRight = true;
	}
	//阻力
	if (apply_resistH)
		vx /= 1.1f;
	if (apply_resistV)
		vy /= 1.1f;

	if (opLeft && opUp)
		ani.sprite_index = L"am_fishhook_225";
	else if (opRight && opUp)
		ani.sprite_index = L"am_fishhook_315";
	else if (opLeft && opDown)
		ani.sprite_index = L"am_fishhook_135";
	else if (opRight && opDown)
		ani.sprite_index = L"am_fishhook_45";
	else if (opLeft)
		ani.sprite_index = L"am_fishhook_180";
	else if (opRight)
		ani.sprite_index = L"am_fishhook_0";
	else if (opUp)
		ani.sprite_index = L"am_fishhook_270";
	else if (opDown)
		ani.sprite_index = L"am_fishhook_90";
}

std::shared_ptr<Entity> CreateCS_FishHook()
{
	return std::make_shared<CS_FishHook>();
}
