#include "CS_IronheadAni.h"

CS_IronheadAni::CS_IronheadAni()
{
	ani.png = L"CS_CaveStoryMod";
	//启用碰撞
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	//设置碰撞框
	cbRect.left = cbRect.right = 28;
	cbRect.top = 11;
	cbRect.bottom = 12;
	drawBeforeOtherEntity = true;
}

void CS_IronheadAni::Step()
{
	switch (stateID)
	{
	case 0://原地缓慢的游动
		ani.image_counter_speed = 0.1f;
		if (floatSrcY < y)
			vy -= 3 / ts;
		if (floatSrcY > y)
			vy += 3 / ts;
		break;
	}
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//动画延迟一帧
	if (animation_delay)
		animation_delay = false;
	else
		Entity::Step();//执行运动
	//执行碰撞
	Entity::Collision();
}

void CS_IronheadAni::SetState(int stateID, int lr)
{
	this->lr = lr;
	this->stateID = stateID;
	switch (stateID)
	{
	case 0:
		floatSrcY = y;
		vx = 0;
		vy = -0x100 / ts;
		ani.sprite_index = lr == 0 ? L"mc_ironhead_l" : L"mc_ironhead_r";
		break;
	case 1://水平直线游泳
		animation_delay = true;
		if (lr == 0)
			vx = -animation_moveSpeed;
		else
			vx = animation_moveSpeed;
		vy = 0;
		ani.sprite_index = lr == 0 ? L"mc_ironhead_l" : L"mc_ironhead_r";
		break;
	case 2://停止直线前进
		vx = 0;
		this->stateID = 0;
		ani.sprite_index = lr == 0 ? L"mc_ironhead_l" : L"mc_ironhead_r";
		break;
	case 3://躺在上的
		vx = vy = 0;
		ani.sprite_index = L"mc_ironhead_lie";
		break;
	case 4://挣扎
		vx = vy = 0;
		ani.sprite_index = L"mc_ironhead_struggle";
		break;
	case 5://躺在上的 染色的
		vx = vy = 0;
		ani.sprite_index = L"mc_ironhead_lie_dyed";
		break;
	case 6://挣扎
		vx = vy = 0;
		ani.sprite_index = L"mc_ironhead_struggle_dyed";
		break;
	case 7://躺在上的 红色的
		vx = vy = 0;
		ani.sprite_index = L"mc_ironhead_red_lie";
		break;
	}
}

void CS_IronheadAni::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"normal")
	{
		if (stateID != 0)
			SetState(0, lr);
		else
			this->lr = lr;
	}
	else if (stateName == L"swim")
		SetState(1, lr);
	else if (stateName == L"swim_stop")
		SetState(2, lr);
	else if (stateName == L"lie")
		SetState(3, lr);
	else if (stateName == L"struggle")
		SetState(4, lr);
	else if (stateName == L"lie_dyed")
		SetState(5, lr);
	else if (stateName == L"struggle_dyed")
		SetState(6, lr);
	else if (stateName == L"red_lie")
		SetState(7, lr);
}

std::shared_ptr<Entity> CreateCS_IronheadAni()
{
	return std::make_shared<CS_IronheadAni>();
}
