/src/Animation.cpp

http://github.com/clintbellanger/flare · C++ · 109 lines · 65 code · 16 blank · 28 comment · 25 complexity · 88b4142e24eaef04b2729f163d29871e MD5 · raw file

  1. /*
  2. Copyright 2011 kitano
  3. This file is part of FLARE.
  4. FLARE is free software: you can redistribute it and/or modify it under the terms
  5. of the GNU General Public License as published by the Free Software Foundation,
  6. either version 3 of the License, or (at your option) any later version.
  7. FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  9. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. FLARE. If not, see http://www.gnu.org/licenses/
  12. */
  13. /**
  14. * class Animation
  15. *
  16. * The Animation class handles the logic of advancing frames based on the animation type
  17. * and returning a renderable frame.
  18. *
  19. * The intention with the class is to keep it as flexible as possible so that the animations
  20. * can be used not only for character animations but any animated in-game objects.
  21. */
  22. #include "Animation.h"
  23. Animation::Animation(std::string _name, Point _render_size, Point _render_offset, int _position, int _frames, int _duration, std::string _type)
  24. : name(_name), sprites(NULL),
  25. render_size(_render_size), render_offset(_render_offset),
  26. position(_position), frames(_frames), duration(_duration), type(_type),
  27. cur_frame(0), disp_frame(0), mid_frame(0), max_frame(0), timesPlayed(0) {
  28. if (type == "play_once" || type == "looped") {
  29. max_frame = frames * duration;
  30. }
  31. else if (type == "back_forth") {
  32. mid_frame = frames * duration;
  33. max_frame = mid_frame + mid_frame;
  34. }
  35. }
  36. void Animation::advanceFrame() {
  37. if (type == "play_once") {
  38. if (cur_frame < max_frame - 1) {
  39. cur_frame++;
  40. }
  41. else {
  42. timesPlayed = 1;
  43. }
  44. disp_frame = (cur_frame / duration) + position;
  45. }
  46. else if (type == "looped") {
  47. cur_frame++;
  48. if (cur_frame == max_frame) {
  49. cur_frame = 0;
  50. //animation has completed one loop
  51. timesPlayed++;
  52. }
  53. disp_frame = (cur_frame / duration) + position;
  54. }
  55. else if (type == "back_forth") {
  56. cur_frame++;
  57. if (cur_frame == max_frame) {
  58. cur_frame = 0;
  59. //animation has completed one loop
  60. timesPlayed++;
  61. }
  62. if (cur_frame >= mid_frame) {
  63. disp_frame = (max_frame -1 - cur_frame) / duration + position;
  64. }
  65. else {
  66. disp_frame = cur_frame / duration + position;
  67. }
  68. }
  69. }
  70. Renderable Animation::getCurrentFrame(int direction) {
  71. Renderable r;
  72. // if the animation contains the spritesheet
  73. if (sprites != NULL) {
  74. r.sprite = sprites;
  75. }
  76. r.src.x = render_size.x * disp_frame;
  77. r.src.y = render_size.y * direction;
  78. r.src.w = render_size.x;
  79. r.src.h = render_size.y;
  80. r.offset.x = render_offset.x;
  81. r.offset.y = render_offset.y; // 112
  82. r.object_layer = true;
  83. return r;
  84. }
  85. void Animation::reset() {
  86. cur_frame = 0;
  87. disp_frame = (cur_frame / duration) + position;
  88. timesPlayed = 0;
  89. }