make polymorphic component storage type

This commit is contained in:
jadebenn 2024-12-14 13:47:35 -06:00
parent 34618607c3
commit afc2966507
7 changed files with 98 additions and 0 deletions

View File

@ -303,6 +303,7 @@ file(
# Add our library subdirectories for creation of the library object
add_subdirectory(dCommon)
add_subdirectory(dDatabase)
add_subdirectory(dECS)
add_subdirectory(dChatFilter)
add_subdirectory(dNet)
add_subdirectory(dScripts) # Add for dGame to use

4
dECS/CMakeLists.txt Normal file
View 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
View File

@ -0,0 +1,8 @@
#include "ECS.h"
#include "dCommonVars.h"
#include <string>
#include <vector>
namespace Component {
}

65
dECS/ECS.h Normal file
View 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;
};
}

View File

@ -6,4 +6,5 @@ include(GoogleTest)
# Add the subdirectories
add_subdirectory(dCommonTests)
add_subdirectory(dECSTests)
add_subdirectory(dGameTests)

View File

@ -0,0 +1,9 @@
add_executable(dECSTests
"TestECS.cpp"
)
# Link needed libraries
target_link_libraries(dECSTests PRIVATE dCommon dECS GTest::gtest_main)
# Discover the tests
gtest_discover_tests(dECSTests)

View File

@ -0,0 +1,10 @@
#include <gtest/gtest.h>
#include "ECS.h"
TEST(ECSTest, MakeStorage) {
const auto storage = Component::Storage<Component::Pet>();
const Component::IStorage* const storagePtr = &storage;
ASSERT_EQ(storagePtr->GetKind(), Component::Kind::PET);
ASSERT_NE(storagePtr->GetKind(), Component::Kind::DESTROYABLE);
}