From afc296650709ace22cc6bdc6efe3be42c5edf217 Mon Sep 17 00:00:00 2001 From: jadebenn Date: Sat, 14 Dec 2024 13:47:35 -0600 Subject: [PATCH] make polymorphic component storage type --- CMakeLists.txt | 1 + dECS/CMakeLists.txt | 4 +++ dECS/ECS.cpp | 8 +++++ dECS/ECS.h | 65 ++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/dECSTests/CMakeLists.txt | 9 +++++ tests/dECSTests/TestECS.cpp | 10 ++++++ 7 files changed, 98 insertions(+) create mode 100644 dECS/CMakeLists.txt create mode 100644 dECS/ECS.cpp create mode 100644 dECS/ECS.h create mode 100644 tests/dECSTests/CMakeLists.txt create mode 100644 tests/dECSTests/TestECS.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ff4e6b3..6482dbe9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/dECS/CMakeLists.txt b/dECS/CMakeLists.txt new file mode 100644 index 00000000..81f2190b --- /dev/null +++ b/dECS/CMakeLists.txt @@ -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") diff --git a/dECS/ECS.cpp b/dECS/ECS.cpp new file mode 100644 index 00000000..28345295 --- /dev/null +++ b/dECS/ECS.cpp @@ -0,0 +1,8 @@ +#include "ECS.h" +#include "dCommonVars.h" +#include +#include + +namespace Component { + +} diff --git a/dECS/ECS.h b/dECS/ECS.h new file mode 100644 index 00000000..612084a9 --- /dev/null +++ b/dECS/ECS.h @@ -0,0 +1,65 @@ +#pragma once +#include +#include +#include +#include +#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; + + // 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 + 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 m_Vec; + }; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d95a24bc..adb6baf5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,4 +6,5 @@ include(GoogleTest) # Add the subdirectories add_subdirectory(dCommonTests) +add_subdirectory(dECSTests) add_subdirectory(dGameTests) diff --git a/tests/dECSTests/CMakeLists.txt b/tests/dECSTests/CMakeLists.txt new file mode 100644 index 00000000..c9ca8239 --- /dev/null +++ b/tests/dECSTests/CMakeLists.txt @@ -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) diff --git a/tests/dECSTests/TestECS.cpp b/tests/dECSTests/TestECS.cpp new file mode 100644 index 00000000..61c2343e --- /dev/null +++ b/tests/dECSTests/TestECS.cpp @@ -0,0 +1,10 @@ +#include +#include "ECS.h" + +TEST(ECSTest, MakeStorage) { + const auto storage = Component::Storage(); + const Component::IStorage* const storagePtr = &storage; + + ASSERT_EQ(storagePtr->GetKind(), Component::Kind::PET); + ASSERT_NE(storagePtr->GetKind(), Component::Kind::DESTROYABLE); +}