#include "CS_ChabaAni.h"
#include "CS_PeZzz.h"
#include "CS_PeSmoke.h"

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

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

void CS_ChabaAni::Step()
{
	//跟上移动平台
	Entity::BeginMovingPlatform();
	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;
		}
		stateGravity.Step();
		break;
	case 1://走路
		stateGravity.Step();
		break;
	case 2://坐在床上
		break;
	case 3://躺在床上
		break;
	case 4://睡在床上（产生zzz）
		//产生zzz
		if (++act_wait > 100)
		{
			act_wait = 0;
			SetPeCS_Zzz(shared_from_this(), x, y);
		}
		break;
	case 5://摔下床
		if (bSetSound && cbRect.cbBottom)
		{
			bSetSound = false;
			gameFunc->PlaySound(L"CS_071_little_crash");
			SetCS_PeSmokeMedium(shared_from_this(), x, y);
		}
		stateGravity.Step();
		break;
	case 6://挣扎
		break;
	}
	//跟上移动平台
	Entity::EndMovingPlatform();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//动画延迟一帧
	if (animation_delay)
		animation_delay = false;
	else
		Entity::Step();//执行运动
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
}

void CS_ChabaAni::SetState(int stateID, int lr)
{
	this->lr = lr;
	this->stateID = stateID;
	if (el.arg.length())
	{
		auto str = el.arg;
		el.arg = L"";
		SetState(str, el.flag.lr ? 1 : 0);
		return;
	}
	//设置动画
	switch (stateID)
	{
	case 0://站立
		vx = 0;
		ani.sprite_index = lr == 0 ? L"mc_chaba_stand_l" : L"mc_chaba_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_chaba_move_l" : L"mc_chaba_move_r";
		}
		break;
	case 2://坐在床上
		vx = 0;
		y -= 6;
		ani.sprite_index = lr == 0 ? L"mc_chaba_sit_l" : L"mc_chaba_sit_r";
		break;
	case 3://躺在床上
		vx = 0;
		ani.sprite_index = lr == 0 ? L"mc_chaba_lie_l" : L"mc_chaba_lie_r";
		break;
	case 4://睡在床上（产生zzz）
		vx = 0;
		ani.sprite_index = lr == 0 ? L"mc_chaba_lie_l" : L"mc_chaba_lie_r";
		break;
	case 5://摔下床
		bSetSound = true;
		break;
	case 6://挣扎
		gameFunc->PlaySound(L"CS_053_enemy_hurt_small");
		ani.sprite_index = lr == 0 ? L"mc_chaba_struggle_l" : L"mc_chaba_struggle_r";
		break;
	}
}

void CS_ChabaAni::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"sit_on_bed")
		SetState(2, lr);
	else if (stateName == L"lie_on_bed")
		SetState(3, lr);
	else if (stateName == L"sleep_on_bed")
		SetState(4, lr);
	else if (stateName == L"fall_on_bed")
		SetState(5, lr);
	else if (stateName == L"struggle")
		SetState(6, lr);
	else if (stateName == L"sleep_on_bed2")
	{
		vx = 0;
		y -= 6;
		x -= 6;
		SetState(4, lr);
	}
}

std::shared_ptr<Entity> CreateCS_ChabaAni()
{
	return std::make_shared<CS_ChabaAni>();
}
