From 3e94ecb3dac5003a3d58210ed1a4d1f1cb2083d1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 12 Nov 2024 22:43:32 +0100 Subject: add script unit tests + major refactoring --- src/crepe/api/Script.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/crepe/api/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 std::vector> Script::get_components() { - ComponentManager & mgr = ComponentManager::get_instance(); - return mgr.get_components_by_id(this->parent->game_object_id); + auto & parent = *this->parent_ref; + auto & mgr = parent.data.component_manager; + + return mgr.get_components_by_id(parent.data.id); } } // namespace crepe -- cgit v1.2.3 From 455bb50a5007daf46b8719fff2a6292da6a294bf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 11:39:45 +0100 Subject: fix physics test --- src/crepe/Component.cpp | 2 +- src/crepe/Component.h | 22 +++++---------- src/crepe/ComponentManager.h | 7 ++++- src/crepe/ComponentManager.hpp | 5 +--- src/crepe/api/Animator.cpp | 4 +-- src/crepe/api/Animator.h | 2 +- src/crepe/api/BehaviorScript.cpp | 3 ++ src/crepe/api/BehaviorScript.h | 3 +- src/crepe/api/BehaviorScript.hpp | 1 + src/crepe/api/CMakeLists.txt | 6 ++-- src/crepe/api/Camera.cpp | 4 +-- src/crepe/api/Camera.h | 2 +- src/crepe/api/GameObject.cpp | 8 ++++++ src/crepe/api/GameObject.h | 13 +++++++++ src/crepe/api/Metadata.cpp | 4 +-- src/crepe/api/Metadata.h | 2 +- src/crepe/api/ParticleEmitter.cpp | 4 +-- src/crepe/api/ParticleEmitter.h | 2 +- src/crepe/api/Rigidbody.cpp | 4 +-- src/crepe/api/Rigidbody.h | 2 +- src/crepe/api/SceneManager.cpp | 17 +++++------- src/crepe/api/SceneManager.h | 16 +++++------ src/crepe/api/Script.h | 7 ++++- src/crepe/api/Script.hpp | 4 +-- src/crepe/api/Sprite.cpp | 4 +-- src/crepe/api/Sprite.h | 2 +- src/crepe/api/Transform.cpp | 4 +-- src/crepe/api/Transform.h | 28 +++++++++---------- src/crepe/api/Vector2.cpp | 15 +--------- src/crepe/api/Vector2.h | 15 +++------- src/crepe/system/CMakeLists.txt | 6 ++-- src/crepe/system/ParticleSystem.cpp | 5 ++-- src/crepe/system/ParticleSystem.h | 15 ++++++---- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/PhysicsSystem.h | 12 ++++---- src/crepe/system/ScriptSystem.h | 3 +- src/example/script.cpp | 10 +++---- src/test/CMakeLists.txt | 2 +- src/test/PhysicsTest.cpp | 55 ++++++++++++++++++++----------------- 39 files changed, 164 insertions(+), 158 deletions(-) (limited to 'src/crepe/api/Script.hpp') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index c133739..d11a37e 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -2,5 +2,5 @@ using namespace crepe; -Component::Component(const Data & data) : data(data) {} +Component::Component(game_object_id_t id) : game_object_id(id) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index c6d72df..12c10cb 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -14,23 +14,20 @@ class ComponentManager; */ class Component { public: - struct Data { - //! The ID of the GameObject this component belongs to - const game_object_id_t id; - //! The manager of this component - ComponentManager & component_manager; - }; + //! Whether the component is active + bool active = true; + //! The id of the GameObject this component belongs to + const game_object_id_t game_object_id; protected: /** - * \param base Data + * \param id The id of the GameObject this component belongs to */ - Component(const Data & base); + Component(game_object_id_t id); //! Only the ComponentManager can create components - friend class crepe::ComponentManager; + friend class ComponentManager; public: - virtual ~Component() = default; /** * \brief Get the maximum number of instances for this component * @@ -41,11 +38,6 @@ public: * \return The maximum number of instances for this component */ virtual int get_instances_max() const { return -1; } - -public: - Data data; - //! Whether the component is active - bool active = true; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index e37bc4a..1d67e69 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -24,7 +24,7 @@ public: ComponentManager(); // dbg_trace ~ComponentManager(); // dbg_trace -public: +protected: /** * \brief Add a component to the ComponentManager * @@ -39,6 +39,11 @@ public: */ template T & add_component(game_object_id_t id, Args &&... args); + //! GameObject is used as an interface to add components instead of the + // component manager directly + friend class GameObject; + +public: /** * \brief Delete all components of a specific type and id * diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 89a8536..98efb49 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -31,10 +31,7 @@ T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance_ptr = new T(Component::Data { - .id = id, - .component_manager = *this, - }, forward(args)...); + T * instance_ptr = new T(id, forward(args)...); if (instance_ptr == nullptr) throw std::bad_alloc(); T & instance_ref = *instance_ptr; diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index ad60981..cbf415d 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -9,8 +9,8 @@ using namespace crepe; -Animator::Animator(const Component::Data & data, Sprite & ss, int row, int col, int col_animator) - : Component(data), +Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_animator) + : Component(id), spritesheet(ss), row(row), col(col) { diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index f66dc1a..75b8139 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -36,7 +36,7 @@ public: * * This constructor sets up the Animator with the given parameters, and initializes the animation system. */ - Animator(const Component::Data & data, Sprite & spritesheet, int row, int col, + Animator(game_object_id_t id, 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 ce1cfde..41c144c 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -1,4 +1,7 @@ #include "BehaviorScript.h" +#include "Component.h" using namespace crepe; +BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) : Component(id), component_manager(mgr) {} + diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 8d21a72..f156081 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -12,7 +12,7 @@ class Script; class BehaviorScript : public Component { protected: - using Component::Component; + BehaviorScript(game_object_id_t id, ComponentManager & component_manager); //! Only ComponentManager is allowed to instantiate BehaviorScript friend class ComponentManager; @@ -36,6 +36,7 @@ protected: //! Flag to indicate if script->init() has been called already bool initialized = false; std::unique_ptr