#include "CS_ItohAni.h"

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

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

void CS_ItohAni::Step()
{
	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;
	}
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//动画延迟一帧
	if (animation_delay)
		animation_delay = false;
	else
		Entity::Step();//执行运动
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
}

void CS_ItohAni::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_itoh_stand_l" : L"mc_itoh_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_itoh_move_l" : L"mc_itoh_move_r";
			}
			break;
		}
	}
}

void CS_ItohAni::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"normal")
	{
		ud = 0;
		SetState(0, lr);
	}
	else if (stateName == L"walk")
		SetState(1, lr);
	else if (stateName == L"hold_noodle")
		ani.hideLayerList.erase(0);
	else if(stateName == L"empty")
		ani.hideLayerList.insert(0);
}

std::shared_ptr<Entity> CreateCS_ItohAni()
{
	return std::make_shared<CS_ItohAni>();
}
