RType
Animation.cpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** RType
4 ** File description:
5 ** Animation.cpp
6 */
7 
8 #include "Animation.hpp"
9 #include "Animatable.hpp"
10 
11 /**
12  * @brief Construct a new Animation:: Animation object
13  * @param event
14  * @param component
15  */
16 void Animation::update(std::shared_ptr<Event> event, std::shared_ptr<IComponentRType> component)
17 {
18  auto animatable = std::dynamic_pointer_cast<Animatable>(component);
19  if (animatable) {
20  gettimeofday(&animatable->_now, NULL);
21  timersub(&animatable->_now, &animatable->_chrono, &animatable->_diff);
22  if (animatable->_diff.tv_sec >= animatable->getTime().tv_sec &&
23  animatable->_diff.tv_usec >= animatable->getTime().tv_usec) {
24  auto spriteAnim = animatable->getTarget();
25  int numberFrameToAnim = animatable->_numberFrameToAnim - 1;
26  std::tuple<float, float> size = spriteAnim->getSize();
27  if (animatable->_frameIndex >= numberFrameToAnim + animatable->_startFrameIndex) {
28  animatable->_frameIndex = animatable->_startFrameIndex;
29  } else {
30  animatable->_frameIndex++;
31  }
32  int frameWidth1 =
33  std::get<0>(size) / spriteAnim->getScale() / animatable->_numberFrame * animatable->_frameIndex;
34  int frameWidth2 =
35  std::get<0>(size) / spriteAnim->getScale() / animatable->_numberFrame * animatable->_frameForOnePicture;
36  int frameHeight = std::get<1>(size) / spriteAnim->getScale();
37  spriteAnim->setRect(std::make_tuple(frameWidth1, 0, frameWidth2, frameHeight));
38  animatable->_chrono = animatable->_now;
39  }
40  }
41 }