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 | 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 | 8 | ||||
-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 |
9 files changed, 125 insertions, 18 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..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/api/Metadata.cpp b/src/crepe/api/Metadata.cpp new file mode 100644 index 0000000..53d93da --- /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..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 |