arrow_system.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "arrow_system.h"
  2. #include "../../render/geom/arrow.h"
  3. #include "../../render/scene_renderer.h"
  4. #include <algorithm>
  5. #include <qvectornd.h>
  6. namespace Game::Systems {
  7. ArrowSystem::ArrowSystem() : m_config(GameConfig::instance().arrow()) {}
  8. void ArrowSystem::spawnArrow(const QVector3D &start, const QVector3D &end,
  9. const QVector3D &color, float speed) {
  10. ArrowInstance a;
  11. a.start = start;
  12. a.end = end;
  13. a.color = color;
  14. a.t = 0.0F;
  15. a.speed = speed;
  16. a.active = true;
  17. QVector3D const delta = end - start;
  18. float const dist = delta.length();
  19. a.arcHeight = std::clamp(m_config.arcHeightMultiplier * dist,
  20. m_config.arcHeightMin, m_config.arcHeightMax);
  21. a.invDist = (dist > 0.001F) ? (1.0F / dist) : 1.0F;
  22. m_arrows.push_back(a);
  23. }
  24. void ArrowSystem::update(Engine::Core::World *, float deltaTime) {
  25. for (auto &arrow : m_arrows) {
  26. if (!arrow.active) {
  27. continue;
  28. }
  29. arrow.t += deltaTime * arrow.speed * arrow.invDist;
  30. if (arrow.t >= 1.0F) {
  31. arrow.t = 1.0F;
  32. arrow.active = false;
  33. }
  34. }
  35. m_arrows.erase(
  36. std::remove_if(m_arrows.begin(), m_arrows.end(),
  37. [](const ArrowInstance &a) { return !a.active; }),
  38. m_arrows.end());
  39. }
  40. } // namespace Game::Systems