selection_utils.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #pragma once
  2. #include "game/core/component.h"
  3. #include "game/core/entity.h"
  4. #include "game/core/world.h"
  5. #include "game/systems/selection_system.h"
  6. #include <algorithm>
  7. #include <vector>
  8. namespace App::Utils {
  9. inline void
  10. sanitizeSelection(Engine::Core::World *world,
  11. Game::Systems::SelectionSystem *selection_system) {
  12. if ((world == nullptr) || (selection_system == nullptr)) {
  13. return;
  14. }
  15. const auto &sel = selection_system->getSelectedUnits();
  16. std::vector<Engine::Core::EntityID> toKeep;
  17. toKeep.reserve(sel.size());
  18. for (auto id : sel) {
  19. if (auto *e = world->getEntity(id)) {
  20. if (auto *u = e->getComponent<Engine::Core::UnitComponent>()) {
  21. if (u->health > 0) {
  22. toKeep.push_back(id);
  23. }
  24. }
  25. }
  26. }
  27. if (toKeep.size() != sel.size() ||
  28. !std::equal(toKeep.begin(), toKeep.end(), sel.begin())) {
  29. selection_system->clearSelection();
  30. for (auto id : toKeep) {
  31. selection_system->selectUnit(id);
  32. }
  33. }
  34. }
  35. } // namespace App::Utils