From 88f613cbb2e4aaf3ed55ac0c6490706dd6f6f19d Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 20:12:31 +0100 Subject: rendering based on world unites instead of pixels --- src/crepe/system/AnimatorSystem.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/crepe/system/AnimatorSystem.cpp') diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 9d18873..ceb5bfd 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,8 +3,8 @@ #include #include "api/Animator.h" -#include "facade/SDLContext.h" +#include "facade/SDLContext.h" #include "AnimatorSystem.h" #include "ComponentManager.h" @@ -19,6 +19,7 @@ void AnimatorSystem::update() { uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { if (a.active) { + // (10 frames per second) a.curr_row = (tick / 100) % a.row; a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; a.spritesheet.sprite_rect = a.animator_rect; -- cgit v1.2.3 From bdc81e355e5bee5d2a3e29346ba08f7bc55196ca Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 12:07:07 +0100 Subject: adjusted branch based on feedback --- mwe/events/include/event.h | 2 +- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/Camera.h | 18 +++++------------- src/crepe/api/Vector2.cpp | 4 +++- src/crepe/api/Vector2.h | 2 ++ src/crepe/facade/SDLContext.cpp | 7 ++----- src/crepe/system/AnimatorSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 1 + src/example/rendering.cpp | 10 +++++----- 9 files changed, 21 insertions(+), 27 deletions(-) (limited to 'src/crepe/system/AnimatorSystem.cpp') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index ee1bf52..e1b220b 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index f3d809c..d206428 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = (col_animator - 1)* animator_rect.h; + animator_rect.y = (col_animator - 1) * animator_rect.h; this->active = false; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index dfccd24..d7292ef 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -28,25 +28,17 @@ public: //! Background color of the camera view. Color bg_color; - /** - * \pos The position of the camera in world units - */ + //! pos The position of the camera in world units Vector2 pos = {0, 0}; - /** - * \screen the display size in pixels ( output resolution ) - */ + //! screen the display size in pixels ( output resolution ) Vector2 screen = {640, 480}; - /** - * \viewport is the area of the world visible through the camera (in world units) - */ + //! viewport is the area of the world visible through the camera (in world units) Vector2 viewport = {500, 500}; - /** - * \scale scaling factor from world units to pixel coordinates - */ - Vector2 scale = {0,0}; + //! scale scaling factor from world units to pixel coordinates + Vector2 scale = {0, 0}; //! Zoom level of the camera view. double zoom = 1.0f; diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index c3a49b7..8658c00 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -8,14 +8,16 @@ Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y Vector2 Vector2::operator*(double scalar) const { return {x * scalar, y * scalar}; } -Vector2 Vector2::operator*(const Vector2 & other) const{ +Vector2 Vector2::operator*(const Vector2 & other) const { return {this->x * other.x, this->y * other.y}; } + Vector2 & Vector2::operator*=(const Vector2 & other) { x *= other.x; y *= other.y; return *this; } + Vector2 & Vector2::operator*=(const double & other) { x *= other; y *= other; diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 2a5db1d..790160d 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -18,6 +18,7 @@ struct Vector2 { //! Multiplies this vector by a scalar and returns the result. Vector2 operator*(double scalar) const; + //! Multiplies this vector by another vector element-wise and updates this vector. Vector2 operator*(const Vector2 & other) const; //! Multiplies this vector by another vector element-wise and updates this vector. @@ -26,6 +27,7 @@ struct Vector2 { //! Multiplies a scalar value to both components of this vector and updates this vector. Vector2 & operator*=(const double & other); + //! Divides this vector by another vector element-wise and updates this vector. Vector2 operator/(const Vector2 & other) const; //! Adds another vector to this vector and updates this vector. diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 785b285..0cb7be9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -38,7 +38,7 @@ SDLContext::SDLContext() { } SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - this->viewport.w, this->viewport.h, SDL_WINDOW_RESIZABLE); + this->viewport.w, this->viewport.h, 0); if (!tmp_window) { // FIXME: throw exception std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; @@ -104,13 +104,11 @@ void SDLContext::handle_events(bool & running) { void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } - void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - Vector2 pixel_coord = (transform.position - cam.pos) * cam.scale; double pixel_w = sprite.sprite_rect.w * transform.scale * cam.scale.x; @@ -148,11 +146,10 @@ void SDLContext::camera(Camera & cam) { cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; } - if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) { this->viewport.w = cam.screen.x; this->viewport.h = cam.screen.y; - SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y); + SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index ceb5bfd..efe0e48 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,8 +3,8 @@ #include #include "api/Animator.h" - #include "facade/SDLContext.h" + #include "AnimatorSystem.h" #include "ComponentManager.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 989a82f..7538e20 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -13,6 +13,7 @@ using namespace crepe; void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } + void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 1db57e5..418e03d 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -24,7 +24,7 @@ int main() { ComponentManager mgr{}; RenderSystem sys{mgr}; - AnimatorSystem anim_sys {mgr}; + AnimatorSystem anim_sys{mgr}; GameObject obj = mgr.new_object("name", "tag", Vector2{250, 0}, 0, 1); GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); @@ -33,10 +33,11 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - Sprite & sprite = obj.add_component(make_shared("../asset/spritesheet/spritesheet_test.png"), color, - FlipSettings{false, false}); + Sprite & sprite = obj.add_component( + make_shared("../asset/spritesheet/spritesheet_test.png"), color, + FlipSettings{false, false}); Camera & cam = obj.add_component(Color::get_red()); - obj.add_component(sprite, 4,1,1).active = true; + obj.add_component(sprite, 4, 1, 1).active = true; } /* { @@ -54,7 +55,6 @@ int main() { } */ - auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { anim_sys.update(); -- cgit v1.2.3 From 2d623522db0677ca5e88a53e3705a10ce59ba8b6 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 10:04:14 +0100 Subject: removed animator rectangle --- src/crepe/api/Animator.cpp | 11 +++++------ src/crepe/api/Animator.h | 2 -- src/crepe/api/Camera.h | 6 +++--- src/crepe/system/AnimatorSystem.cpp | 11 +++++------ src/crepe/system/RenderSystem.h | 2 +- 5 files changed, 14 insertions(+), 18 deletions(-) (limited to 'src/crepe/system/AnimatorSystem.cpp') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index b6540cf..31b9632 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -14,14 +14,13 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a col(col) { dbg_trace(); - animator_rect = spritesheet.sprite_rect; - animator_rect.h /= col; - animator_rect.w /= row; - animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + this->spritesheet.sprite_rect.h /= col; + this->spritesheet.sprite_rect.w /= row; + this->spritesheet.sprite_rect.x = 0; + this->spritesheet.sprite_rect.y = col_animator * this->spritesheet.sprite_rect.h; this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio = static_cast(animator_rect.w) / animator_rect.h; + this->spritesheet.aspect_ratio = static_cast(this->spritesheet.sprite_rect.w) / this->spritesheet.sprite_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 19c9ebd..6c506aa 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -57,8 +57,6 @@ private: //! The current row being animated. int curr_row = 0; - Rect animator_rect; - //TODO: Is this necessary? //int fps; diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index ac56495..3682222 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -33,13 +33,13 @@ public: vec2 offset; //! screen the display size in pixels ( output resolution ) - const ivec2 screen = {1080, 720}; + const ivec2 screen; //! viewport is the area of the world visible through the camera (in world units) - const ivec2 viewport = {500, 1000}; + const ivec2 viewport; //! Zoom level of the camera view. - const double zoom = 1.0f; + const double zoom; public: /** diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index bc94253..e5b277f 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -15,11 +15,10 @@ void AnimatorSystem::update() { uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { - if (a.active) { - // (10 frames per second) - a.curr_row = (tick / 100) % a.row; - a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; - a.spritesheet.sprite_rect = a.animator_rect; - } + if (!a.active) continue; + // (10 frames per second) + a.curr_row = (tick / 100) % a.row; + a.spritesheet.sprite_rect.x = (a.curr_row * a.spritesheet.sprite_rect.w) + a.curr_col; + a.spritesheet.sprite_rect = a.spritesheet.sprite_rect; } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 4667424..7279b5c 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -79,7 +79,7 @@ private: SDLContext & context = SDLContext::get_instance(); //! camera postion in the current scene - vec2 cam_pos = {0,0}; + vec2 cam_pos; }; -- cgit v1.2.3 From 6fd7cec7d4bbf5aeb361b3f1337671bb0f9af61b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 10:13:11 +0100 Subject: manager mediator refactor --- src/crepe/CMakeLists.txt | 4 +- src/crepe/ComponentManager.cpp | 60 ------------ src/crepe/ComponentManager.h | 159 ------------------------------ src/crepe/ComponentManager.hpp | 161 ------------------------------ src/crepe/api/BehaviorScript.cpp | 6 +- src/crepe/api/BehaviorScript.h | 9 +- src/crepe/api/BehaviorScript.hpp | 4 +- src/crepe/api/CMakeLists.txt | 8 -- src/crepe/api/EventManager.cpp | 46 --------- src/crepe/api/EventManager.h | 161 ------------------------------ src/crepe/api/EventManager.hpp | 36 ------- src/crepe/api/GameObject.hpp | 2 +- src/crepe/api/IKeyListener.h | 3 +- src/crepe/api/IMouseListener.h | 3 +- src/crepe/api/LoopManager.cpp | 25 ++--- src/crepe/api/LoopManager.h | 19 +++- src/crepe/api/LoopManager.hpp | 7 +- src/crepe/api/SaveManager.cpp | 173 --------------------------------- src/crepe/api/SaveManager.h | 114 ---------------------- src/crepe/api/Scene.h | 7 +- src/crepe/api/SceneManager.cpp | 34 ------- src/crepe/api/SceneManager.h | 53 ---------- src/crepe/api/SceneManager.hpp | 25 ----- src/crepe/api/Script.cpp | 17 +++- src/crepe/api/Script.h | 16 +-- src/crepe/api/Script.hpp | 8 +- src/crepe/manager/CMakeLists.txt | 20 ++++ src/crepe/manager/ComponentManager.cpp | 63 ++++++++++++ src/crepe/manager/ComponentManager.h | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/ComponentManager.hpp | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/EventManager.cpp | 46 +++++++++ src/crepe/manager/EventManager.h | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/EventManager.hpp | 36 +++++++ src/crepe/manager/Manager.cpp | 6 ++ src/crepe/manager/Manager.h | 17 ++++ src/crepe/manager/Mediator.h | 29 ++++++ src/crepe/manager/SaveManager.cpp | 173 +++++++++++++++++++++++++++++++++ src/crepe/manager/SaveManager.h | 114 ++++++++++++++++++++++ src/crepe/manager/SceneManager.cpp | 35 +++++++ src/crepe/manager/SceneManager.h | 52 ++++++++++ src/crepe/manager/SceneManager.hpp | 25 +++++ src/crepe/system/AnimatorSystem.cpp | 6 +- src/crepe/system/ParticleSystem.cpp | 6 +- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 3 +- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/system/System.cpp | 2 +- src/crepe/system/System.h | 4 +- src/test/DBTest.cpp | 3 +- src/test/ECSTest.cpp | 5 +- src/test/EventTest.cpp | 11 ++- src/test/ParticleTest.cpp | 8 +- src/test/PhysicsTest.cpp | 7 +- src/test/RenderSystemTest.cpp | 9 +- src/test/SceneManagerTest.cpp | 23 +++-- src/test/ScriptTest.cpp | 11 ++- 56 files changed, 1240 insertions(+), 1121 deletions(-) delete mode 100644 src/crepe/ComponentManager.cpp delete mode 100644 src/crepe/ComponentManager.h delete mode 100644 src/crepe/ComponentManager.hpp delete mode 100644 src/crepe/api/EventManager.cpp delete mode 100644 src/crepe/api/EventManager.h delete mode 100644 src/crepe/api/EventManager.hpp delete mode 100644 src/crepe/api/SaveManager.cpp delete mode 100644 src/crepe/api/SaveManager.h delete mode 100644 src/crepe/api/SceneManager.cpp delete mode 100644 src/crepe/api/SceneManager.h delete mode 100644 src/crepe/api/SceneManager.hpp create mode 100644 src/crepe/manager/CMakeLists.txt create mode 100644 src/crepe/manager/ComponentManager.cpp create mode 100644 src/crepe/manager/ComponentManager.h create mode 100644 src/crepe/manager/ComponentManager.hpp create mode 100644 src/crepe/manager/EventManager.cpp create mode 100644 src/crepe/manager/EventManager.h create mode 100644 src/crepe/manager/EventManager.hpp create mode 100644 src/crepe/manager/Manager.cpp create mode 100644 src/crepe/manager/Manager.h create mode 100644 src/crepe/manager/Mediator.h create mode 100644 src/crepe/manager/SaveManager.cpp create mode 100644 src/crepe/manager/SaveManager.h create mode 100644 src/crepe/manager/SceneManager.cpp create mode 100644 src/crepe/manager/SceneManager.h create mode 100644 src/crepe/manager/SceneManager.hpp (limited to 'src/crepe/system/AnimatorSystem.cpp') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 7e176e7..da9d492 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,13 +1,10 @@ target_sources(crepe PUBLIC Particle.cpp - ComponentManager.cpp Component.cpp Collider.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - ComponentManager.h - ComponentManager.hpp Component.h Collider.h ValueBroker.h @@ -16,6 +13,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES add_subdirectory(api) add_subdirectory(facade) +add_subdirectory(manager) add_subdirectory(system) add_subdirectory(util) diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp deleted file mode 100644 index 5b73009..0000000 --- a/src/crepe/ComponentManager.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "api/GameObject.h" -#include "util/Log.h" - -#include "ComponentManager.h" -#include "types.h" - -using namespace crepe; -using namespace std; - -ComponentManager::ComponentManager() { dbg_trace(); } -ComponentManager::~ComponentManager() { dbg_trace(); } - -void ComponentManager::delete_all_components_of_id(game_object_id_t id) { - // Do not delete persistent objects - if (this->persistent[id]) { - return; - } - - // Loop through all the types (in the unordered_map<>) - for (auto & [type, component_array] : this->components) { - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < component_array.size()) { - // Clear the components at this specific id - component_array[id].clear(); - } - } -} - -void ComponentManager::delete_all_components() { - // Loop through all the types (in the unordered_map<>) - for (auto & [type, component_array] : this->components) { - // Loop through all the ids (in the vector<>) - for (game_object_id_t id = 0; id < component_array.size(); id++) { - // Do not delete persistent objects - if (!this->persistent[id]) { - // Clear the components at this specific id - component_array[id].clear(); - } - } - } - - this->next_id = 0; -} - -GameObject ComponentManager::new_object(const string & name, const string & tag, - const vec2 & position, double rotation, double scale) { - // Find the first available id (taking persistent objects into account) - while (this->persistent[this->next_id]) { - this->next_id++; - } - - GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; - this->next_id++; - - return object; -} - -void ComponentManager::set_persistent(game_object_id_t id, bool persistent) { - this->persistent[id] = persistent; -} diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h deleted file mode 100644 index 480124f..0000000 --- a/src/crepe/ComponentManager.h +++ /dev/null @@ -1,159 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Component.h" -#include "types.h" - -namespace crepe { - -class GameObject; - -/** - * \brief Manages all components - * - * This class manages all components. It provides methods to add, delete and get components. - */ -class ComponentManager { - // TODO: This relation should be removed! I (loek) believe that the scene manager should - // create/destroy components because the GameObject's are stored in concrete Scene classes, - // which will in turn call GameObject's destructor, which will in turn call - // ComponentManager::delete_components_by_id or something. This is a pretty major change, so - // here is a comment and temporary fix instead :tada: - friend class SceneManager; - -public: - ComponentManager(); // dbg_trace - ~ComponentManager(); // dbg_trace - - /** - * \brief Create a new game object using the component manager - * - * \param name Metadata::name (required) - * \param tag Metadata::tag (optional, empty by default) - * \param position Transform::position (optional, origin by default) - * \param rotation Transform::rotation (optional, 0 by default) - * \param scale Transform::scale (optional, 1 by default) - * - * \returns GameObject interface - * - * \note This method automatically assigns a new entity ID - */ - GameObject new_object(const std::string & name, const std::string & tag = "", - const vec2 & position = {0, 0}, double rotation = 0, - double scale = 1); - -protected: - /** - * GameObject is used as an interface to add/remove components, and the game programmer is - * supposed to use it instead of interfacing with the component manager directly. - */ - friend class GameObject; - /** - * \brief Add a component to the ComponentManager - * - * This method adds a component to the ComponentManager. The component is created with the - * given arguments and added to the ComponentManager. - * - * \tparam T The type of the component - * \tparam Args The types of the arguments - * \param id The id of the GameObject this component belongs to - * \param args The arguments to create the component - * \return The created component - */ - template - T & add_component(game_object_id_t id, Args &&... args); - /** - * \brief Delete all components of a specific type and id - * - * This method deletes all components of a specific type and id. - * - * \tparam T The type of the component - * \param id The id of the GameObject this component belongs to - */ - template - void delete_components_by_id(game_object_id_t id); - /** - * \brief Delete all components of a specific type - * - * This method deletes all components of a specific type. - * - * \tparam T The type of the component - */ - template - void delete_components(); - /** - * \brief Delete all components of a specific id - * - * This method deletes all components of a specific id. - * - * \param id The id of the GameObject this component belongs to - */ - void delete_all_components_of_id(game_object_id_t id); - /** - * \brief Delete all components - * - * This method deletes all components. - */ - void delete_all_components(); - /** - * \brief Set a GameObject as persistent - * - * This method sets a GameObject as persistent. If a GameObject is persistent, its - * components will not be deleted. - * - * \param id The id of the GameObject to set as persistent - * \param persistent The persistent flag - */ - void set_persistent(game_object_id_t id, bool persistent); - -public: - /** - * \brief Get all components of a specific type and id - * - * This method gets all components of a specific type and id. - * - * \tparam T The type of the component - * \param id The id of the GameObject this component belongs to - * \return A vector of all components of the specific type and id - */ - template - RefVector get_components_by_id(game_object_id_t id) const; - /** - * \brief Get all components of a specific type - * - * This method gets all components of a specific type. - * - * \tparam T The type of the component - * \return A vector of all components of the specific type - */ - template - RefVector get_components_by_type() const; - -private: - /** - * \brief The components - * - * This unordered_map stores all components. The key is the type of the component and the - * value is a vector of vectors of unique pointers to the components. - * - * Every component type has its own vector of vectors of unique pointers to the components. - * The first vector is for the ids of the GameObjects and the second vector is for the - * components (because a GameObject might have multiple components). - */ - std::unordered_map>>> - components; - - //! Persistent flag for each GameObject - std::unordered_map persistent; - - //! ID of next GameObject allocated by \c ComponentManager::new_object - game_object_id_t next_id = 0; -}; - -} // namespace crepe - -#include "ComponentManager.hpp" diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp deleted file mode 100644 index ffb38ec..0000000 --- a/src/crepe/ComponentManager.hpp +++ /dev/null @@ -1,161 +0,0 @@ -#pragma once - -#include - -#include "ComponentManager.h" -#include "types.h" - -namespace crepe { - -template -T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { - using namespace std; - - static_assert(is_base_of::value, - "add_component must recieve a derivative class of Component"); - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Check if this component type is already in the unordered_map<> - if (this->components.find(type) == this->components.end()) { - //If not, create a new (empty) vector<> of vector> - this->components[type] = vector>>(); - } - - // Resize the vector<> if the id is greater than the current size - if (id >= this->components[type].size()) { - // Initialize new slots to nullptr (resize does automatically init to nullptr) - this->components[type].resize(id + 1); - } - - // Create a new component of type T (arguments directly forwarded). The - // constructor must be called by ComponentManager. - T * instance_ptr = new T(id, forward(args)...); - if (instance_ptr == nullptr) throw std::bad_alloc(); - - T & instance_ref = *instance_ptr; - unique_ptr instance = unique_ptr(instance_ptr); - - // Check if the vector size is not greater than get_instances_max - int max_instances = instance->get_instances_max(); - if (max_instances != -1 && components[type][id].size() >= max_instances) { - throw std::runtime_error( - "Exceeded maximum number of instances for this component type"); - } - - // store its unique_ptr in the vector<> - this->components[type][id].push_back(std::move(instance)); - - return instance_ref; -} - -template -void ComponentManager::delete_components_by_id(game_object_id_t id) { - using namespace std; - - // Do not delete persistent objects - if (this->persistent[id]) { - return; - } - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Find the type (in the unordered_map<>) - if (this->components.find(type) != this->components.end()) { - // Get the correct vector<> - vector>> & component_array = this->components[type]; - - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < component_array.size()) { - // Clear the whole vector<> of this specific type and id - component_array[id].clear(); - } - } -} - -template -void ComponentManager::delete_components() { - // Determine the type of T (this is used as the key of the unordered_map<>) - std::type_index type = typeid(T); - - if (this->components.find(type) == this->components.end()) return; - - // Loop through the whole vector<> of this specific type - for (game_object_id_t i = 0; i < this->components[type].size(); ++i) { - // Do not delete persistent objects - if (!this->persistent[i]) { - this->components[type][i].clear(); - } - } -} - -template -RefVector ComponentManager::get_components_by_id(game_object_id_t id) const { - using namespace std; - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Create an empty vector<> - RefVector component_vector; - - if (this->components.find(type) == this->components.end()) return component_vector; - - // Get the correct vector<> - const vector>> & component_array = this->components.at(type); - - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id >= component_array.size()) return component_vector; - - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component_array[id]) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - if (casted_component == nullptr) continue; - - // Add the dereferenced raw pointer to the vector<> - component_vector.push_back(*casted_component); - } - - return component_vector; -} - -template -RefVector ComponentManager::get_components_by_type() const { - using namespace std; - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Create an empty vector<> - RefVector component_vector; - - // Find the type (in the unordered_map<>) - if (this->components.find(type) == this->components.end()) return component_vector; - - // Get the correct vector<> - const vector>> & component_array = this->components.at(type); - - // Loop through the whole vector<> - for (const vector> & component : component_array) { - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - // Ensure that the cast was successful - if (casted_component == nullptr) continue; - - // Add the dereferenced raw pointer to the vector<> - component_vector.emplace_back(ref(*casted_component)); - } - } - - // Return the vector<> - return component_vector; -} - -} // namespace crepe diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index 7bbace0..d22afdf 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -4,12 +4,12 @@ using namespace crepe; -BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) +BehaviorScript::BehaviorScript(game_object_id_t id, Mediator & mediator) : Component(id), - component_manager(mgr) {} + mediator(mediator) {} template <> BehaviorScript & GameObject::add_component() { ComponentManager & mgr = this->component_manager; - return mgr.add_component(this->id, mgr); + return mgr.add_component(this->id, mgr.mediator); } diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index d556fe5..3909b96 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -23,14 +23,13 @@ class BehaviorScript : public Component { protected: /** * \param id Parent \c GameObject id - * \param component_manager Reference to component manager (passed through to \c Script - * instance) + * \param mediator Mediator reference * * \note Calls to this constructor (should) always pass through \c GameObject::add_component, * which has an exception for this specific component type. This was done so the user does * not have to pass references used within \c Script to each \c BehaviorScript instance. */ - BehaviorScript(game_object_id_t id, ComponentManager & component_manager); + BehaviorScript(game_object_id_t id, Mediator & mediator); //! Only ComponentManager is allowed to instantiate BehaviorScript friend class ComponentManager; @@ -55,8 +54,8 @@ protected: friend class ScriptSystem; protected: - //! Reference to component manager (passed to Script) - ComponentManager & component_manager; + //! Reference mediator + Mediator & mediator; }; /** diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index bd59337..6de0157 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -14,11 +14,11 @@ BehaviorScript & BehaviorScript::set_script(Args &&... args) { dbg_trace(); static_assert(std::is_base_of::value); Script * s = new T(std::forward(args)...); + Mediator & mediator = this->mediator; s->game_object_id = this->game_object_id; s->active = this->active; - s->component_manager = this->component_manager; - s->event_manager = EventManager::get_instance(); + s->mediator = mediator; this->script = std::unique_ptr