#include "CS_Enemy_SoapA.h"
#include "CS_PeSmoke.h"
#include "CS_Exp.h"
CS_Enemy_SoapA::CS_Enemy_SoapA()
{
	type = 2;
	ani.png = L"CS_CaveStoryMod";
	cbRect.enableCbRect = true;
	cbRect.enableSoild = true;
	cbRect.enableSelfSoild = true;
	cbRect.left = cbRect.right = 6;
	cbRect.top = cbRect.bottom = 5;
	cbRect.fullWaterBlock = true;
	hp_max = hp = 2;
	dropExp = 1;
	drawBeforeOtherEntity = true;
	el.name = CS_ENEMY_SOAPA_CREATENAME;
	stateGravity.SetGravityScale(0.75f);
}

void CS_Enemy_SoapA::Init(std::weak_ptr<Entity> self)
{
	stateGravity.Init(shared_from_this());
	stateResist.Init(shared_from_this());
	stateSplash.Init(shared_from_this());
	auto watkZone = std::make_shared<CS_Enemy_SoapFrontDmgRect>(shared_from_this());
	atkZone = watkZone;
	gameFunc->SetEntity(shared_from_this(), watkZone);
	Entity::Init(self);
}

void CS_Enemy_SoapA::Step()
{
	auto& ani_no = ani.image_index;
	//跟上移动平台
	Entity::BeginMovingPlatform();
	stateGravity.Step();
	stateResist.Step();
	stateSplash.Step();
	if (shock_counter == 0)
	{
		if (cbRect.cbLeft)
		{
			lr = 1;
			vx = 0x200 / ts;
		}

		if (cbRect.cbRight)
		{
			lr = 0;
			vx = -0x200 / ts;
		}
		//跳跃
		if (cbRect.cbBottom && Random(0, 240) == 10)
		{
			vy = -0x380 / ts;
		}
		if (lr == 0)
			vx = -0x100 / ts;
		else
			vx = 0x100 / ts;
		if (cbRect.cbBottom)
			ani.sprite_index = lr == 0 ? L"ls_soapA_move_l" : L"ls_soapA_move_r";
		else
			ani.sprite_index = lr == 0 ? L"ls_soapA_jump_l" : L"ls_soapA_jump_r";
	}
	auto watkZone = atkZone.lock();
	if (watkZone)
	{
		if (lr == 0)
		{
			watkZone->x = x - 5;
			watkZone->last_x = last_x - 5;
			watkZone->y = y;
			watkZone->last_y = last_y;
		}
		else
		{
			watkZone->x = x + 5;
			watkZone->last_x = last_x + 5;
			watkZone->y = y;
			watkZone->last_y = last_y;
		}
	}
	//跟上移动平台
	Entity::EndMovingPlatform();
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
	//维持在斜坡上
	Entity::KeepOnSlopes();
}

void CS_Enemy_SoapA::SetState(int stateID, int lr)
{
	this->lr = lr;
	ani.sprite_index = lr == 0 ? L"ls_soapA_stand_l" : L"ls_soapA_stand_r";
	ani.image_index = 0;
}

bool CS_Enemy_SoapA::Hurt(std::weak_ptr<Entity> src, int getDamage, float angle, bool bTakedown, int elemType, float shockForce, float heavy, int* outRealDamage)
{
	if (getDamage == 0)
		return false;
	*outRealDamage = getDamage;
	shock_counter = 30;
	hp -= getDamage;
	if (hp > 0)
	{
		gameFunc->PlaySound(L"CS_055_enemy_squeak2");
	}
	else
	{
		gameFunc->PlaySound(L"CS_071_little_crash");
		//创建云团
		SetCS_PeSmokeMedium(shared_from_this(), x, y);
		hp = 0;
		//掉落物品
		SetDropItem(shared_from_this(), x, y, dropExp);
		Destroy();
	}
	//伤害冲击处理
	if (shockForce > 0)
	{
		//被攻击弹反
		float radian = angle * deg2rad;
		vx = shockForce * cosf(radian);
		vy = shockForce * sinf(radian);
		vy -= 1;
	}
	vx = lr == 0 ? -1.0f : 1.0f;
	//显示伤害值
	if (gameFunc->dmgNum)
		gameFunc->SetDmgNum(gameFunc->dmgNum, getDamage, x, y - 16, 0, shared_from_this());
	return true;
}

void CS_Enemy_SoapA::Destroy()
{
	auto watkZone = atkZone.lock();
	if (watkZone)
		watkZone->Destroy();
	Entity::Destroy();
}

std::shared_ptr<Entity> CreateCS_Enemy_SoapA()
{
	return std::make_shared<CS_Enemy_SoapA>();
}
