56 lines
1.7 KiB
C++
56 lines
1.7 KiB
C++
#include "game/entities/entity_manager.hpp"
|
|
|
|
#include <utility>
|
|
|
|
#include "game/entities/concrete/door.hpp"
|
|
#include "game/entities/concrete/sarcophagus.hpp"
|
|
#include "game/entities/entity.hpp"
|
|
|
|
EntityManager::EntityManager() {
|
|
// Subscripció al bus: cada sarcòfag obert incrementa el comptador;
|
|
// quan iguala el total registrat, obrim la porta. La subscripció
|
|
// sobreviu tot el nivell.
|
|
sarc_sub_id_ = bus_.subscribe("sarcophagus_opened", [this](const Event& /*ev*/) {
|
|
++opened_sarcs_;
|
|
if (total_sarcs_ > 0 && opened_sarcs_ >= total_sarcs_ && door_ != nullptr) {
|
|
door_->open();
|
|
}
|
|
});
|
|
}
|
|
|
|
EntityManager::~EntityManager() {
|
|
bus_.unsubscribe(sarc_sub_id_);
|
|
}
|
|
|
|
auto EntityManager::add(std::unique_ptr<Entity> entity) -> Entity* {
|
|
if (entity == nullptr) { return nullptr; }
|
|
entity->setEventBus(&bus_);
|
|
Entity* raw = entity.get();
|
|
entities_.push_back(std::move(entity));
|
|
// cppcheck-suppress returnDanglingLifetime
|
|
return raw;
|
|
}
|
|
|
|
void EntityManager::setDoor(Door* door) {
|
|
door_ = door;
|
|
if (door_ != nullptr) { door_->setEventBus(&bus_); }
|
|
}
|
|
|
|
auto EntityManager::addSarcophagus(std::unique_ptr<Sarcophagus> sarc) -> Sarcophagus* {
|
|
if (sarc == nullptr) { return nullptr; }
|
|
sarc->setEventBus(&bus_);
|
|
Sarcophagus* raw = sarc.get();
|
|
entities_.push_back(std::move(sarc));
|
|
++total_sarcs_;
|
|
// cppcheck-suppress returnDanglingLifetime
|
|
return raw;
|
|
}
|
|
|
|
void EntityManager::update(float delta_time) {
|
|
for (auto& e : entities_) { e->update(delta_time); }
|
|
}
|
|
|
|
void EntityManager::notifyPlayerEnteredTile(int col, int row) {
|
|
for (auto& e : entities_) { e->notifyTileEntered(col, row); }
|
|
}
|