diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-12 22:43:32 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-12 22:43:32 +0100 |
commit | 3e94ecb3dac5003a3d58210ed1a4d1f1cb2083d1 (patch) | |
tree | 67c3e1c122652ae09e58e7de49db668e252c4730 /src/crepe/api | |
parent | f2509e89c02894ebd3ad992324eb300103621d26 (diff) |
add script unit tests + major refactoring
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Animator.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Animator.h | 3 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 20 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.hpp | 4 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 10 | ||||
-rw-r--r-- | src/crepe/api/Camera.cpp | 7 | ||||
-rw-r--r-- | src/crepe/api/Camera.h | 2 | ||||
-rw-r--r-- | src/crepe/api/GameObject.cpp | 9 | ||||
-rw-r--r-- | src/crepe/api/GameObject.h | 17 | ||||
-rw-r--r-- | src/crepe/api/GameObject.hpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Metadata.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Metadata.h | 2 | ||||
-rw-r--r-- | src/crepe/api/ParticleEmitter.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/ParticleEmitter.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Rigidbody.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Rigidbody.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Script.cpp | 1 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 15 | ||||
-rw-r--r-- | src/crepe/api/Script.hpp | 6 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Transform.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 2 |
24 files changed, 86 insertions, 48 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 58fee2a..ad60981 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -9,8 +9,8 @@ using namespace crepe; -Animator::Animator(uint32_t id, Sprite & ss, int row, int col, int col_animator) - : Component(id), +Animator::Animator(const Component::Data & data, Sprite & ss, int row, int col, int col_animator) + : Component(data), spritesheet(ss), row(row), col(col) { diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index def0240..f66dc1a 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -6,6 +6,7 @@ #include "Sprite.h" namespace crepe { + class AnimatorSystem; class SDLContext; @@ -35,7 +36,7 @@ public: * * This constructor sets up the Animator with the given parameters, and initializes the animation system. */ - Animator(uint32_t id, Sprite & spritesheet, int row, int col, + Animator(const Component::Data & data, Sprite & spritesheet, int row, int col, int col_animate); ~Animator(); // dbg_trace diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index e69de29..ce1cfde 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -0,0 +1,4 @@ +#include "BehaviorScript.h" + +using namespace crepe; + diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 6b1fec7..8d21a72 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -12,19 +12,35 @@ class Script; class BehaviorScript : public Component { protected: - friend class crepe::ComponentManager; using Component::Component; + //! Only ComponentManager is allowed to instantiate BehaviorScript + friend class ComponentManager; public: virtual ~BehaviorScript() = default; public: + /** + * \brief Set the concrete script of this component + * + * \tparam T Concrete script type (derived from \c crepe::Script) + * + * \returns Reference to BehaviorScript component (`*this`) + */ template <class T> BehaviorScript & set_script(); protected: - friend class crepe::ScriptSystem; + //! ScriptSystem needs direct access to the script instance + friend class ScriptSystem; + //! Flag to indicate if script->init() has been called already + bool initialized = false; std::unique_ptr<Script> script = nullptr; + +private: + //! Script accesses the component manager directly via its parent + // (BehaviorScript) reference + friend class Script; }; } // namespace crepe diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index 4751607..bb5d829 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -11,10 +11,10 @@ namespace crepe { template <class T> BehaviorScript & BehaviorScript::set_script() { - static_assert(std::is_base_of<Script, T>::value); dbg_trace(); + static_assert(std::is_base_of<Script, T>::value); Script * s = new T(); - s->parent = this; + s->parent_ref = this; this->script = std::unique_ptr<Script>(s); return *this; } diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 85696c4..d9cda57 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -14,11 +14,11 @@ target_sources(crepe PUBLIC Config.cpp Metadata.cpp Scene.cpp - SceneManager.cpp + # SceneManager.cpp Vector2.cpp Camera.cpp Animator.cpp - LoopManager.cpp + # LoopManager.cpp LoopTimer.cpp ) @@ -40,10 +40,10 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SaveManager.h Scene.h Metadata.h - SceneManager.h - SceneManager.hpp + # SceneManager.h + # SceneManager.hpp Camera.h Animator.h - LoopManager.h + # LoopManager.h LoopTimer.h ) diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 6355a03..fece199 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,6 +1,3 @@ - -#include <cstdint> - #include "util/log.h" #include "Camera.h" @@ -9,8 +6,8 @@ using namespace crepe; -Camera::Camera(uint32_t id, const Color & bg_color) - : Component(id), +Camera::Camera(const Component::Data & data, const Color & bg_color) + : Component(data), bg_color(bg_color) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index ba3a9ef..a52f2f7 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -23,7 +23,7 @@ public: * \param id Unique identifier for the camera component. * \param bg_color Background color for the camera view. */ - Camera(uint32_t id, const Color & bg_color); + Camera(const Component::Data & data, const Color & bg_color); ~Camera(); // dbg_trace only public: diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index d252e77..bba1f26 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -6,18 +6,19 @@ using namespace crepe; using namespace std; -GameObject::GameObject(game_object_id_t id, const std::string & name, +GameObject::GameObject(ComponentManager & component_manager, game_object_id_t id, const std::string & name, const std::string & tag, const Vector2 & position, double rotation, double scale) - : id(id) { + : id(id), component_manager(component_manager) { + // Add Transform and Metadata components - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; mgr.add_component<Transform>(this->id, position, rotation, scale); mgr.add_component<Metadata>(this->id, name, tag); } void GameObject::set_parent(const GameObject & parent) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; // Set parent on own Metadata component vector<reference_wrapper<Metadata>> this_metadata diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index d703730..d09938a 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -3,10 +3,11 @@ #include <string> #include "types.h" +#include "Vector2.h" namespace crepe { -class Vector2; +class ComponentManager; /** * \brief Represents a GameObject @@ -16,11 +17,12 @@ class Vector2; * done in the ComponentManager. */ class GameObject { -public: +private: /** * This constructor creates a new GameObject. It creates a new * Transform and Metadata component and adds them to the ComponentManager. * + * \param component_manager Reference to component_manager * \param id The id of the GameObject * \param name The name of the GameObject * \param tag The tag of the GameObject @@ -28,9 +30,11 @@ public: * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - GameObject(game_object_id_t id, const std::string & name, - const std::string & tag, const Vector2 & position, - double rotation, double scale); + GameObject(ComponentManager & component_manager, game_object_id_t id, const std::string & name, const std::string & tag, const Vector2 & position, double rotation, double scale); + //! ComponentManager instances GameObject + friend class ComponentManager; + +public: /** * \brief Set the parent of this GameObject * @@ -58,6 +62,9 @@ public: public: //! The id of the GameObject const game_object_id_t id; + +protected: + ComponentManager & component_manager; }; } // namespace crepe diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp index bfba7fe..17b17d7 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) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; 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 index d421de5..6f0a280 100644 --- a/src/crepe/api/Metadata.cpp +++ b/src/crepe/api/Metadata.cpp @@ -3,7 +3,7 @@ using namespace crepe; using namespace std; -Metadata::Metadata(game_object_id_t id, const string & name, const string & tag) - : Component(id), +Metadata::Metadata(const Component::Data & data, const string & name, const string & tag) + : Component(data), name(name), tag(tag) {} diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h index c61e006..8c91797 100644 --- a/src/crepe/api/Metadata.h +++ b/src/crepe/api/Metadata.h @@ -20,7 +20,7 @@ public: * \param name The name of the GameObject * \param tag The tag of the GameObject */ - Metadata(game_object_id_t id, const std::string & name, + Metadata(const Component::Data & data, const std::string & name, const std::string & tag); /** * \brief Get the maximum number of instances for this component diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 0bc2197..ed4fec8 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -6,12 +6,12 @@ using namespace crepe; -ParticleEmitter::ParticleEmitter(game_object_id_t id, uint32_t max_particles, +ParticleEmitter::ParticleEmitter(const Component::Data & data, uint32_t max_particles, uint32_t emission_rate, uint32_t speed, uint32_t speed_offset, uint32_t angle, uint32_t angleOffset, float begin_lifespan, float end_lifespan) - : Component(id), + : Component(data), max_particles(max_particles), emission_rate(emission_rate), speed(speed), diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 5939723..ba89538 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -10,7 +10,7 @@ namespace crepe { class ParticleEmitter : public Component { public: - ParticleEmitter(game_object_id_t id, uint32_t max_particles, + ParticleEmitter(const Component::Data & data, uint32_t max_particles, uint32_t emission_rate, uint32_t speed, uint32_t speed_offset, uint32_t angle, uint32_t angleOffset, float begin_lifespan, float end_lifespan); diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index 3bf1c5b..be6bb93 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,8 +2,8 @@ using namespace crepe; -crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const Data & data) - : Component(game_object_id), +crepe::Rigidbody::Rigidbody(const Component::Data & component_data, const Data & data) + : Component(component_data), data(data) {} void crepe::Rigidbody::add_force_linear(const Vector2 & force) { diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 68481f4..99837a6 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -82,7 +82,7 @@ public: * \param game_object_id id of the gameobject the rigibody is added to. * \param data struct to configure the rigidbody. */ - Rigidbody(uint32_t game_object_id, const Data & data); + Rigidbody(const Component::Data & component_data, const Data & data); //! struct to hold data of rigidbody Data data; diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 390cec7..0423315 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -1,3 +1,4 @@ #include "Script.h" using namespace crepe; + diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 49e625f..d543df8 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -11,6 +11,7 @@ namespace crepe { class BehaviorScript; class Script { + //! ScriptSystem calls \c update() friend class crepe::ScriptSystem; protected: @@ -22,17 +23,25 @@ protected: // added event. protected: + //! Retrieve component from component manager (utility) template <typename T> T & get_component(); + //! Retrieve components from component manager (utility) template <typename T> std::vector<std::reference_wrapper<T>> get_components(); -private: - friend class crepe::BehaviorScript; - BehaviorScript * parent = nullptr; +protected: + // NOTE: Script must have a constructor without arguments so the game + // programmer doesn't need to manually add `using Script::Script` to their + // concrete script class. + Script() = default; + //! Only \c BehaviorScript instantiates Script + friend class BehaviorScript; + BehaviorScript * parent_ref = nullptr; }; } // namespace crepe #include "Script.hpp" + diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index d96c0e8..68bcc29 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -18,8 +18,10 @@ 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); + auto & parent = *this->parent_ref; + auto & mgr = parent.data.component_manager; + + return mgr.get_components_by_id<T>(parent.data.id); } } // namespace crepe diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 6f0433f..716609f 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -10,9 +10,9 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, +Sprite::Sprite(const Component::Data & data, const shared_ptr<Texture> image, const Color & color, const FlipSettings & flip) - : Component(id), + : Component(data), color(color), flip(flip), sprite_image(image) { diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index deb3f93..171dfeb 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -43,7 +43,7 @@ public: * \param color Color tint applied to the sprite. * \param flip Flip settings for horizontal and vertical orientation. */ - Sprite(game_object_id_t id, const std::shared_ptr<Texture> image, + Sprite(const Component::Data & data, const std::shared_ptr<Texture> image, const Color & color, const FlipSettings & flip); /** diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index e401120..9645d03 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -4,9 +4,9 @@ using namespace crepe; -Transform::Transform(game_object_id_t id, const Vector2 & point, +Transform::Transform(const Component::Data & data, const Vector2 & point, double rotation, double scale) - : Component(id), + : Component(data), position(point), rotation(rotation), scale(scale) { diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 756e45b..a7bcd89 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -20,7 +20,7 @@ public: * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - Transform(game_object_id_t id, const Vector2 & point, double rotation, + Transform(const Component::Data & data, const Vector2 & point, double rotation, double scale); /** * \brief Get the maximum number of instances for this component |