From d849aae7a61c4c945230c6af051e8c9d5a071380 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 20 May 2018 21:28:00 +0100 Subject: Component pool now in entities --- .dir-locals.el | 2 +- examples/simple/main.cpp | 2 + yage/entity/componentmanager.h | 17 ------- yage/entity/entity.cpp | 41 +++++++++++++++++ yage/entity/entity.h | 100 +++++++++++++++++++++++++++++++++++++++++ yage/entity/entitymanager.cpp | 39 ---------------- yage/entity/entitymanager.h | 77 ------------------------------- yage/entity/space.h | 6 ++- 8 files changed, 149 insertions(+), 135 deletions(-) delete mode 100644 yage/entity/componentmanager.h create mode 100644 yage/entity/entity.cpp create mode 100644 yage/entity/entity.h delete mode 100644 yage/entity/entitymanager.cpp delete mode 100644 yage/entity/entitymanager.h diff --git a/.dir-locals.el b/.dir-locals.el index f1672c13..40e6adb0 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -1,3 +1,3 @@ ((nil . ((projectile-project-compilation-cmd . "mkdir -p build && cd build && cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. && ninja -j4") - (projectile-project-test-cmd . "cd build/tests && ctest") + (projectile-project-test-cmd . "cd build/tests && ctest -j4 --schedule-random") (projectile-project-run-cmd . "./build/bin/simplegame")))) diff --git a/examples/simple/main.cpp b/examples/simple/main.cpp index 0869f9b1..c25c41df 100644 --- a/examples/simple/main.cpp +++ b/examples/simple/main.cpp @@ -1,5 +1,7 @@ #include +#include + int main() { std::cout << "Hello world"; diff --git a/yage/entity/componentmanager.h b/yage/entity/componentmanager.h deleted file mode 100644 index fa1096e7..00000000 --- a/yage/entity/componentmanager.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include - -#include "entitymanager.h" - -namespace yage -{ - -template -class ComponentMapper -{ -private: - std::map map_; -}; - -} // namespace yage diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp new file mode 100644 index 00000000..c7e8fd50 --- /dev/null +++ b/yage/entity/entity.cpp @@ -0,0 +1,41 @@ +/** --------------------------------------------------------------------------- + * @file: entity.cpp + * + * Copyright (c) 2017 Yann Herklotz Grave + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#include "entity.h" + +#include + +namespace yage +{ + +BaseComponent::Group BaseComponent::group_id_counter_ = 0; + +EntityManager::EntityManager(Space *space) : space_(space) {} + +EntityManager::EntityManager(Space *space, std::size_t n) : space_(space) +{ + entities_.reserve(n); +} + +Entity EntityManager::createEntity() +{ + Entity entity = next_entity_++; + entities_.push_back(entity); + return entity; +} + +void EntityManager::deleteEntity(Entity entity) +{ + auto index = std::find_if(entities_.begin(), entities_.end(), + [&](Entity &value) { return value == entity; }); + if (index != entities_.end()) { + entities_.erase(index); + } +} + +} // namespace yage diff --git a/yage/entity/entity.h b/yage/entity/entity.h new file mode 100644 index 00000000..26d391b5 --- /dev/null +++ b/yage/entity/entity.h @@ -0,0 +1,100 @@ +/** --------------------------------------------------------------------------- + * @file: entity.h + * + * Copyright (c) 2017 Yann Herklotz Grave + * MIT License, see LICENSE file for more details. + * ---------------------------------------------------------------------------- + */ + +#pragma once + +#include +#include + +namespace yage +{ + +class Space; +class ComponentPool; + +typedef unsigned int Entity; + +/** + * Manages entities in a space. + */ +class EntityManager +{ +public: + /** + * Default instance of an EntityManager. + */ + EntityManager() = default; + + /** + * Creates an instance of the entity manager, which refers back to the space + * it was created in and belongs to. + * + * @param space Current space that the EntityManager belongs to. + */ + EntityManager(Space *space); + + /** + * Creates an instance of the entitiy manager with an initial size. + * + * @param space Current space that the EntityManager belongs to. + * @param n Initial size of the EntityManager. + */ + EntityManager(Space *space, std::size_t n); + + /** + * Creates an Entity and returns the handle to the entity, which can then be + * used by the user to do operations on it. + * + * @return The handle to the entity that was created in the space. + */ + Entity createEntity(); + + /** + * Delete an entity. + */ + void deleteEntity(Entity entity); + +private: + /** + * The next available handle to give to the user. + */ + Entity next_entity_ = 0; + + /** + * The space that the entity manager belongs to. + */ + Space *space_ = nullptr; + + /** + * The entities in the current space. + */ + std::vector entities_; + + std::vector> components; +}; + +class BaseComponent { +public: + typedef unsigned Group; + +protected: + static Group group_id_counter_; +}; + +template +class Component : public BaseComponent { +private: + BaseComponent::Group group(); +}; + +class ComponentPool { +private: + std::vector components_; +}; + +} // namespace yage diff --git a/yage/entity/entitymanager.cpp b/yage/entity/entitymanager.cpp deleted file mode 100644 index c37bc620..00000000 --- a/yage/entity/entitymanager.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: entitymanager.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include "entitymanager.h" - -#include - -namespace yage -{ - -EntityManager::EntityManager(Space *space) : space_(space) {} - -EntityManager::EntityManager(Space *space, std::size_t n) : space_(space) -{ - entities_.reserve(n); -} - -Entity EntityManager::createEntity() -{ - Entity entity = next_entity_++; - entities_.push_back(entity); - return entity; -} - -void EntityManager::deleteEntity(Entity entity) -{ - auto index = std::find_if(entities_.begin(), entities_.end(), - [&](Entity &value) { return value == entity; }); - if (index != entities_.end()) { - entities_.erase(index); - } -} - -} // namespace yage diff --git a/yage/entity/entitymanager.h b/yage/entity/entitymanager.h deleted file mode 100644 index 97f7c6f9..00000000 --- a/yage/entity/entitymanager.h +++ /dev/null @@ -1,77 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: entitymanager.h - * - * Copyright (c) 2017 Yann Herklotz Grave - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#pragma once - -#include - -namespace yage -{ - -class Space; - -typedef unsigned int Entity; - -/** - * Manages entities in a space. - */ -class EntityManager -{ -public: - /** - * Default instance of an EntityManager. - */ - EntityManager() = default; - - /** - * Creates an instance of the entity manager, which refers back to the space - * it was created in and belongs to. - * - * @param space Current space that the EntityManager belongs to. - */ - EntityManager(Space *space); - - /** - * Creates an instance of the entitiy manager with an initial size. - * - * @param space Current space that the EntityManager belongs to. - * @param n Initial size of the EntityManager. - */ - EntityManager(Space *space, std::size_t n); - - /** - * Creates an Entity and returns the handle to the entity, which can then be - * used by the user to do operations on it. - * - * @return The handle to the entity that was created in the space. - */ - Entity createEntity(); - - /** - * Delete an entity. - */ - void deleteEntity(Entity entity); - -private: - /** - * The next available handle to give to the user. - */ - Entity next_entity_ = 0; - - /** - * The space that the entity manager belongs to. - */ - Space *space_ = nullptr; - - /** - * The entities in the current space. - */ - std::vector entities_; -}; - -} // namespace yage diff --git a/yage/entity/space.h b/yage/entity/space.h index dff2d2ce..ed867534 100644 --- a/yage/entity/space.h +++ b/yage/entity/space.h @@ -11,9 +11,13 @@ #include #include -#include "entitymanager.h" +#include "entity.h" #include "system.h" +struct Movement { + +}; + namespace yage { -- cgit