Browse Source

Add error feedback when troop limit is reached

Co-authored-by: djeada <[email protected]>
copilot-swe-agent[bot] 2 months ago
parent
commit
bc1e664909

+ 5 - 1
app/controllers/command_controller.cpp

@@ -199,8 +199,12 @@ void CommandController::recruitNearSelected(const QString &unitType,
   if (sel.empty())
     return;
 
-  Game::Systems::ProductionService::startProductionForFirstSelectedBarracks(
+  auto result = Game::Systems::ProductionService::startProductionForFirstSelectedBarracks(
       *m_world, sel, localOwnerId, unitType.toStdString());
+  
+  if (result == Game::Systems::ProductionResult::GlobalTroopLimitReached) {
+    emit troopLimitReached();
+  }
 }
 
 void CommandController::resetMovement(Engine::Core::Entity *entity) {

+ 1 - 0
app/controllers/command_controller.h

@@ -49,6 +49,7 @@ public:
 
 signals:
   void attackTargetSelected();
+  void troopLimitReached();
 
 private:
   Engine::Core::World *m_world;

+ 6 - 0
app/game_engine.cpp

@@ -142,6 +142,12 @@ GameEngine::GameEngine() {
               }
             }
           });
+  
+  connect(m_commandController.get(),
+          &App::Controllers::CommandController::troopLimitReached,
+          [this]() {
+            setError("Maximum troop limit reached. Cannot produce more units.");
+          });
 
   connect(this, SIGNAL(selectedUnitsChanged()), m_selectedUnitsModel,
           SLOT(refresh()));

+ 7 - 7
game/systems/production_service.cpp

@@ -22,32 +22,32 @@ findFirstSelectedBarracks(Engine::Core::World &world,
   return nullptr;
 }
 
-bool ProductionService::startProductionForFirstSelectedBarracks(
+ProductionResult ProductionService::startProductionForFirstSelectedBarracks(
     Engine::Core::World &world,
     const std::vector<Engine::Core::EntityID> &selected, int ownerId,
     const std::string &unitType) {
   auto *e = findFirstSelectedBarracks(world, selected, ownerId);
   if (!e)
-    return false;
+    return ProductionResult::NoBarracks;
   auto *p = e->getComponent<Engine::Core::ProductionComponent>();
   if (!p)
     p = e->addComponent<Engine::Core::ProductionComponent>();
   if (!p)
-    return false;
+    return ProductionResult::NoBarracks;
   if (p->producedCount >= p->maxUnits)
-    return false;
+    return ProductionResult::PerBarracksLimitReached;
   if (p->inProgress)
-    return false;
+    return ProductionResult::AlreadyInProgress;
   
   int currentTroops = world.countTroopsForPlayer(ownerId);
   int maxTroops = Game::GameConfig::instance().getMaxTroopsPerPlayer();
   if (currentTroops >= maxTroops)
-    return false;
+    return ProductionResult::GlobalTroopLimitReached;
   
   p->productType = unitType;
   p->timeRemaining = p->buildTime;
   p->inProgress = true;
-  return true;
+  return ProductionResult::Success;
 }
 
 bool ProductionService::setRallyForFirstSelectedBarracks(

+ 9 - 1
game/systems/production_service.h

@@ -13,6 +13,14 @@ using EntityID = unsigned int;
 namespace Game {
 namespace Systems {
 
+enum class ProductionResult {
+  Success,
+  NoBarracks,
+  PerBarracksLimitReached,
+  GlobalTroopLimitReached,
+  AlreadyInProgress
+};
+
 struct ProductionState {
   bool hasBarracks = false;
   bool inProgress = false;
@@ -25,7 +33,7 @@ struct ProductionState {
 
 class ProductionService {
 public:
-  static bool startProductionForFirstSelectedBarracks(
+  static ProductionResult startProductionForFirstSelectedBarracks(
       Engine::Core::World &world,
       const std::vector<Engine::Core::EntityID> &selected, int ownerId,
       const std::string &unitType);