home_system.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "home_system.h"
  2. #include "../core/component.h"
  3. #include "../core/world.h"
  4. #include "../units/spawn_type.h"
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <limits>
  8. namespace Game::Systems {
  9. void HomeSystem::update(Engine::Core::World *world, float delta_time) {
  10. if (world == nullptr) {
  11. return;
  12. }
  13. auto home_entities = world->get_entities_with<Engine::Core::HomeComponent>();
  14. auto barracks_entities =
  15. world->get_entities_with<Engine::Core::ProductionComponent>();
  16. for (auto *home_entity : home_entities) {
  17. auto *home_comp = home_entity->get_component<Engine::Core::HomeComponent>();
  18. auto *home_transform =
  19. home_entity->get_component<Engine::Core::TransformComponent>();
  20. auto *home_unit = home_entity->get_component<Engine::Core::UnitComponent>();
  21. if (home_comp == nullptr || home_transform == nullptr ||
  22. home_unit == nullptr) {
  23. continue;
  24. }
  25. home_comp->update_cooldown -= delta_time;
  26. if (home_comp->update_cooldown > 0.0F) {
  27. continue;
  28. }
  29. home_comp->update_cooldown = kUpdateInterval;
  30. float min_distance = std::numeric_limits<float>::max();
  31. Engine::Core::EntityID nearest_barracks = 0;
  32. for (auto *barracks_entity : barracks_entities) {
  33. auto *barracks_transform =
  34. barracks_entity->get_component<Engine::Core::TransformComponent>();
  35. auto *barracks_unit =
  36. barracks_entity->get_component<Engine::Core::UnitComponent>();
  37. if (barracks_transform == nullptr || barracks_unit == nullptr) {
  38. continue;
  39. }
  40. if (barracks_unit->spawn_type != Game::Units::SpawnType::Barracks) {
  41. continue;
  42. }
  43. if (barracks_unit->owner_id != home_unit->owner_id) {
  44. continue;
  45. }
  46. float dx = barracks_transform->position.x - home_transform->position.x;
  47. float dz = barracks_transform->position.z - home_transform->position.z;
  48. float distance = std::sqrt(dx * dx + dz * dz);
  49. if (distance < min_distance && distance <= kMaxSearchRadius) {
  50. min_distance = distance;
  51. nearest_barracks = barracks_entity->get_id();
  52. }
  53. }
  54. Engine::Core::EntityID old_barracks = home_comp->nearest_barracks_id;
  55. home_comp->nearest_barracks_id = nearest_barracks;
  56. if (old_barracks != 0 && old_barracks != nearest_barracks) {
  57. auto *old_barracks_entity = world->get_entity(old_barracks);
  58. if (old_barracks_entity != nullptr) {
  59. auto *prod_comp =
  60. old_barracks_entity
  61. ->get_component<Engine::Core::ProductionComponent>();
  62. if (prod_comp != nullptr) {
  63. prod_comp->max_units = std::max(
  64. 0, prod_comp->max_units - home_comp->population_contribution);
  65. }
  66. }
  67. }
  68. if (nearest_barracks != 0) {
  69. auto *barracks_entity = world->get_entity(nearest_barracks);
  70. if (barracks_entity != nullptr) {
  71. auto *prod_comp =
  72. barracks_entity->get_component<Engine::Core::ProductionComponent>();
  73. if (prod_comp != nullptr) {
  74. if (old_barracks != nearest_barracks) {
  75. prod_comp->max_units += home_comp->population_contribution;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. } // namespace Game::Systems