diff options
Diffstat (limited to 'src/crepe/api')
-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 | 8 | ||||
-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 |
10 files changed, 135 insertions, 31 deletions
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/api/Metadata.cpp b/src/crepe/api/Metadata.cpp new file mode 100644 index 0000000..55d9ae2 --- /dev/null +++ b/src/crepe/api/Metadata.cpp @@ -0,0 +1,8 @@ +#include "Metadata.h" + +using namespace crepe; +using namespace std; + +Metadata::Metadata(uint32_t game_object_id, const string & name, + const string & 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 |