anim.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "anim.h"
  2. //Constructors
  3. Animation::Animation(unsigned char duration)
  4. {
  5. // Initialize at given duration
  6. max = duration;
  7. counter = 0;
  8. /* Positive being 0 means the animation is at a "standby" phase and
  9. * can't run yet
  10. */
  11. positive = 0;
  12. }
  13. Animation::Animation()
  14. {
  15. // Initialize at 0
  16. max = 0;
  17. counter = 0;
  18. positive = 0;
  19. }
  20. // Update
  21. bool Animation::update()
  22. {
  23. //Positive animation
  24. if (positive)
  25. {
  26. //Counter is below max
  27. if (counter < max)
  28. {
  29. //Advance animation and return updated
  30. counter++;
  31. return 1;
  32. }
  33. }
  34. //Negative animation
  35. else
  36. {
  37. //Counter is above zero
  38. if (counter > 0)
  39. {
  40. //Advance animation (negatively) and return updated
  41. counter--;
  42. return 1;
  43. }
  44. }
  45. //Animation is not updated
  46. return 0;
  47. }
  48. //Actions
  49. void Animation::startFromStart() {counter = 0 ; positive = 1;}
  50. void Animation::startFromEnd () {counter = max; positive = 0;}
  51. void Animation::startFromStart(unsigned char gMax) {max = gMax; counter = 0 ; positive = 1;}
  52. void Animation::startFromEnd (unsigned char gMax) {max = gMax; counter = max; positive = 0;}
  53. //Isers
  54. bool Animation::isAtStart() {return counter == 0 && !positive;}
  55. bool Animation::isAtEnd () {return counter == max && positive;}
  56. bool Animation::isRunning()
  57. {
  58. return (counter < max && positive)
  59. || (counter > 0 && !positive);
  60. }
  61. //Interpolation
  62. float Animation::interpolate(float amount) {return amount *counter /max;}