feat: add base item

This commit is contained in:
k0t9i
2023-01-24 02:49:35 +04:00
parent 6ca8752108
commit 43f38c7a57
20 changed files with 461 additions and 33 deletions

View File

@@ -0,0 +1,157 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include "EntityInterface.h"
namespace L2Bot::Domain::Entities
{
class BaseItem : public EntityInterface
{
public:
const uint32_t GetId() const
{
return m_ItemId;
}
void Update(const EntityInterface* other) override
{
const BaseItem* casted = static_cast<const BaseItem*>(other);
SaveState();
m_ItemId = casted->m_ItemId;
m_Amount = casted->m_Amount;
m_IsEquipped = casted->m_IsEquipped;
m_EnchantLevel = casted->m_EnchantLevel;
m_Mana = casted->m_Mana;
m_Name = casted->m_Name;
m_IconName = casted->m_IconName;
m_Description = casted->m_Description;
m_Weight = casted->m_Weight;
}
void SaveState() override
{
m_PrevState =
{
m_Amount,
m_IsEquipped,
m_EnchantLevel,
m_Mana,
m_Weight,
false
};
}
const bool IsEqual(const EntityInterface* other) const override
{
const BaseItem* casted = static_cast<const BaseItem*>(other);
return m_ItemId == casted->m_ItemId &&
m_Amount == casted->m_Amount &&
m_IsEquipped == casted->m_IsEquipped &&
m_EnchantLevel == casted->m_EnchantLevel &&
m_Mana == casted->m_Mana &&
m_Name == casted->m_Name &&
m_IconName == casted->m_IconName &&
m_Description == casted->m_Description &&
m_Weight == casted->m_Weight;
}
const std::vector<Serializers::Node> BuildSerializationNodes() const override
{
std::vector<Serializers::Node> result;
result.push_back({ "itemId", std::to_string(m_ItemId) });
if (m_PrevState.isNewState)
{
result.push_back({ "name", m_Name });
result.push_back({ "iconName", m_IconName });
result.push_back({ "description", m_Description });
}
if (m_PrevState.isNewState || m_Amount != m_PrevState.amount)
{
result.push_back({ "amount", std::to_string(m_Amount) });
}
if (m_PrevState.isNewState || m_IsEquipped != m_PrevState.isEquipped)
{
result.push_back({ "isEquipped", std::to_string(m_IsEquipped) });
}
if (m_PrevState.isNewState || m_EnchantLevel != m_PrevState.enchantLevel)
{
result.push_back({ "enchantLevel", std::to_string(m_EnchantLevel) });
}
if (m_PrevState.isNewState || m_Mana != m_PrevState.mana)
{
result.push_back({ "mana", std::to_string(m_Mana) });
}
if (m_PrevState.isNewState || m_Weight != m_PrevState.weight)
{
result.push_back({ "weight", std::to_string(m_Weight) });
}
return result;
}
BaseItem(
const uint32_t itemId,
const uint32_t amount,
const bool isEquipped,
const uint16_t enchantLevel,
const int32_t mana,
const std::string& name,
const std::string& iconName,
const std::string& description,
const uint16_t weight
) :
m_ItemId(itemId),
m_Amount(amount),
m_IsEquipped(isEquipped),
m_EnchantLevel(enchantLevel),
m_Mana(mana),
m_Name(name),
m_IconName(iconName),
m_Description(description),
m_Weight(weight)
{
}
BaseItem(const BaseItem* other) :
m_ItemId(other->m_ItemId),
m_Amount(other->m_Amount),
m_IsEquipped(other->m_IsEquipped),
m_EnchantLevel(other->m_EnchantLevel),
m_Mana(other->m_Mana),
m_Name(other->m_Name),
m_IconName(other->m_IconName),
m_Description(other->m_Description),
m_Weight(other->m_Weight)
{
}
BaseItem() = default;
virtual ~BaseItem() = default;
private:
struct GetState
{
uint32_t amount = 0;
bool isEquipped = 0;
uint16_t enchantLevel = 0;
int32_t mana = -1;
uint16_t weight = 0;
bool isNewState = true;
};
private:
uint32_t m_ItemId = 0;
uint32_t m_Amount = 0;
bool m_IsEquipped = 0;
uint16_t m_EnchantLevel = 0;
int32_t m_Mana = -1;
std::string m_Name = "";
std::string m_IconName = "";
std::string m_Description = "";
uint16_t m_Weight = 0;
GetState m_PrevState = GetState();
};
}