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

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

void CS_ColonAni::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;
	case 3://躺下（在床上）
	case 4:
		vx = 0;
		vy = 0;
		//产生zzz
		if (++act_wait > 100)
		{
			act_wait = 0;
			SetPeCS_Zzz(shared_from_this(), x, y);
		}
		break;
	case 5:
	case 6://坐在椅子上
		vx = 0;
		vy = 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;
	case 7://睡觉（在床上）
	case 8:
		vx = 0;
		vy = 0;
		//产生zzz
		if (++act_wait > 100)
		{
			act_wait = 0;
			SetPeCS_Zzz(shared_from_this(), x, y);
		}
		break;
	}
	//跟上移动平台
	Entity::EndMovingPlatform();
	//跟上移动平台
	Entity::EndMovingPlatform();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//动画延迟一帧
	if (animation_delay)
		animation_delay = false;
	else
		Entity::Step();//执行运动
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
}

void CS_ColonAni::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_colon_stand_l" : L"mc_colon_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_colon_move_l" : L"mc_colon_move_r";
			}
			break;
		case 2://背对着
			vx = 0;
			ani.sprite_index = lr == 0 ? L"mc_colon_back_l" : L"mc_colon_back_r";
			break;
		case 3://躺下（在床上）
			y -= 6;
			x -= 9;
			vx = 0;
			vy = 0;
			ani.sprite_index = lr == 0 ? L"mc_colon_lie_l" : L"mc_colon_lie_r";
			break;
		case 4:
			vx = 0;
			ani.sprite_index = lr == 0 ? L"mc_colon_lie_l" : L"mc_colon_lie_r";
			break;
		case 5://坐着
			vx = 0;
			ani.sprite_index = lr == 0 ? L"mc_colon_sit_l" : L"mc_colon_sit_r";
			break;
		case 6://坐在椅子上
			vx = 0;
			y -= 3;
			ani.sprite_index = lr == 0 ? L"mc_colon_sit_l" : L"mc_colon_sit_r";
			break;
		case 7://睡觉（在床上）
			y -= 6;
			x -= 9;
			vx = 0;
			vy = 0;
			ani.sprite_index = lr == 0 ? L"mc_colon_sleep_l" : L"mc_colon_sleep_r";
			break;
		case 8://睡觉（在地板上）
			vx = 0;
			vy = 0;
			ani.sprite_index = lr == 0 ? L"mc_colon_sleep_l" : L"mc_colon_sleep_r";
			break;
		}
	}
}

void CS_ColonAni::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"lie")
		SetState(3, lr);
	else if (stateName == L"sleep_on_bed")
		SetState(7, lr);
	else if (stateName == L"sit")
		SetState(5, lr);
	else if (stateName == L"sit_on_chair")
		SetState(6, lr);
	else if (stateName == L"sleep")
		SetState(8, lr);
}

std::shared_ptr<Entity> CreateCS_ColonAni()
{
	return std::make_shared<CS_ColonAni>();
}
