diff options
-rw-r--r-- | src/crepe/Component.h | 21 | ||||
-rw-r--r-- | src/crepe/ComponentManager.h | 93 | ||||
-rw-r--r-- | src/crepe/Metadata.h | 20 | ||||
-rw-r--r-- | src/crepe/api/GameObject.h | 41 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 25 |
5 files changed, 179 insertions, 21 deletions
diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 40e6f49..02a4e7e 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -6,17 +6,38 @@ 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; + //! Whether the component is active bool active = true; }; diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 2b5e1df..850aadc 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -10,35 +10,92 @@ 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; -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; @@ -46,11 +103,17 @@ 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/Metadata.h b/src/crepe/Metadata.h index 1577987..d52ab67 100644 --- a/src/crepe/Metadata.h +++ b/src/crepe/Metadata.h @@ -7,16 +7,36 @@ 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; }; diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 602f33c..246c4d4 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -7,15 +7,54 @@ namespace crepe { +/** + * \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: + /** + * 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, std::string name, 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/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 |