camera_follow_system.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "camera_follow_system.h"
  2. #include "../../render/gl/camera.h"
  3. #include "../core/component.h"
  4. #include "../core/world.h"
  5. #include "selection_system.h"
  6. #include <qvectornd.h>
  7. namespace Game::Systems {
  8. void CameraFollowSystem::update(Engine::Core::World &world,
  9. SelectionSystem &selection,
  10. Render::GL::Camera &camera) {
  11. const auto &sel = selection.get_selected_units();
  12. if (sel.empty()) {
  13. return;
  14. }
  15. QVector3D sum(0, 0, 0);
  16. int count = 0;
  17. for (auto id : sel) {
  18. if (auto *e = world.get_entity(id)) {
  19. if (auto *t = e->get_component<Engine::Core::TransformComponent>()) {
  20. sum += QVector3D(t->position.x, t->position.y, t->position.z);
  21. ++count;
  22. }
  23. }
  24. }
  25. if (count > 0) {
  26. QVector3D const center = sum / float(count);
  27. camera.update_follow(center);
  28. }
  29. }
  30. void CameraFollowSystem::snap_to_selection(Engine::Core::World &world,
  31. SelectionSystem &selection,
  32. Render::GL::Camera &camera) {
  33. const auto &sel = selection.get_selected_units();
  34. if (sel.empty()) {
  35. return;
  36. }
  37. QVector3D sum(0, 0, 0);
  38. int count = 0;
  39. for (auto id : sel) {
  40. if (auto *e = world.get_entity(id)) {
  41. if (auto *t = e->get_component<Engine::Core::TransformComponent>()) {
  42. sum += QVector3D(t->position.x, t->position.y, t->position.z);
  43. ++count;
  44. }
  45. }
  46. }
  47. if (count > 0) {
  48. QVector3D const target = sum / float(count);
  49. camera.set_target(target);
  50. camera.capture_follow_offset();
  51. }
  52. }
  53. } // namespace Game::Systems