Animation.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * 2D Game Engine
  3. * Animation.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 15/02/2021.
  8. */
  9. #ifndef GAMEENGINE_ANIMATIONSYS_H
  10. #define GAMEENGINE_ANIMATIONSYS_H
  11. #include <ECS.h>
  12. #include <Components/Animation.h>
  13. #include <Components/Sprite.h>
  14. class AnimationSystem: public System
  15. {
  16. public:
  17. AnimationSystem()
  18. {
  19. this->requireComponent<AnimationComponent>();
  20. this->requireComponent<SpriteComponent>();
  21. }
  22. void update()
  23. {
  24. for(auto entity: this->getSystemEntities())
  25. {
  26. auto &animation = entity.getComponent<AnimationComponent>();
  27. auto &sprite = entity.getComponent<SpriteComponent>();
  28. animation.currentFrame = ((SDL_GetTicks() - animation.startTime) * animation.frameSpeedRate / 1000) % animation.numFrames;
  29. sprite.sourceRect.x = animation.currentFrame * sprite.width;
  30. }
  31. }
  32. };
  33. #endif /* GAMEENGINE_ANIMATIONSYS_H */