63 lines
1.7 KiB
C++

// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "assert/assert.hpp"
#include "scene/SceneItem.hpp"
#include "Map.hpp"
#include "component/entity/Entity.hpp"
using namespace Dawn;
void Map::entityNotifyInit(std::shared_ptr<SceneItem> item) {
auto entity = item->getComponent<Entity>();
assertNotNull(entity, "Entity component not found on item.");
auto id = entity->id;
assertMapNotHasKey(this->entities, id, "Entity already exists in Map.");
assertTrue(id != EntityID::Null, "Entity ID is invalid.");
this->entities[id] = entity;
}
void Map::entityNotifyDispose(std::shared_ptr<SceneItem> item) {
auto entity = item->getComponent<Entity>();
assertNotNull(entity, "Entity component not found on item.");
auto id = entity->id;
assertMapHasKey(this->entities, id, "Entity does not exist in Map.");
assertTrue(id != EntityID::Null, "Entity ID is invalid.");
this->entities.erase(id);
}
void Map::onInit() {
}
void Map::onDispose() {
}
std::shared_ptr<World> Map::getWorld() {
auto world = this->world.lock();
assertNotNull(world, "World cannot be null?");
return world;
}
std::shared_ptr<Entity> Map::getEntity(const EntityID id) {
assertMapHasKey(this->entities, id, "Entity does not exist in Map.");
auto ent = this->entities[id];
auto lock = ent.lock();
if(!lock) {
this->entities.erase(id);
return nullptr;
}
assertTrue(lock->id == id, "Entity ID mismatch.");
return lock;
}
std::shared_ptr<Entity> Map::getEntityAt(const EntityTilePosition &pos) {
for(auto &pair : this->entities) {
auto ent = pair.second.lock();
if(!ent) continue;
if(ent->tilePosition != pos) continue;
return ent;
}
return nullptr;
}