#include "CS_KazumaAni.h"

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

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

void CS_KazumaAni::Step()
{
	//跟上移动平台
	Entity::BeginMovingPlatform();
	switch (stateID)
	{
	case 0:
	case 1:
	case 2:
		stateGravity.Step();
		break;
	case 3://电脑
		{
			auto& ani_no = ani.image_index;
			switch (act_no)
			{
			case 0:
				act_no = 1;
				ani_no = 0;
				ani_wait = 0;
				[[fallthrough]];
			case 1:
				if (++ani_wait > 2)
				{
					ani_wait = 0;
					++ani_no;
				}
				if (ani_no > 1)
					ani_no = 0;
				if (Random(0, 80) == 1)
				{
					act_no = 2;
					act_wait = 0;
					ani_no = 1;
				}
				if (Random(0, 120) == 10)
				{
					act_no = 3;
					act_wait = 0;
					ani_no = 2;
				}
				break;
			case 2:
				if (++act_wait > 40)
				{
					act_no = 3;
					act_wait = 0;
					ani_no = 2;
				}
				break;
			case 3:
				if (++act_wait > 80)
				{
					act_no = 1;
					ani_no = 0;
				}
				break;
			}
		}
		break;
	case 4:
		break;
	}
	//跟上移动平台
	Entity::EndMovingPlatform();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//动画延迟一帧
	if (animation_delay)
		animation_delay = false;
	else
		Entity::Step();//执行运动
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
}

void CS_KazumaAni::SetState(int stateID, int lr)
{
	this->lr = lr;
	this->stateID = stateID;
	//设置动画
	switch (stateID)
	{
	case 0://站立
		vx = 0;
		ani.sprite_index = lr == 0 ? L"mc_kazuma_stand_l" : L"mc_kazuma_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_kazuma_move_l" : L"mc_kazuma_move_r";
		}
		break;
	case 2://背对着
		vx = 0;
		ani.sprite_index = lr == 0 ? L"mc_kazuma_back_l" : L"mc_kazuma_back_r";
		break;
	case 3://电脑
		act_no = 0;
		vx = 0;
		ani.sprite_index = L"mc_kazuma_computer";
		break;
	case 4://跳舞
		ani.sprite_index = lr == 0 ? L"mc_kazuma_dance_l" : L"mc_kazuma_dance_r";
		break;
	}
}

void CS_KazumaAni::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"back")
		SetState(2, lr);
	else if (stateName == L"computer")
		SetState(3, lr);
	else if (stateName == L"dance")
		SetState(4, lr);
}

std::shared_ptr<Entity> CreateCS_KazumaAni()
{
	return std::make_shared<CS_KazumaAni>();
}
