capture_system.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "capture_system.h"
  2. #include "../core/component.h"
  3. #include "../core/event_manager.h"
  4. #include "../core/ownership_constants.h"
  5. #include "../core/world.h"
  6. #include "../units/troop_config.h"
  7. #include "../visuals/team_colors.h"
  8. #include "building_collision_registry.h"
  9. #include <cmath>
  10. #include <iostream>
  11. namespace Game::Systems {
  12. void CaptureSystem::update(Engine::Core::World *world, float deltaTime) {
  13. processBarrackCapture(world, deltaTime);
  14. }
  15. int CaptureSystem::countNearbyTroops(Engine::Core::World *world, float barrackX,
  16. float barrackZ, int ownerId,
  17. float radius) {
  18. int totalTroops = 0;
  19. auto entities = world->getEntitiesWith<Engine::Core::UnitComponent>();
  20. for (auto *e : entities) {
  21. auto *unit = e->getComponent<Engine::Core::UnitComponent>();
  22. auto *transform = e->getComponent<Engine::Core::TransformComponent>();
  23. if (!unit || !transform || unit->health <= 0) {
  24. continue;
  25. }
  26. if (unit->ownerId != ownerId) {
  27. continue;
  28. }
  29. if (unit->spawnType == Game::Units::SpawnType::Barracks) {
  30. continue;
  31. }
  32. float dx = transform->position.x - barrackX;
  33. float dz = transform->position.z - barrackZ;
  34. float distSq = dx * dx + dz * dz;
  35. if (distSq <= radius * radius) {
  36. int individualsPerUnit =
  37. Game::Units::TroopConfig::instance().getIndividualsPerUnit(
  38. unit->spawnType);
  39. totalTroops += individualsPerUnit;
  40. }
  41. }
  42. return totalTroops;
  43. }
  44. void CaptureSystem::transferBarrackOwnership(Engine::Core::World *world,
  45. Engine::Core::Entity *barrack,
  46. int newOwnerId) {
  47. auto *unit = barrack->getComponent<Engine::Core::UnitComponent>();
  48. auto *renderable = barrack->getComponent<Engine::Core::RenderableComponent>();
  49. auto *transform = barrack->getComponent<Engine::Core::TransformComponent>();
  50. auto *prod = barrack->getComponent<Engine::Core::ProductionComponent>();
  51. if (!unit || !renderable || !transform) {
  52. return;
  53. }
  54. int previousOwnerId = unit->ownerId;
  55. unit->ownerId = newOwnerId;
  56. QVector3D tc = Game::Visuals::teamColorForOwner(newOwnerId);
  57. renderable->color[0] = tc.x();
  58. renderable->color[1] = tc.y();
  59. renderable->color[2] = tc.z();
  60. Game::Systems::BuildingCollisionRegistry::instance().updateBuildingOwner(
  61. barrack->getId(), newOwnerId);
  62. if (!Game::Core::isNeutralOwner(newOwnerId) && !prod) {
  63. prod = barrack->addComponent<Engine::Core::ProductionComponent>();
  64. if (prod) {
  65. prod->productType = Game::Units::TroopType::Archer;
  66. prod->buildTime = 10.0f;
  67. prod->maxUnits = 150;
  68. prod->inProgress = false;
  69. prod->timeRemaining = 0.0f;
  70. prod->producedCount = 0;
  71. prod->rallyX = transform->position.x + 4.0f;
  72. prod->rallyZ = transform->position.z + 2.0f;
  73. prod->rallySet = true;
  74. prod->villagerCost =
  75. Game::Units::TroopConfig::instance().getIndividualsPerUnit(
  76. prod->productType);
  77. }
  78. } else if (Game::Core::isNeutralOwner(newOwnerId) && prod) {
  79. barrack->removeComponent<Engine::Core::ProductionComponent>();
  80. }
  81. Engine::Core::EventManager::instance().publish(
  82. Engine::Core::BarrackCapturedEvent(barrack->getId(), previousOwnerId,
  83. newOwnerId));
  84. }
  85. void CaptureSystem::processBarrackCapture(Engine::Core::World *world,
  86. float deltaTime) {
  87. constexpr float CAPTURE_RADIUS = 8.0f;
  88. constexpr int TROOP_ADVANTAGE_MULTIPLIER = 3;
  89. auto barracks = world->getEntitiesWith<Engine::Core::BuildingComponent>();
  90. for (auto *barrack : barracks) {
  91. auto *unit = barrack->getComponent<Engine::Core::UnitComponent>();
  92. auto *transform = barrack->getComponent<Engine::Core::TransformComponent>();
  93. if (!unit || !transform) {
  94. continue;
  95. }
  96. if (unit->spawnType != Game::Units::SpawnType::Barracks) {
  97. continue;
  98. }
  99. auto *capture = barrack->getComponent<Engine::Core::CaptureComponent>();
  100. if (!capture) {
  101. capture = barrack->addComponent<Engine::Core::CaptureComponent>();
  102. }
  103. float barrackX = transform->position.x;
  104. float barrackZ = transform->position.z;
  105. int barrackOwnerId = unit->ownerId;
  106. int maxEnemyTroops = 0;
  107. int capturingPlayerId = -1;
  108. auto entities = world->getEntitiesWith<Engine::Core::UnitComponent>();
  109. std::vector<int> playerIds;
  110. for (auto *e : entities) {
  111. auto *u = e->getComponent<Engine::Core::UnitComponent>();
  112. if (u && u->ownerId != barrackOwnerId &&
  113. !Game::Core::isNeutralOwner(u->ownerId)) {
  114. if (std::find(playerIds.begin(), playerIds.end(), u->ownerId) ==
  115. playerIds.end()) {
  116. playerIds.push_back(u->ownerId);
  117. }
  118. }
  119. }
  120. for (int playerId : playerIds) {
  121. int troopCount = countNearbyTroops(world, barrackX, barrackZ, playerId,
  122. CAPTURE_RADIUS);
  123. if (troopCount > maxEnemyTroops) {
  124. maxEnemyTroops = troopCount;
  125. capturingPlayerId = playerId;
  126. }
  127. }
  128. int defenderTroops = 0;
  129. if (!Game::Core::isNeutralOwner(barrackOwnerId)) {
  130. defenderTroops = countNearbyTroops(world, barrackX, barrackZ,
  131. barrackOwnerId, CAPTURE_RADIUS);
  132. }
  133. bool canCapture =
  134. maxEnemyTroops >= (defenderTroops * TROOP_ADVANTAGE_MULTIPLIER);
  135. if (canCapture && capturingPlayerId != -1) {
  136. if (capture->capturingPlayerId != capturingPlayerId) {
  137. capture->capturingPlayerId = capturingPlayerId;
  138. capture->captureProgress = 0.0f;
  139. }
  140. capture->isBeingCaptured = true;
  141. capture->captureProgress += deltaTime;
  142. if (capture->captureProgress >= capture->requiredTime) {
  143. transferBarrackOwnership(world, barrack, capturingPlayerId);
  144. capture->captureProgress = 0.0f;
  145. capture->isBeingCaptured = false;
  146. capture->capturingPlayerId = -1;
  147. }
  148. } else {
  149. if (capture->isBeingCaptured) {
  150. capture->captureProgress -= deltaTime * 2.0f;
  151. if (capture->captureProgress <= 0.0f) {
  152. capture->captureProgress = 0.0f;
  153. capture->isBeingCaptured = false;
  154. capture->capturingPlayerId = -1;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. } // namespace Game::Systems