world.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "world.h"
  2. #include "../systems/owner_registry.h"
  3. #include "../systems/troop_count_registry.h"
  4. #include "component.h"
  5. #include "core/entity.h"
  6. #include "core/system.h"
  7. #include <algorithm>
  8. #include <memory>
  9. #include <mutex>
  10. #include <utility>
  11. #include <vector>
  12. namespace Engine::Core {
  13. World::World() = default;
  14. World::~World() = default;
  15. auto World::createEntity() -> Entity * {
  16. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  17. EntityID const id = m_nextEntityId++;
  18. auto entity = std::make_unique<Entity>(id);
  19. auto *ptr = entity.get();
  20. m_entities[id] = std::move(entity);
  21. return ptr;
  22. }
  23. auto World::createEntityWithId(EntityID entity_id) -> Entity * {
  24. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  25. if (entity_id == NULL_ENTITY) {
  26. return nullptr;
  27. }
  28. auto entity = std::make_unique<Entity>(entity_id);
  29. auto *ptr = entity.get();
  30. m_entities[entity_id] = std::move(entity);
  31. if (entity_id >= m_nextEntityId) {
  32. m_nextEntityId = entity_id + 1;
  33. }
  34. return ptr;
  35. }
  36. void World::destroyEntity(EntityID entity_id) {
  37. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  38. m_entities.erase(entity_id);
  39. }
  40. void World::clear() {
  41. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  42. m_entities.clear();
  43. m_nextEntityId = 1;
  44. }
  45. auto World::getEntity(EntityID entity_id) -> Entity * {
  46. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  47. auto it = m_entities.find(entity_id);
  48. return it != m_entities.end() ? it->second.get() : nullptr;
  49. }
  50. void World::addSystem(std::unique_ptr<System> system) {
  51. m_systems.push_back(std::move(system));
  52. }
  53. void World::update(float deltaTime) {
  54. for (auto &system : m_systems) {
  55. system->update(this, deltaTime);
  56. }
  57. }
  58. auto World::getUnitsOwnedBy(int owner_id) const -> std::vector<Entity *> {
  59. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  60. std::vector<Entity *> result;
  61. result.reserve(m_entities.size());
  62. for (const auto &[entity_id, entity] : m_entities) {
  63. auto *unit = entity->getComponent<UnitComponent>();
  64. if (unit == nullptr) {
  65. continue;
  66. }
  67. if (unit->owner_id == owner_id) {
  68. result.push_back(entity.get());
  69. }
  70. }
  71. return result;
  72. }
  73. auto World::getUnitsNotOwnedBy(int owner_id) const -> std::vector<Entity *> {
  74. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  75. std::vector<Entity *> result;
  76. result.reserve(m_entities.size());
  77. for (const auto &[entity_id, entity] : m_entities) {
  78. auto *unit = entity->getComponent<UnitComponent>();
  79. if (unit == nullptr) {
  80. continue;
  81. }
  82. if (unit->owner_id != owner_id) {
  83. result.push_back(entity.get());
  84. }
  85. }
  86. return result;
  87. }
  88. auto World::getAlliedUnits(int owner_id) const -> std::vector<Entity *> {
  89. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  90. std::vector<Entity *> result;
  91. result.reserve(m_entities.size());
  92. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  93. for (const auto &[entity_id, entity] : m_entities) {
  94. auto *unit = entity->getComponent<UnitComponent>();
  95. if (unit == nullptr) {
  96. continue;
  97. }
  98. if (unit->owner_id == owner_id ||
  99. owner_registry.areAllies(owner_id, unit->owner_id)) {
  100. result.push_back(entity.get());
  101. }
  102. }
  103. return result;
  104. }
  105. auto World::getEnemyUnits(int owner_id) const -> std::vector<Entity *> {
  106. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  107. std::vector<Entity *> result;
  108. result.reserve(m_entities.size());
  109. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  110. for (const auto &[entity_id, entity] : m_entities) {
  111. auto *unit = entity->getComponent<UnitComponent>();
  112. if (unit == nullptr) {
  113. continue;
  114. }
  115. if (owner_registry.areEnemies(owner_id, unit->owner_id)) {
  116. result.push_back(entity.get());
  117. }
  118. }
  119. return result;
  120. }
  121. auto World::countTroopsForPlayer(int owner_id) -> int {
  122. return Game::Systems::TroopCountRegistry::instance().getTroopCount(owner_id);
  123. }
  124. auto World::getNextEntityId() const -> EntityID {
  125. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  126. return m_nextEntityId;
  127. }
  128. void World::setNextEntityId(EntityID next_id) {
  129. const std::lock_guard<std::recursive_mutex> lock(m_entityMutex);
  130. m_nextEntityId = std::max(next_id, m_nextEntityId);
  131. }
  132. } // namespace Engine::Core