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 | 23 | ||||
-rw-r--r-- | src/crepe/ComponentManager.cpp | 3 | ||||
-rw-r--r-- | src/crepe/ComponentManager.h | 95 | ||||
-rw-r--r-- | src/crepe/ComponentManager.hpp | 7 | ||||
-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 | 12 | ||||
-rw-r--r-- | src/crepe/api/GameObject.h | 47 | ||||
-rw-r--r-- | src/crepe/api/GameObject.hpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Metadata.cpp (renamed from src/crepe/Metadata.cpp) | 0 | ||||
-rw-r--r-- | src/crepe/api/Metadata.h | 43 | ||||
-rw-r--r-- | src/crepe/api/Transform.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 25 |
16 files changed, 222 insertions, 68 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..230bb70 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..02a4e7e 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: + //! The id of the GameObject this component belongs to uint32_t game_object_id; - bool active; + //! Whether the component is active + bool active = true; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 8bde33a..55bd82f 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() { 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 e74f2e9..11c4d15 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -122,8 +122,6 @@ 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; @@ -142,12 +140,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 0bb1263..3e0c044 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 ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -28,4 +29,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Texture.h AssetManager.h AssetManager.hpp + Metadata.h ) diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 51cd08f..b24b980 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,23 +6,25 @@ using namespace crepe; using namespace std; -GameObject::GameObject(uint32_t id, std::string name, std::string tag, - const Point & position, double rotation, double scale) +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); } 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; - // 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); diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 602f33c..2a82258 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -3,19 +3,58 @@ #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); +public: + //! The id of the GameObject uint32_t id; }; diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp index 77cf40e..bfba7fe 100644 --- a/src/crepe/api/GameObject.hpp +++ b/src/crepe/api/GameObject.hpp @@ -8,7 +8,7 @@ namespace crepe { template <typename T, typename... Args> T & GameObject::add_component(Args &&... args) { - auto & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = ComponentManager::get_instance(); return mgr.add_component<T>(this->id, std::forward<Args>(args)...); } diff --git a/src/crepe/Metadata.cpp b/src/crepe/api/Metadata.cpp index 53d93da..53d93da 100644 --- a/src/crepe/Metadata.cpp +++ b/src/crepe/api/Metadata.cpp diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h new file mode 100644 index 0000000..4d37108 --- /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 + std::string name; + //! The tag of the GameObject + 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/Transform.cpp b/src/crepe/api/Transform.cpp index 1d8d401..5274b01 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; diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 557061b..02125ef 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -8,15 +8,30 @@ 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); + /** + * \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); ~Transform(); + /** + * \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 |