world.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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::create_entity() -> Entity * {
  16. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  17. EntityID const id = m_next_entity_id++;
  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::create_entity_with_id(EntityID entity_id) -> Entity * {
  24. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  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_next_entity_id) {
  32. m_next_entity_id = entity_id + 1;
  33. }
  34. return ptr;
  35. }
  36. void World::destroy_entity(EntityID entity_id) {
  37. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  38. m_entities.erase(entity_id);
  39. }
  40. void World::clear() {
  41. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  42. m_entities.clear();
  43. m_next_entity_id = 1;
  44. }
  45. auto World::get_entity(EntityID entity_id) -> Entity * {
  46. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  47. auto it = m_entities.find(entity_id);
  48. return it != m_entities.end() ? it->second.get() : nullptr;
  49. }
  50. void World::add_system(std::unique_ptr<System> system) {
  51. m_systems.push_back(std::move(system));
  52. }
  53. void World::update(float delta_time) {
  54. for (auto &system : m_systems) {
  55. system->update(this, delta_time);
  56. }
  57. }
  58. auto World::get_units_owned_by(int owner_id) const -> std::vector<Entity *> {
  59. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  60. std::vector<Entity *> result;
  61. result.reserve(m_entities.size());
  62. for (const auto &[entity_id, entity] : m_entities) {
  63. auto *unit = entity->get_component<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::get_units_not_owned_by(int owner_id) const
  74. -> std::vector<Entity *> {
  75. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  76. std::vector<Entity *> result;
  77. result.reserve(m_entities.size());
  78. for (const auto &[entity_id, entity] : m_entities) {
  79. auto *unit = entity->get_component<UnitComponent>();
  80. if (unit == nullptr) {
  81. continue;
  82. }
  83. if (unit->owner_id != owner_id) {
  84. result.push_back(entity.get());
  85. }
  86. }
  87. return result;
  88. }
  89. auto World::get_allied_units(int owner_id) const -> std::vector<Entity *> {
  90. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  91. std::vector<Entity *> result;
  92. result.reserve(m_entities.size());
  93. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  94. for (const auto &[entity_id, entity] : m_entities) {
  95. auto *unit = entity->get_component<UnitComponent>();
  96. if (unit == nullptr) {
  97. continue;
  98. }
  99. if (unit->owner_id == owner_id ||
  100. owner_registry.are_allies(owner_id, unit->owner_id)) {
  101. result.push_back(entity.get());
  102. }
  103. }
  104. return result;
  105. }
  106. auto World::get_enemy_units(int owner_id) const -> std::vector<Entity *> {
  107. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  108. std::vector<Entity *> result;
  109. result.reserve(m_entities.size());
  110. auto &owner_registry = Game::Systems::OwnerRegistry::instance();
  111. for (const auto &[entity_id, entity] : m_entities) {
  112. auto *unit = entity->get_component<UnitComponent>();
  113. if (unit == nullptr) {
  114. continue;
  115. }
  116. if (owner_registry.are_enemies(owner_id, unit->owner_id)) {
  117. result.push_back(entity.get());
  118. }
  119. }
  120. return result;
  121. }
  122. auto World::count_troops_for_player(int owner_id) -> int {
  123. return Game::Systems::TroopCountRegistry::instance().get_troop_count(
  124. owner_id);
  125. }
  126. auto World::get_next_entity_id() const -> EntityID {
  127. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  128. return m_next_entity_id;
  129. }
  130. void World::set_next_entity_id(EntityID next_id) {
  131. const std::lock_guard<std::recursive_mutex> lock(m_entity_mutex);
  132. m_next_entity_id = std::max(next_id, m_next_entity_id);
  133. }
  134. } // namespace Engine::Core