mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-09 20:24:16 +00:00
make polymorphic component storage type
This commit is contained in:
4
dECS/CMakeLists.txt
Normal file
4
dECS/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
add_library(dECS STATIC "ECS.cpp")
|
||||
target_include_directories(dECS PUBLIC .)
|
||||
target_link_libraries(dECS PRIVATE dCommon)
|
||||
target_compile_options(dECS PRIVATE "-Wall")
|
8
dECS/ECS.cpp
Normal file
8
dECS/ECS.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "ECS.h"
|
||||
#include "dCommonVars.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Component {
|
||||
|
||||
}
|
65
dECS/ECS.h
Normal file
65
dECS/ECS.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
#include <bitset>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include "dCommonVars.h"
|
||||
#include "eReplicaComponentType.h"
|
||||
|
||||
namespace Component {
|
||||
constexpr size_t MAX_KINDS { 32 };
|
||||
|
||||
enum class Kind : uint32_t {
|
||||
NONE = 0,
|
||||
DESTROYABLE = 7,
|
||||
PET = 26,
|
||||
};
|
||||
|
||||
using Signature = std::bitset<MAX_KINDS>;
|
||||
|
||||
// Components
|
||||
struct Destroyable;
|
||||
struct Pet {
|
||||
static constexpr Kind KIND_ID = Kind::PET;
|
||||
|
||||
// The ID under which this pet is stored in the database (if it's tamed)
|
||||
LWOOBJID m_DatabaseId;
|
||||
|
||||
// The ID of the item from which this pet was creat
|
||||
LWOOBJID m_ItemId;
|
||||
|
||||
// The name of this pet
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class IStorage {
|
||||
public:
|
||||
constexpr IStorage(const Kind kind) : m_Kind{ kind } {}
|
||||
virtual ~IStorage() = default;
|
||||
constexpr virtual void Remove(const size_t index) = 0;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Kind GetKind() const {
|
||||
return m_Kind;
|
||||
}
|
||||
|
||||
protected:
|
||||
Kind m_Kind;
|
||||
};
|
||||
|
||||
template <typename C>
|
||||
class Storage : public IStorage {
|
||||
public:
|
||||
constexpr Storage() : IStorage{ C::KIND_ID } {}
|
||||
|
||||
constexpr void Remove(const size_t index) override {
|
||||
auto& elementToDelete = m_Vec.at(index);
|
||||
auto& lastElement = m_Vec.back();
|
||||
std::swap(elementToDelete, lastElement);
|
||||
m_Vec.pop_back();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<C> m_Vec;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user