#include "CS_FortBlockSmall.h"
#include "CS_PeSmoke.h"
#include "CS_Exp.h"
#include <PeSmog.h>

CS_FortBlockSmall::CS_FortBlockSmall()
{
	ani.png = L"CS_CaveStoryMod";
	ani.sprite_index = L"ls_fortblock_small";
	cbRect.left = 8;
	cbRect.top = 8;
	cbRect.right = 8;
	cbRect.bottom = 8;
	cbRect.enableSelfSoild = true;
	cbRect.enableMovePlatform = true;
	hp_max = hp = 500;
	dropExp = 20;
}

void CS_FortBlockSmall::Init(std::weak_ptr<Entity> self)
{
	if (gameFunc->pGameOption_difficulty)
		if (*gameFunc->pGameOption_difficulty == 0)
			hp_max = hp = 250;
	Entity::Init(self);
}

void CS_FortBlockSmall::Step()
{
	if (vy > 0)
	{
		if (++move_counter % 8 == 0)
			gameFunc->PlaySound(L"CS_026_quake");
	}
	if (bSetLanding && cbRect.cbBottom)
	{
		bSetLanding = false;
		for (int i = 0; i < 4; ++i)
		{
			auto pe = std::make_shared<CS_PeSmoke>();
			pe->x = x;
			pe->y = y;
			pe->vx = Random(-341, 341) / ts;
			pe->vy = Random(-0x600, 0) / ts;
			gameFunc->SetEntity(shared_from_this(), pe);
		}
	}
	ani.image_index = (int)((1 - (float)hp / hp_max) * 5);
	//清除最后一次碰撞结果
	Entity::ClearCollisionResult();
	//执行运动
	Entity::Step();
	//执行碰撞
	Entity::Collision();
}

void CS_FortBlockSmall::SetState(std::wstring stateName, int lr)
{
	if (stateName == L"press")
	{
		vy = moveSpeed;
		bSetLanding = true;
		cbRect.enableCbRect = true;
		cbRect.enableSoild = true;
	}
	else if (stateName == L"destroy")
	{
		for (int i = 0; i < 4; ++i)
		{
			auto pe = std::make_shared<CS_PeSmoke>();
			pe->x = x;
			pe->y = y;
			pe->vx = Random(-341, 341) / ts;
			pe->vy = Random(-0x600, 0) / ts;
			gameFunc->SetEntity(shared_from_this(), pe);
		}
		gameFunc->PlaySound(L"CS_072_big_crash");
		Destroy();
	}
	else if (stateName == L"set_smog")
	{
		SetPeSmog(shared_from_this(), x, y, 1, 1, 90);
	}
}

bool CS_FortBlockSmall::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;
	hp -= getDamage;
	if (hp > 0)
	{
		gameFunc->PlaySound(L"CS_054_enemy_hurt_cool");
	}
	else
	{
		gameFunc->PlaySound(L"CS_072_big_crash");
		//创建云团
		SetCS_PeSmokeLarge(shared_from_this(), x, y);
		hp = 0;
		//掉落物品
		SetDropItem(shared_from_this(), x, y, dropExp);
		Destroy();
	}
	//显示伤害值
	if (gameFunc->dmgNum)
		gameFunc->SetDmgNum(gameFunc->dmgNum, getDamage, x, y - 16, 0, shared_from_this());
	return true;
}

std::shared_ptr<Entity> CreateCS_FortBlockSmall()
{
	return std::make_shared<CS_FortBlockSmall>();
}
