Use shared_ptr for components instead of unique_ptr
This commit is contained in:
parent
7808b7dbf7
commit
17b81c83cc
|
@ -23,6 +23,9 @@ private:
|
|||
float movement_speed = 130;
|
||||
sf::Vector2u window_size;
|
||||
|
||||
// Components:
|
||||
std::shared_ptr<simpleengine::HitboxComponent> hitbox_component;
|
||||
std::shared_ptr<simpleengine::SideScrollerMovementAnimationComponent> 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<simpleengine::SideScrollerMovementAnimationComponent>(*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<simpleengine::SideScrollerMovementAnimationComponent>(*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<simpleengine::HitboxComponent>(*this, sprite,
|
||||
20, 12,
|
||||
sprite.getGlobalBounds().width - 40, sprite.getGlobalBounds().height - 15));
|
||||
hitbox_component = std::make_shared<simpleengine::HitboxComponent>(*this, sprite,
|
||||
20, 12, sprite.getGlobalBounds().width - 40, sprite.getGlobalBounds().height - 15);
|
||||
AddComponent(hitbox_component);
|
||||
}
|
||||
|
||||
~PlayerEntity() override {
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <assert.h>
|
||||
|
||||
namespace simpleengine {
|
||||
class Component;
|
||||
|
@ -41,7 +42,7 @@ namespace simpleengine {
|
|||
|
||||
template<typename T>
|
||||
bool HasComponent() {
|
||||
for (std::unique_ptr<Component>& comp : components) {
|
||||
for (std::shared_ptr<Component>& comp : components) {
|
||||
if (dynamic_cast<T*>(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> component);
|
||||
|
||||
template<typename T>
|
||||
void AddComponent(std::shared_ptr<T> component) {
|
||||
static_assert(std::is_base_of_v<Component, T>, "Component class must derive from simpleengine::Component");
|
||||
|
||||
// Only allow one type of the same component.
|
||||
assert(!HasComponent<T>());
|
||||
components.push_back(component);
|
||||
}
|
||||
|
||||
sf::Sprite& GetSprite();
|
||||
protected:
|
||||
sf::Sprite& sprite;
|
||||
std::vector<std::unique_ptr<Component>> components;
|
||||
std::vector<std::shared_ptr<Component>> components;
|
||||
bool destroying = false;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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>& component : components) {
|
||||
for (std::shared_ptr<Component>& 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>& component : components) {
|
||||
for (std::shared_ptr<Component>& component : components) {
|
||||
component->Render(target);
|
||||
}
|
||||
}
|
||||
|
||||
void simpleengine::Entity::AddComponent(std::unique_ptr<Component> component) {
|
||||
components.push_back(std::move(component));
|
||||
}
|
||||
|
||||
void simpleengine::Entity::Destroying() {
|
||||
for (std::unique_ptr<Component>& component : components) {
|
||||
for (std::shared_ptr<Component>& component : components) {
|
||||
component->DestroyLater();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue