feat: change death logic

This commit is contained in:
Иванов Иван 2024-08-21 15:00:17 +02:00
parent ad7a4c3074
commit 4e2e108076
8 changed files with 64 additions and 12 deletions

View File

@ -11,13 +11,15 @@ namespace Client.Domain.ValueObjects
private uint maxMp;
private uint cp;
private uint maxCp;
private bool isDead;
public uint Hp { get => hp; set { if (value != hp) { hp = value; OnPropertyChanged(); OnPropertyChanged("MaxHp"); OnPropertyChanged("IsDead"); } } }
public uint MaxHp { get => Math.Max(hp, maxHp); set { if (value != maxHp) { maxHp = value; OnPropertyChanged(); OnPropertyChanged("IsDead"); } } }
public uint Hp { get => hp; set { if (value != hp) { hp = value; OnPropertyChanged(); OnPropertyChanged("MaxHp"); } } }
public uint MaxHp { get => Math.Max(hp, maxHp); set { if (value != maxHp) { maxHp = value; OnPropertyChanged(); } } }
public uint Mp { get => mp; set { if (value != mp) { mp = value; OnPropertyChanged(); } } }
public uint MaxMp { get => maxMp; set { if (value != maxMp) { maxMp = value; OnPropertyChanged(); } } }
public uint Cp { get => cp; set { if (value != cp) { cp = value; OnPropertyChanged(); } } }
public uint MaxCp { get => maxCp; set { if (value != maxCp) { maxCp = value; OnPropertyChanged(); } } }
public bool IsDead { get => isDead; set { if (value != isDead) { isDead = value; OnPropertyChanged(); } } }
public double HpPercent
{
@ -41,9 +43,7 @@ namespace Client.Domain.ValueObjects
}
}
public bool IsDead => MaxHp > 0 && hp == 0;
public VitalStats(uint hp, uint maxHp, uint mp, uint maxMp, uint cp, uint maxCp)
public VitalStats(uint hp, uint maxHp, uint mp, uint maxMp, uint cp, uint maxCp, bool isDead)
{
this.hp = hp;
this.maxHp = maxHp;
@ -51,6 +51,7 @@ namespace Client.Domain.ValueObjects
this.maxMp = maxMp;
this.cp = cp;
this.maxCp = maxCp;
this.isDead = isDead;
}
}
}

View File

@ -31,7 +31,7 @@ namespace L2Bot::Domain::Entities
WorldObject::Update(transform);
m_FullName = fullName;
m_VitalStats = vitalStats;
m_VitalStats.LoadFromOther(vitalStats);
m_Phenotype = phenotype;
m_ExperienceInfo = experienceInfo;
m_PermanentStats = permanentStats;
@ -93,6 +93,10 @@ namespace L2Bot::Domain::Entities
{
m_AttackerIds.clear();
}
void MarkAsDead()
{
m_VitalStats.MarkAsDead();
}
const std::vector<Serializers::Node> BuildSerializationNodes() const override
{

View File

@ -28,7 +28,7 @@ namespace L2Bot::Domain::Entities
m_IsHostile = isHostile;
m_NpcId = npcId;
m_FullName = fullName;
m_VitalStats = vitalStats;
m_VitalStats.LoadFromOther(vitalStats);
}
const size_t GetHash() const override
{
@ -45,6 +45,10 @@ namespace L2Bot::Domain::Entities
{
return "npc";
}
void MarkAsDead()
{
m_VitalStats.MarkAsDead();
}
const std::vector<Serializers::Node> BuildSerializationNodes() const override
{

View File

@ -19,7 +19,7 @@ namespace L2Bot::Domain::Entities
m_FullName = fullName;
m_Phenotype = phenotype;
m_VitalStats = vitalStats;
m_VitalStats.LoadFromOther(vitalStats);
}
const size_t GetHash() const override
{
@ -34,6 +34,10 @@ namespace L2Bot::Domain::Entities
{
return "player";
}
void MarkAsDead()
{
m_VitalStats.MarkAsDead();
}
const std::vector<Serializers::Node> BuildSerializationNodes() const override
{

View File

@ -10,9 +10,9 @@ namespace L2Bot::Domain::ValueObjects
class VitalStats : public Serializers::Serializable, public Entities::Hashable
{
public:
const bool IsAlive() const
const bool IsDead() const
{
return m_MaxHp <= 0 || m_Hp > 0;
return m_IsDead || (m_MaxHp > 0 && m_Hp < 0);
}
const uint32_t GetMaxHp() const
{
@ -46,9 +46,19 @@ namespace L2Bot::Domain::ValueObjects
std::hash<uint32_t>{}(m_MaxMp),
std::hash<uint32_t>{}(m_Mp),
std::hash<uint32_t>{}(m_MaxCp),
std::hash<uint32_t>{}(m_Cp)
std::hash<uint32_t>{}(m_Cp),
std::hash<bool>{}(m_IsDead)
});
}
void LoadFromOther(VitalStats other)
{
m_MaxHp = other.m_MaxHp;
m_Hp = other.m_Hp;
m_MaxMp = other.m_MaxMp;
m_Mp = other.m_Mp;
m_MaxCp = other.m_MaxCp;
m_Cp = other.m_Cp;
}
const std::vector<Serializers::Node> BuildSerializationNodes() const override
{
@ -59,9 +69,14 @@ namespace L2Bot::Domain::ValueObjects
{ L"maxMp", std::to_wstring(m_MaxMp) },
{ L"mp", std::to_wstring(m_Mp) },
{ L"maxCp", std::to_wstring(m_MaxCp) },
{ L"cp", std::to_wstring(m_Cp) }
{ L"cp", std::to_wstring(m_Cp) },
{ L"isDead", std::to_wstring(m_IsDead) }
};
}
void MarkAsDead()
{
m_IsDead = true;;
}
VitalStats(
uint32_t maxHp,
@ -90,5 +105,6 @@ namespace L2Bot::Domain::ValueObjects
uint32_t m_Mp = 0;
uint32_t m_MaxCp = 0;
uint32_t m_Cp = 0;
uint32_t m_IsDead = false;
};
}

View File

@ -83,6 +83,7 @@ namespace Interlude
{
Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} died", m_Hero->GetFullName().GetNickname());
m_Hero->ClearAttackers();
m_Hero->MarkAsDead();
}
else
{

View File

@ -111,6 +111,9 @@ namespace Interlude
m_Spoiled[casted.GetCreatureId()] = Enums::SpoilStateEnum::none;
}
}
if (m_Npcs.find(casted.GetCreatureId()) != m_Npcs.end()) {
m_Npcs[casted.GetCreatureId()]->MarkAsDead();
}
}
}

View File

@ -46,6 +46,13 @@ namespace Interlude
m_Players.clear();
}
void Init() override
{
Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::CreatureDiedEvent::name, [this](const Events::Event& evt) {
OnCreatureDied(evt);
});
}
PlayerRepository(const NetworkHandlerWrapper& networkHandler, const PlayerFactory& factory, const uint16_t radius) :
m_NetworkHandler(networkHandler),
m_Factory(factory),
@ -57,6 +64,18 @@ namespace Interlude
PlayerRepository() = delete;
virtual ~PlayerRepository() = default;
void OnCreatureDied(const Events::Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == Events::CreatureDiedEvent::name)
{
const auto casted = static_cast<const Events::CreatureDiedEvent&>(evt);
if (m_Players.find(casted.GetCreatureId()) != m_Players.end()) {
m_Players[casted.GetCreatureId()]->MarkAsDead();
}
}
}
private:
const PlayerFactory& m_Factory;
const NetworkHandlerWrapper& m_NetworkHandler;