#include "CS_BlueRobotAni.h"
#include <PeSmog.h>

CS_BlueRobotAni::CS_BlueRobotAni()
{
	ani.png = L"CS_CaveStoryMod";
	//启用碰撞
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	//设置碰撞框
	cbRect.left = cbRect.right = 5;
	cbRect.top = 6;
	cbRect.bottom = 8;
}

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

void CS_BlueRobotAni::Step()
{
	//跟上移动平台
	Entity::BeginMovingPlatform();
	stateGravity.Step();
	switch (stateID)
	{
	case 0:
		//眨眼
		if (Random(0, 120) == 10)
		{
			wink_counter = 8;
		}
		if (wink_counter > 0)
		{
			wink_counter--;
			ani.image_index = 1;
		}
		else
		{
			ani.image_index = 0;
		}
		break;
	}
	if (stateID != 1)
	{
		//阻力
		vx = 0;
	}
	//跟上移动平台
	Entity::EndMovingPlatform();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//动画延迟一帧
	if (animation_delay)
		animation_delay = false;
	else
		Entity::Step();//执行运动
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
}

void CS_BlueRobotAni::SetState(int stateID, int lr)
{
	if (stateID < 200)
	{
		this->lr = lr;
		this->stateID = stateID;
		//设置动画
		switch (stateID)
		{
		case 0://站立
			vx = 0;
			ani.sprite_index = lr == 0 ? L"mc_bluerobot_stand_l" : L"mc_bluerobot_stand_r";
			break;
		case 1://走路
			{
				animation_delay = true;
				if (lr == 0)
					vx = -animation_moveSpeed;
				else
					vx = animation_moveSpeed;
				ani.sprite_index = lr == 0 ? L"mc_bluerobot_move_l" : L"mc_bluerobot_move_r";
			}
			break;
		}
	}
	else
	{
		switch (stateID)
		{
		case 200://变成鱼
			cbRect.left = cbRect.right = 5;
			cbRect.top = 4;
			cbRect.bottom = 4;
			ani.sprite_index = this->lr == 0 ? L"mc_robotfish_stand_l" : L"mc_robotfish_stand_r";
			SetPeSmog(shared_from_this(), x, y, 3);
			break;
		case 201://变回来
			cbRect.left = cbRect.right = 5;
			cbRect.top = 6;
			cbRect.bottom = 8;
			this->stateID = 0;
			SetPeSmog(shared_from_this(), x, y, 3);
			break;
		}
	}
}

void CS_BlueRobotAni::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"normal")
		SetState(0, lr);
	else if (stateName == L"walk")
		SetState(1, lr);
	else if (stateName == L"transform_fish")
		SetState(200, lr);
	else if (stateName == L"transform_back")
		SetState(201, lr);
}

std::shared_ptr<Entity> CreateCS_BlueRobotAni()
{
	return std::make_shared<CS_BlueRobotAni>();
}
