#include "CS_PuppyAni.h"
#include "CS_PeSmoke.h"
#include "CS_PeZzz.h"

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

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

void CS_PuppyAni::Step()
{
	switch (stateID)
	{
	case 0://站立
		//眨眼
		if (Random(0, 120) == 10)
		{
			wink_counter = 8;
		}
		if (wink_counter > 0)
		{
			wink_counter--;
			ani.hideLayerList.erase(0);
			ani.hideLayerList.insert(1);
		}
		else
		{
			ani.hideLayerList.erase(1);
			ani.hideLayerList.insert(0);
		}
		break;
	case 1://下落
		vx = 0;
		if (cbRect.cbBottom)
		{
			for (int i = 0; i < 4; i++)
			{
				auto pe = std::make_shared<CS_PeSmoke>();
				pe->x = x + Random(-12, 12);
				pe->y = y + Random(-12, 12);
				pe->vx = Random(-341, 341) / ts;
				pe->vy = Random(-0x600, 0) / ts;
				gameFunc->SetEntity(shared_from_this(), pe);
			}
			gameFunc->PlaySound(L"CS_071_little_crash");
			SetState(2, lr);
		}
		else
		{
			//重力
			stateGravity.State();
			stateWater();
		}
		break;
	case 2://趴在地上
		if (abs(vx) > 0.001f)
			vx /= 1.05f;
		else
			vx = 0;
		break;
	case 3://吠叫
		if (action_timecounter > 0)
			action_timecounter--;
		if (action_timecounter == 0)
			SetState(0, lr);
		break;
	case 4://移动
		break;
	case 5://睡着了
		if (++act_wait > 100)
		{
			act_wait = 0;
			SetPeCS_Zzz(shared_from_this(), x, y);
		}
		if (abs(vx) > 0.001f)
			vx /= 1.05f;
		else
			vx = 0;
		break;
	}
	//风场
	Entity::Wind();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
}

void CS_PuppyAni::SetState(int stateID, int lr)
{
	this->lr = lr;
	this->stateID = stateID;
	ani.hideLayerList.erase(1);
	ani.hideLayerList.insert(0);
	//设置动画
	switch (stateID)
	{
	case 0://站立
		vx = 0;
		if(bSit)
			ani.sprite_index = lr == 0 ? L"mc_puppy_sit_l" : L"mc_puppy_sit_r";
		else
			ani.sprite_index = lr == 0 ? L"mc_puppy_stand_l" : L"mc_puppy_stand_r";
		break;
	case 1://下落
		ani.sprite_index = lr == 0 ? L"mc_puppy_hurt_l" : L"mc_puppy_hurt_r";
		break;
	case 2://趴在地上
		ani.sprite_index = lr == 0 ? L"mc_puppy_lie_l" : L"mc_puppy_lie_r";
		break;
	case 3://吠叫
		action_timecounter = 8;
		if (bSit)
			ani.sprite_index = lr == 0 ? L"mc_puppy_sit_barking_l" : L"mc_puppy_sit_barking_r";
		else
			ani.sprite_index = lr == 0 ? L"mc_puppy_stand_barking_l" : L"mc_puppy_stand_barking_r";
		gameFunc->PlaySound(L"CS_105_puppy_bark");
		break;
	case 4://移动
		animation_delay = true;
		ani.sprite_index = lr == 0 ? L"mc_puppy_move_l" : L"mc_puppy_move_r";
		vx = lr == 0 ? -animation_moveSpeed : animation_moveSpeed;
		break;
	case 5://睡着
		ani.sprite_index = lr == 0 ? L"mc_puppy_lie_l" : L"mc_puppy_lie_r";
		break;
	}
}

void CS_PuppyAni::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"normal")
		SetState(0, lr);
	else if (stateName == L"fall")
		SetState(1, lr);
	else if (stateName == L"lie")
		SetState(2, lr);
	else if (stateName == L"bark")
		SetState(3, lr);
	else if (stateName == L"walk")
		SetState(4, lr);
	else if (stateName == L"sleep")
		SetState(5, lr);
}

void CS_PuppyAni::stateWater()
{
	stateGravity.SetPhysice(cbRect.inWater);
	if (cbRect.pxaBottom == 0x12)
		stateGravity.SetPhysice(true);
	//入水水花效果
	if (cbRect.inWater)
	{
		if (fallToWater && setSplash && vy >= stateGravity.max_fall_speed / 2)
		{
			//产生水花
			auto nE = gameFunc->GameCreateEntity(L"pe_splash");
			nE->x = x;
			nE->y = y;
			//岩浆红
			if (cbRect.inLava)
				nE->color = RGBA(0xff, 0, 0, 0xff);
			gameFunc->SetEntity(shared_from_this(), nE);
			fallToWater = false;
		}
		else
		{
			//速度未达到但仍然在水中
			setSplash = false;
		}
	}
	else
	{
		//重置水花产生条件
		fallToWater = true;
		setSplash = true;
	}
}

std::shared_ptr<Entity> CreateCS_PuppyAni()
{
	return std::make_shared<CS_PuppyAni>();
}
