diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/Component.cpp | 2 | ||||
-rw-r--r-- | src/crepe/Component.h | 25 | ||||
-rw-r--r-- | src/crepe/ComponentManager.cpp | 5 | ||||
-rw-r--r-- | src/crepe/ComponentManager.h | 95 | ||||
-rw-r--r-- | src/crepe/ComponentManager.hpp | 35 | ||||
-rw-r--r-- | src/crepe/Metadata.h | 23 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 2 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/api/GameObject.cpp | 26 | ||||
-rw-r--r-- | src/crepe/api/GameObject.h | 49 | ||||
-rw-r--r-- | src/crepe/api/GameObject.hpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Metadata.cpp (renamed from src/crepe/Metadata.cpp) | 2 | ||||
-rw-r--r-- | src/crepe/api/Metadata.h | 43 | ||||
-rw-r--r-- | src/crepe/api/Script.hpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Transform.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 26 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 4 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 2 |
19 files changed, 253 insertions, 100 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 867329f..8830e05 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -4,7 +4,6 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp - Metadata.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -13,7 +12,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ComponentManager.hpp Component.h Collider.h - Metadata.h ) add_subdirectory(api) diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 358ce31..cdbda67 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Component::Component(uint32_t id) : game_object_id(id), active(true) {} +Component::Component(uint32_t id) : GAME_OBJECT_ID(id) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 8db9b2a..41badc3 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -6,18 +6,39 @@ namespace crepe { class ComponentManager; +/** + * \brief Base class for all components + * + * This class is the base class for all components. It provides a common + * interface for all components. + */ class Component { protected: + //! Only the ComponentManager can create components friend class crepe::ComponentManager; + /** + * \param id The id of the GameObject this component belongs to + */ Component(uint32_t id); public: virtual ~Component() = default; + /** + * \brief Get the maximum number of instances for this component + * + * This method returns -1 by default, which means that there is no limit + * for the number of instances. Concrete components can override this method + * to set a limit. + * + * \return The maximum number of instances for this component + */ virtual int get_instances_max() const { return -1; } public: - uint32_t game_object_id; - bool active; + //! The id of the GameObject this component belongs to + const uint32_t GAME_OBJECT_ID; + //! Whether the component is active + bool active = true; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 8bde33a..01bc8d7 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,6 +1,7 @@ -#include "ComponentManager.h" #include "util/log.h" +#include "ComponentManager.h" + using namespace crepe; ComponentManager & ComponentManager::get_instance() { @@ -10,7 +11,7 @@ ComponentManager & ComponentManager::get_instance() { void ComponentManager::delete_all_components_of_id(uint32_t id) { // Loop through all the types (in the unordered_map<>) - for (auto & [type, componentArray] : components) { + for (auto & [type, componentArray] : this->components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id < componentArray.size()) { // Clear the components at this specific id diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 2b5e1df..f3b0ace 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -10,47 +10,110 @@ namespace crepe { +/** + * \brief Manages all components + * + * This class manages all components. It provides methods to add, delete and get + * components. + */ class ComponentManager { public: - // Singleton + /** + * \brief Get the instance of the ComponentManager + * + * \return The instance of the ComponentManager + */ static ComponentManager & get_instance(); ComponentManager(const ComponentManager &) = delete; ComponentManager(ComponentManager &&) = delete; ComponentManager & operator=(const ComponentManager &) = delete; ComponentManager & operator=(ComponentManager &&) = delete; + ~ComponentManager(); -public: - //! Add a component of a specific type + /** + * \brief Add a component to the ComponentManager + * + * This method adds a component to the ComponentManager. The component is + * created with the given arguments and added to the ComponentManager. + * + * \tparam T The type of the component + * \tparam Args The types of the arguments + * \param id The id of the GameObject this component belongs to + * \param args The arguments to create the component + * \return The created component + */ template <typename T, typename... Args> T & add_component(uint32_t id, Args &&... args); - //! Deletes all components of a specific type and id + /** + * \brief Delete all components of a specific type and id + * + * This method deletes all components of a specific type and id. + * + * \tparam T The type of the component + * \param id The id of the GameObject this component belongs to + */ template <typename T> void delete_components_by_id(uint32_t id); - //! Deletes all components of a specific type + /** + * \brief Delete all components of a specific type + * + * This method deletes all components of a specific type. + * + * \tparam T The type of the component + */ template <typename T> void delete_components(); - //! Deletes all components of a specific id + /** + * \brief Delete all components of a specific id + * + * This method deletes all components of a specific id. + * + * \param id The id of the GameObject this component belongs to + */ void delete_all_components_of_id(uint32_t id); - //! Deletes all components + /** + * \brief Delete all components + * + * This method deletes all components. + */ void delete_all_components(); - - //! Get a vector<> of all components at specific type and id + /** + * \brief Get all components of a specific type and id + * + * This method gets all components of a specific type and id. + * + * \tparam T The type of the component + * \param id The id of the GameObject this component belongs to + * \return A vector of all components of the specific type and id + */ template <typename T> std::vector<std::reference_wrapper<T>> get_components_by_id(uint32_t id) const; - //! Get a vector<> of all components of a specific type + /** + * \brief Get all components of a specific type + * + * This method gets all components of a specific type. + * + * \tparam T The type of the component + * \return A vector of all components of the specific type + */ template <typename T> std::vector<std::reference_wrapper<T>> get_components_by_type() const; private: ComponentManager(); - virtual ~ComponentManager(); - /* - * The std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> below might seem a bit strange, let me explain this structure: - * The std::unordered_map<> has a key and value. The key is a std::type_index and the value is a std::vector. So, a new std::vector will be created for each new std::type_index. - * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component. - * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id). +private: + /** + * \brief The components + * + * This unordered_map stores all components. The key is the type of the + * component and the value is a vector of vectors of unique pointers to the + * components. + * Every component type has its own vector of vectors of unique pointers to + * the components. The first vector is for the ids of the GameObjects and the + * second vector is for the components (because a GameObject might have multiple + * components). */ std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 89ed111..489e188 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -17,15 +17,15 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { type_index type = typeid(T); // Check if this component type is already in the unordered_map<> - if (components.find(type) == components.end()) { + if (this->components.find(type) == this->components.end()) { //If not, create a new (empty) vector<> of vector<unique_ptr<Component>> - components[type] = vector<vector<unique_ptr<Component>>>(); + this->components[type] = vector<vector<unique_ptr<Component>>>(); } // Resize the vector<> if the id is greater than the current size - if (id >= components[type].size()) { + if (id >= this->components[type].size()) { // Initialize new slots to nullptr (resize does automatically init to nullptr) - components[type].resize(id + 1); + this->components[type].resize(id + 1); } // Create a new component of type T (arguments directly forwarded). The @@ -46,7 +46,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { } // store its unique_ptr in the vector<> - components[type][id].push_back(std::move(instance)); + this->components[type][id].push_back(std::move(instance)); return instance_ref; } @@ -59,10 +59,10 @@ void ComponentManager::delete_components_by_id(uint32_t id) { type_index type = typeid(T); // Find the type (in the unordered_map<>) - if (components.find(type) != components.end()) { + if (this->components.find(type) != this->components.end()) { // Get the correct vector<> vector<vector<unique_ptr<Component>>> & component_array - = components[type]; + = this->components[type]; // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id < component_array.size()) { @@ -77,9 +77,9 @@ void ComponentManager::delete_components() { // Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); - if (components.find(type) == components.end()) return; + if (this->components.find(type) == this->components.end()) return; - components[type].clear(); + this->components[type].clear(); } template <typename T> @@ -93,11 +93,12 @@ ComponentManager::get_components_by_id(uint32_t id) const { // Create an empty vector<> vector<reference_wrapper<T>> component_vector; - if (components.find(type) == components.end()) return component_vector; + if (this->components.find(type) == this->components.end()) + return component_vector; // Get the correct vector<> const vector<vector<unique_ptr<Component>>> & component_array - = components.at(type); + = this->components.at(type); // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id >= component_array.size()) return component_vector; @@ -126,15 +127,14 @@ ComponentManager::get_components_by_type() const { // Create an empty vector<> vector<reference_wrapper<T>> component_vector; - // Set the id to 0 (the id will also be stored in the returned vector<>) - // uint32_t id = 0; // Find the type (in the unordered_map<>) - if (components.find(type) == components.end()) return component_vector; + if (this->components.find(type) == this->components.end()) + return component_vector; // Get the correct vector<> const vector<vector<unique_ptr<Component>>> & component_array - = components.at(type); + = this->components.at(type); // Loop through the whole vector<> for (const vector<unique_ptr<Component>> & component : component_array) { @@ -146,12 +146,9 @@ ComponentManager::get_components_by_type() const { // Ensure that the cast was successful if (casted_component == nullptr) continue; - // Pair the dereferenced raw pointer and the id and add it to the vector<> + // Add the dereferenced raw pointer to the vector<> component_vector.emplace_back(ref(*casted_component)); } - - // Increase the id (the id will also be stored in the returned vector<>) - //++id; } // Return the vector<> diff --git a/src/crepe/Metadata.h b/src/crepe/Metadata.h deleted file mode 100644 index 1577987..0000000 --- a/src/crepe/Metadata.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include <string> -#include <vector> - -#include "Component.h" - -namespace crepe { - -class Metadata : public Component { -public: - Metadata(uint32_t game_object_id, const std::string & name, - const std::string & tag); - virtual int get_instances_max() const { return 1; } - -public: - std::string name; - std::string tag; - uint32_t parent = -1; - std::vector<uint32_t> children; -}; - -} // namespace crepe diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 42add50..1e24ae8 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -10,7 +10,7 @@ namespace crepe { class Sound; //! Audio source component -class AudioSource : Component { +class AudioSource : public Component { public: AudioSource(std::unique_ptr<Asset> audio_clip); virtual ~AudioSource() = default; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 7fd8ffd..dbd6bf1 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -11,6 +11,7 @@ target_sources(crepe PUBLIC Texture.cpp AssetManager.cpp Sprite.cpp + Metadata.cpp Scene.cpp SceneManager.cpp ) @@ -31,6 +32,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES AssetManager.h AssetManager.hpp Scene.h + Metadata.h SceneManager.h SceneManager.hpp ) diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 51cd08f..b1b8f85 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,24 +6,26 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, - const Point & position, double rotation, double scale) - : id(id) { +GameObject::GameObject(uint32_t id, const std::string & name, + const std::string & tag, const Point & position, + double rotation, double scale) + : ID(id) { + // Add Transform and Metadata components ComponentManager & mgr = ComponentManager::get_instance(); - mgr.add_component<Transform>(this->id, position, rotation, scale); - mgr.add_component<Metadata>(this->id, name, tag); + mgr.add_component<Transform>(this->ID, position, rotation, scale); + mgr.add_component<Metadata>(this->ID, name, tag); } void GameObject::set_parent(const GameObject & parent) { - auto & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = ComponentManager::get_instance(); - // set parent on own Metadata component + // Set parent on own Metadata component vector<reference_wrapper<Metadata>> this_metadata - = mgr.get_components_by_id<Metadata>(this->id); - this_metadata.at(0).get().parent = parent.id; + = mgr.get_components_by_id<Metadata>(this->ID); + this_metadata.at(0).get().parent = parent.ID; - // add own id to children list of parent's Metadata component + // Add own id to children list of parent's Metadata component vector<reference_wrapper<Metadata>> parent_metadata - = mgr.get_components_by_id<Metadata>(parent.id); - parent_metadata.at(0).get().children.push_back(this->id); + = mgr.get_components_by_id<Metadata>(parent.ID); + parent_metadata.at(0).get().children.push_back(this->ID); } diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 602f33c..2992787 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -3,20 +3,59 @@ #include <cstdint> #include <string> -#include "api/Point.h" - namespace crepe { +class Point; + +/** + * \brief Represents a GameObject + * + * This class represents a GameObject. The GameObject class is only used + * as an interface for the game programmer. The actual implementation is + * done in the ComponentManager. + */ class GameObject { public: - GameObject(uint32_t id, std::string name, std::string tag, + /** + * This constructor creates a new GameObject. It creates a new + * Transform and Metadata component and adds them to the ComponentManager. + * + * \param id The id of the GameObject + * \param name The name of the GameObject + * \param tag The tag of the GameObject + * \param position The position of the GameObject + * \param rotation The rotation of the GameObject + * \param scale The scale of the GameObject + */ + GameObject(uint32_t id, const std::string & name, const std::string & tag, const Point & position, double rotation, double scale); + /** + * \brief Set the parent of this GameObject + * + * This method sets the parent of this GameObject. It sets the parent + * in the Metadata component of this GameObject and adds this GameObject + * to the children list of the parent GameObject. + * + * \param parent The parent GameObject + */ void set_parent(const GameObject & parent); - + /** + * \brief Add a component to the GameObject + * + * This method adds a component to the GameObject. It forwards the + * arguments to the ComponentManager. + * + * \tparam T The type of the component + * \tparam Args The types of the arguments + * \param args The arguments to create the component + * \return The created component + */ template <typename T, typename... Args> T & add_component(Args &&... args); - uint32_t id; +public: + //! The id of the GameObject + const uint32_t ID; }; } // namespace crepe diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp index 77cf40e..7e6148c 100644 --- a/src/crepe/api/GameObject.hpp +++ b/src/crepe/api/GameObject.hpp @@ -8,8 +8,8 @@ namespace crepe { template <typename T, typename... Args> T & GameObject::add_component(Args &&... args) { - auto & mgr = ComponentManager::get_instance(); - return mgr.add_component<T>(this->id, std::forward<Args>(args)...); + ComponentManager & mgr = ComponentManager::get_instance(); + return mgr.add_component<T>(this->ID, std::forward<Args>(args)...); } } // namespace crepe diff --git a/src/crepe/Metadata.cpp b/src/crepe/api/Metadata.cpp index 53d93da..55d9ae2 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/api/Metadata.cpp @@ -5,4 +5,4 @@ using namespace std; Metadata::Metadata(uint32_t game_object_id, const string & name, const string & tag) - : Component(game_object_id), name(name), tag(tag) {} + : Component(game_object_id), NAME(name), TAG(tag) {} diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h new file mode 100644 index 0000000..fdbed41 --- /dev/null +++ b/src/crepe/api/Metadata.h @@ -0,0 +1,43 @@ +#pragma once + +#include <string> +#include <vector> + +#include "../Component.h" + +namespace crepe { + +/** + * \brief Metadata component + * + * This class represents the Metadata component. It stores the name, tag, parent + * and children of a GameObject. + */ +class Metadata : public Component { +public: + /** + * \param game_object_id The id of the GameObject this component belongs to + * \param name The name of the GameObject + * \param tag The tag of the GameObject + */ + Metadata(uint32_t game_object_id, const std::string & name, + const std::string & tag); + /** + * \brief Get the maximum number of instances for this component + * + * \return The maximum number of instances for this component + */ + virtual int get_instances_max() const { return 1; } + +public: + //! The name of the GameObject + const std::string NAME; + //! The tag of the GameObject + const std::string TAG; + //! The id of the parent GameObject (-1 if no parent) + uint32_t parent = -1; + //! The ids of the children GameObjects + std::vector<uint32_t> children; +}; + +} // namespace crepe diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index d96c0e8..6d111af 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -19,7 +19,7 @@ T & Script::get_component() { template <typename T> std::vector<std::reference_wrapper<T>> Script::get_components() { ComponentManager & mgr = ComponentManager::get_instance(); - return mgr.get_components_by_id<T>(this->parent->game_object_id); + return mgr.get_components_by_id<T>(this->parent->GAME_OBJECT_ID); } } // namespace crepe diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 1d8d401..be1769e 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,9 +1,7 @@ #include <cstdint> -#include "api/Point.h" #include "util/log.h" -#include "Component.h" #include "Transform.h" using namespace crepe; @@ -13,5 +11,3 @@ Transform::Transform(uint32_t game_id, const Point & point, double rot, : Component(game_id), position(point), rotation(rot), scale(scale) { dbg_trace(); } - -Transform::~Transform() { dbg_trace(); } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 557061b..69ea48f 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -8,15 +8,29 @@ namespace crepe { +/** + * \brief Transform component + * + * This class represents the Transform component. It stores the position, + * rotation and scale of a GameObject. + */ class Transform : public Component { - // FIXME: What's the difference between the `Point` and `Position` - // classes/structs? How about we replace both with a universal `Vec2` that - // works similar (or the same) as those found in GLSL? - public: - Transform(uint32_t id, const Point &, double, double); - ~Transform(); + /** + * \param id The id of the GameObject this component belongs to + * \param point The position of the GameObject + * \param rot The rotation of the GameObject + * \param scale The scale of the GameObject + */ + Transform(uint32_t id, const Point & point, double rot, double scale); + /** + * \brief Get the maximum number of instances for this component + * + * \return The maximum number of instances for this component + */ virtual int get_instances_max() const { return 1; } + +public: //! Translation (shift) Point position; //! Rotation, in radians diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index cea8062..dd80312 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -23,12 +23,12 @@ void PhysicsSystem::update() { switch (rigidbody.body_type) { case BodyType::DYNAMIC: for (Transform & transform : transforms) { - if (transform.game_object_id == rigidbody.game_object_id) { + if (transform.GAME_OBJECT_ID == rigidbody.GAME_OBJECT_ID) { rigidbody.velocity_x = 0; rigidbody.velocity_y = 0; std::vector<std::reference_wrapper<Force>> forces = mgr.get_components_by_id<Force>( - rigidbody.game_object_id); + rigidbody.GAME_OBJECT_ID); rigidbody.velocity_y += rigidbody.gravity_scale * 1 * rigidbody.mass; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 5a07cc2..2003eaf 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -32,7 +32,7 @@ void RenderSystem::update() { for (const Sprite & sprite : sprites) { std::vector<std::reference_wrapper<Transform>> transforms - = mgr.get_components_by_id<Transform>(sprite.game_object_id); + = mgr.get_components_by_id<Transform>(sprite.GAME_OBJECT_ID); for (const Transform & transform : transforms) { render.draw(sprite, transform); } |