From 17b81c83cc88898b78977760fe722484462c5ff3 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sun, 12 Jul 2020 23:16:59 -0500 Subject: [PATCH] Use shared_ptr for components instead of unique_ptr --- examples/animation/src/main.cpp | 20 ++++++++++++-------- include/simpleengine/entity.h | 15 ++++++++++++--- src/entity.cpp | 10 +++------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/examples/animation/src/main.cpp b/examples/animation/src/main.cpp index 6e70d1d..9f91f2b 100644 --- a/examples/animation/src/main.cpp +++ b/examples/animation/src/main.cpp @@ -23,6 +23,9 @@ private: float movement_speed = 130; sf::Vector2u window_size; + // Components: + std::shared_ptr hitbox_component; + std::shared_ptr move_anim_component; public: explicit PlayerEntity(sf::Vector2u window_size) : Entity(sprite), window_size(window_size) { texture.loadFromFile("player_sheet.png"); @@ -30,17 +33,18 @@ public: sprite.setTexture(texture); sprite.setScale(.7, .7); - auto move_anim_component = std::make_unique(*this, sprite, - texture, movement_speed, 5, 1.1); - move_anim_component->SetAnimation(simpleengine::MovementAnimationType::WALK_LEFT, 8, 0, 8, - 9, 8, 128, 128); + move_anim_component = std::make_shared(*this, sprite, + texture, movement_speed, 5, 1.1); + move_anim_component->SetAnimation(simpleengine::MovementAnimationType::WALK_LEFT, 8, 0, 9, + 9, 9, 128, 128); move_anim_component->SetAnimation(simpleengine::MovementAnimationType::IDLE_LEFT, 20, 0, 0, - 6, 0, 128, 128); + 6, 0, 128, 128); AddComponent(std::move(move_anim_component)); + AddComponent(move_anim_component); - AddComponent(std::make_unique(*this, sprite, - 20, 12, - sprite.getGlobalBounds().width - 40, sprite.getGlobalBounds().height - 15)); + hitbox_component = std::make_shared(*this, sprite, + 20, 12, sprite.getGlobalBounds().width - 40, sprite.getGlobalBounds().height - 15); + AddComponent(hitbox_component); } ~PlayerEntity() override { diff --git a/include/simpleengine/entity.h b/include/simpleengine/entity.h index b496524..fdc761a 100644 --- a/include/simpleengine/entity.h +++ b/include/simpleengine/entity.h @@ -11,6 +11,7 @@ #include #include +#include namespace simpleengine { class Component; @@ -41,7 +42,7 @@ namespace simpleengine { template bool HasComponent() { - for (std::unique_ptr& comp : components) { + for (std::shared_ptr& comp : components) { if (dynamic_cast(comp.get())) { return true; } @@ -52,12 +53,20 @@ namespace simpleengine { void UpdateComponents(const float& delta_time); void RenderComponents(sf::RenderTarget* target); - void AddComponent(std::unique_ptr component); + + template + void AddComponent(std::shared_ptr component) { + static_assert(std::is_base_of_v, "Component class must derive from simpleengine::Component"); + + // Only allow one type of the same component. + assert(!HasComponent()); + components.push_back(component); + } sf::Sprite& GetSprite(); protected: sf::Sprite& sprite; - std::vector> components; + std::vector> components; bool destroying = false; }; } diff --git a/src/entity.cpp b/src/entity.cpp index da1dcb6..bfbd068 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -32,7 +32,7 @@ void simpleengine::Entity::Update(const float &delta_time) { } void simpleengine::Entity::UpdateComponents(const float& delta_time) { - for (std::unique_ptr& component : components) { + for (std::shared_ptr& component : components) { component->Update(delta_time); if (component->IsGettingDestroyed()) { @@ -42,17 +42,13 @@ void simpleengine::Entity::UpdateComponents(const float& delta_time) { } void simpleengine::Entity::RenderComponents(sf::RenderTarget* target) { - for (std::unique_ptr& component : components) { + for (std::shared_ptr& component : components) { component->Render(target); } } -void simpleengine::Entity::AddComponent(std::unique_ptr component) { - components.push_back(std::move(component)); -} - void simpleengine::Entity::Destroying() { - for (std::unique_ptr& component : components) { + for (std::shared_ptr& component : components) { component->DestroyLater(); } }