Movement.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * 2D Game Engine
  3. * Movement.h:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 11/02/2021.
  8. */
  9. #ifndef GAMEENGINE_MOVEMENT_H
  10. #define GAMEENGINE_MOVEMENT_H
  11. #include <ECS.h>
  12. #include <Logger.h>
  13. #include <Components/Transform.h>
  14. #include <Components/RigidBody.h>
  15. class MovementSystem: public System
  16. {
  17. public:
  18. MovementSystem()
  19. {
  20. this->requireComponent<TransformComponent>();
  21. this->requireComponent<RigidBodyComponent>();
  22. }
  23. void update(double deltaTime)
  24. {
  25. for (auto entity: this->getSystemEntities())
  26. {
  27. auto &transform = entity.getComponent<TransformComponent>();
  28. const auto rigidbody = entity.getComponent<RigidBodyComponent>();
  29. transform.position.x += rigidbody.velocity.x * deltaTime;
  30. transform.position.y += rigidbody.velocity.y * deltaTime;
  31. Logger::Debug("Entity id#%d - Position is now (%f, %f)",
  32. entity.getId(), transform.position.x, transform.position.y);
  33. }
  34. }
  35. };
  36. #endif /* GAMEENGINE_MOVEMENT_H */