diff options
Diffstat (limited to 'src/crepe/api')
26 files changed, 90 insertions, 86 deletions
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<Script> script = nullptr; + ComponentManager & component_manager; private: //! Script accesses the component manager directly via its parent diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index bb5d829..2bed1b7 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -15,6 +15,7 @@ BehaviorScript & BehaviorScript::set_script() { static_assert(std::is_base_of<Script, T>::value); Script * s = new T(); s->parent_ref = this; + s->component_manager_ref = &this->component_manager; this->script = std::unique_ptr<Script>(s); return *this; } diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index d9cda57..ee77947 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -14,7 +14,7 @@ target_sources(crepe PUBLIC Config.cpp Metadata.cpp Scene.cpp - # SceneManager.cpp + SceneManager.cpp Vector2.cpp Camera.cpp Animator.cpp @@ -40,8 +40,8 @@ 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 diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index fece199..232c922 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -6,8 +6,8 @@ using namespace crepe; -Camera::Camera(const Component::Data & data, const Color & bg_color) - : Component(data), +Camera::Camera(game_object_id_t id, const Color & bg_color) + : Component(id), bg_color(bg_color) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index a52f2f7..d8c08a6 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(const Component::Data & data, const Color & bg_color); + Camera(game_object_id_t id, const Color & bg_color); ~Camera(); // dbg_trace only public: diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index bba1f26..dd20b7f 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -2,6 +2,7 @@ #include "GameObject.h" #include "Metadata.h" +#include "BehaviorScript.h" using namespace crepe; using namespace std; @@ -30,3 +31,10 @@ void GameObject::set_parent(const GameObject & parent) { = mgr.get_components_by_id<Metadata>(parent.id); parent_metadata.at(0).get().children.push_back(this->id); } + +template <> +BehaviorScript & GameObject::add_component<BehaviorScript>() { + ComponentManager & mgr = this->component_manager; + return mgr.add_component<BehaviorScript>(this->id, mgr); +} + diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index d09938a..7751fd0 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -8,6 +8,7 @@ namespace crepe { class ComponentManager; +class BehaviorScript; /** * \brief Represents a GameObject @@ -67,6 +68,18 @@ protected: ComponentManager & component_manager; }; +/** + * \brief Add a BehaviorScript component to this game object + * + * The \c BehaviorScript class is the only exception to the ECS harmony, and + * requires a reference to the component manager passed to its constructor in + * order to function normally. This is because the \c BehaviorScript (and \c + * Script) classes are the only component-related classes that store + * implemented member functions as data. + */ +template <> +BehaviorScript & GameObject::add_component<BehaviorScript>(); + } // namespace crepe #include "GameObject.hpp" diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp index 6f0a280..d421de5 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(const Component::Data & data, const string & name, const string & tag) - : Component(data), +Metadata::Metadata(game_object_id_t id, const string & name, const string & tag) + : Component(id), name(name), tag(tag) {} diff --git a/src/crepe/api/Metadata.h b/src/crepe/api/Metadata.h index 8c91797..c61e006 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(const Component::Data & data, const std::string & name, + Metadata(game_object_id_t id, 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 ed4fec8..0bc2197 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -6,12 +6,12 @@ using namespace crepe; -ParticleEmitter::ParticleEmitter(const Component::Data & data, uint32_t max_particles, +ParticleEmitter::ParticleEmitter(game_object_id_t id, 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(data), + : Component(id), 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 ba89538..5939723 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(const Component::Data & data, uint32_t max_particles, + ParticleEmitter(game_object_id_t id, 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 be6bb93..6b87695 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,8 +2,8 @@ using namespace crepe; -crepe::Rigidbody::Rigidbody(const Component::Data & component_data, const Data & data) - : Component(component_data), +crepe::Rigidbody::Rigidbody(game_object_id_t id, const Data & data) + : Component(id), 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 99837a6..2e20288 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(const Component::Data & component_data, const Data & data); + Rigidbody(game_object_id_t id, const Data & data); //! struct to hold data of rigidbody Data data; diff --git a/src/crepe/api/SceneManager.cpp b/src/crepe/api/SceneManager.cpp index dfed6ee..814b7d7 100644 --- a/src/crepe/api/SceneManager.cpp +++ b/src/crepe/api/SceneManager.cpp @@ -8,10 +8,7 @@ using namespace crepe; using namespace std; -SceneManager & SceneManager::get_instance() { - static SceneManager instance; - return instance; -} +SceneManager::SceneManager(ComponentManager & mgr) : component_manager(mgr) {} void SceneManager::set_next_scene(const string & name) { next_scene = name; } @@ -19,18 +16,18 @@ void SceneManager::load_next_scene() { // next scene not set if (this->next_scene.empty()) return; - auto it - = find_if(this->scenes.begin(), this->scenes.end(), - [&next_scene = this->next_scene](unique_ptr<Scene> & scene) { - return scene->name == next_scene; - }); + auto it = find_if(this->scenes.begin(), this->scenes.end(), + [&next_scene = this->next_scene](unique_ptr<Scene> & scene) { + return scene->name == next_scene; + } + ); // next scene not found if (it == this->scenes.end()) return; unique_ptr<Scene> & scene = *it; // Delete all components of the current scene - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; mgr.delete_all_components(); // Load the new scene diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index 1e0e670..553194f 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -8,14 +8,12 @@ namespace crepe { +class ComponentManager; + class SceneManager { public: - // Singleton - static SceneManager & get_instance(); - SceneManager(const SceneManager &) = delete; - SceneManager(SceneManager &&) = delete; - SceneManager & operator=(const SceneManager &) = delete; - SceneManager & operator=(SceneManager &&) = delete; + SceneManager(ComponentManager & mgr); + virtual ~SceneManager() = default; public: /** @@ -38,11 +36,11 @@ public: void load_next_scene(); private: - SceneManager() = default; - -private: std::vector<std::unique_ptr<Scene>> scenes; std::string next_scene; + +protected: + ComponentManager & component_manager; }; } // namespace crepe diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index d543df8..c5e3cc4 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -9,6 +9,7 @@ class ScriptSystem; namespace crepe { class BehaviorScript; +class ComponentManager; class Script { //! ScriptSystem calls \c update() @@ -31,14 +32,18 @@ protected: template <typename T> std::vector<std::reference_wrapper<T>> get_components(); -protected: +private: // 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; + + // These references are set by BehaviorScript immediately after calling the + // constructor of Script. BehaviorScript * parent_ref = nullptr; + ComponentManager * component_manager_ref = nullptr; }; } // namespace crepe diff --git a/src/crepe/api/Script.hpp b/src/crepe/api/Script.hpp index 68bcc29..ee8c380 100644 --- a/src/crepe/api/Script.hpp +++ b/src/crepe/api/Script.hpp @@ -19,9 +19,9 @@ T & Script::get_component() { template <typename T> std::vector<std::reference_wrapper<T>> Script::get_components() { auto & parent = *this->parent_ref; - auto & mgr = parent.data.component_manager; + auto & mgr = *this->component_manager_ref; - return mgr.get_components_by_id<T>(parent.data.id); + return mgr.get_components_by_id<T>(parent.game_object_id); } } // namespace crepe diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 716609f..6f0433f 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(const Component::Data & data, const shared_ptr<Texture> image, +Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, const Color & color, const FlipSettings & flip) - : Component(data), + : Component(id), color(color), flip(flip), sprite_image(image) { diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 171dfeb..deb3f93 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(const Component::Data & data, const std::shared_ptr<Texture> image, + Sprite(game_object_id_t id, 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 9645d03..e401120 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -4,9 +4,9 @@ using namespace crepe; -Transform::Transform(const Component::Data & data, const Vector2 & point, +Transform::Transform(game_object_id_t id, const Vector2 & point, double rotation, double scale) - : Component(data), + : Component(id), position(point), rotation(rotation), scale(scale) { diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index a7bcd89..a1ef406 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -14,28 +14,26 @@ namespace crepe { */ class Transform : public Component { public: + //! Translation (shift) + Vector2 position = { 0, 0 }; + //! Rotation, in degrees + double rotation = 0; + //! Multiplication factor + double scale = 0; + +protected: /** * \param id The id of the GameObject this component belongs to * \param point The position of the GameObject * \param rotation The rotation of the GameObject * \param scale The scale of the GameObject */ - Transform(const Component::Data & data, const Vector2 & point, double rotation, - double scale); - /** - * \brief Get the maximum number of instances for this component - * - * \return The maximum number of instances for this component - */ + Transform(game_object_id_t id, const Vector2 & point, double rotation = 0, + double scale = 0); + //! There is always exactly one transform component per entity virtual int get_instances_max() const { return 1; } - -public: - //! Translation (shift) - Vector2 position; - //! Rotation, in degrees - double rotation; - //! Multiplication factor - double scale; + //! ComponentManager instantiates all components + friend class ComponentManager; }; } // namespace crepe diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index 09bb59b..aa391fa 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -1,57 +1,44 @@ #include "Vector2.h" -namespace crepe { +using namespace crepe; -// Constructor with initial values -Vector2::Vector2(float x, float y) : x(x), y(y) {} - -// Subtracts another vector from this vector and returns the result. Vector2 Vector2::operator-(const Vector2 & other) const { return {x - other.x, y - other.y}; } -// Adds another vector to this vector and returns the result. Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y + other.y}; } -// Multiplies this vector by a scalar and returns the result. Vector2 Vector2::operator*(float scalar) const { return {x * scalar, y * scalar}; } -// Multiplies this vector by another vector element-wise and updates this vector. Vector2 & Vector2::operator*=(const Vector2 & other) { x *= other.x; y *= other.y; return *this; } -// Adds another vector to this vector and updates this vector. Vector2 & Vector2::operator+=(const Vector2 & other) { x += other.x; y += other.y; return *this; } -// Adds a scalar value to both components of this vector and updates this vector. Vector2 & Vector2::operator+=(float other) { x += other; y += other; return *this; } -// Returns the negation of this vector. Vector2 Vector2::operator-() const { return {-x, -y}; } -// Checks if this vector is equal to another vector. bool Vector2::operator==(const Vector2 & other) const { return x == other.x && y == other.y; } -// Checks if this vector is not equal to another vector. bool Vector2::operator!=(const Vector2 & other) const { return !(*this == other); } -} // namespace crepe diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 741951b..a069a57 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -2,19 +2,12 @@ namespace crepe { -//! Vector2 class -class Vector2 { -public: +//! 2D vector +struct Vector2 { //! X component of the vector - float x; + float x = 0; //! Y component of the vector - float y; - - //! Default constructor - Vector2() = default; - - //! Constructor with initial values - Vector2(float x, float y); + float y = 0; //! Subtracts another vector from this vector and returns the result. Vector2 operator-(const Vector2 & other) const; |