movement_system.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <application/context.h>
  2. #include <component/position_component.h>
  3. #include <component/rect_component.h>
  4. #include <component/velocity_component.h>
  5. #include <entt/entity/registry.hpp>
  6. #include <system/movement_system.h>
  7. namespace testbed {
  8. void movement_system(entt::registry &registry, const context &ctx, const double delta) {
  9. int width{};
  10. int height{};
  11. for(auto [entity, position, velocity, rect]: registry.view<position_component, velocity_component, rect_component>().each()) {
  12. position.x += (velocity.dx * delta) * (ctx.logical_width() / 10.);
  13. position.y += velocity.dy * delta * (ctx.logical_height() / 10.);
  14. if(position.x < 0) {
  15. position.x = 0.;
  16. velocity.dx = 1.;
  17. } else if(auto pos = position.x + rect.w; pos > ctx.logical_width()) {
  18. position.x = ctx.logical_width() - rect.w;
  19. velocity.dx = -1;
  20. }
  21. if(position.y < 0) {
  22. position.y = 0.;
  23. velocity.dy = 1.;
  24. } else if(auto pos = position.y + rect.h; pos > ctx.logical_height()) {
  25. position.y = ctx.logical_height() - rect.h;
  26. velocity.dy = -1;
  27. }
  28. }
  29. }
  30. } // namespace testbed