CCreatureAnimation.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * CCreatureAnimation.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/FunctionList.h"
  12. #include "../widgets/Images.h"
  13. #include "../gui/CAnimation.h"
  14. class CIntObject;
  15. class CCreatureAnimation;
  16. class CCanvas;
  17. /// Namespace for some common controls of animations
  18. namespace AnimationControls
  19. {
  20. /// get SDL_Color for creature selection borders
  21. SDL_Color getBlueBorder();
  22. SDL_Color getGoldBorder();
  23. SDL_Color getNoBorder();
  24. /// creates animation object with preset speed control
  25. std::shared_ptr<CCreatureAnimation> getAnimation(const CCreature * creature);
  26. /// returns animation speed of specific group, taking in mind game setting (in frames per second)
  27. float getCreatureAnimationSpeed(const CCreature * creature, const CCreatureAnimation * anim, size_t groupID);
  28. /// returns how far projectile should move each frame
  29. /// TODO: make it time-based
  30. float getProjectileSpeed();
  31. /// returns speed of catapult projectile
  32. float getCatapultSpeed();
  33. /// returns speed of any spell effects, including any special effects like morale (in frames per second)
  34. float getSpellEffectSpeed();
  35. /// returns duration of full movement animation, in seconds. Needed to move animation on screen
  36. float getMovementDuration(const CCreature * creature);
  37. /// Returns distance on which flying creatures should during one animation loop
  38. float getFlightDistance(const CCreature * creature);
  39. }
  40. /// Class which manages animations of creatures/units inside battles
  41. /// TODO: split into constant image container and class that does *control* of animation
  42. class CCreatureAnimation : public CIntObject
  43. {
  44. public:
  45. typedef std::function<float(CCreatureAnimation *, size_t)> TSpeedController;
  46. private:
  47. std::string name;
  48. std::shared_ptr<CAnimation> forward;
  49. std::shared_ptr<CAnimation> reverse;
  50. int fullWidth;
  51. int fullHeight;
  52. // speed of animation, measure in frames per second
  53. float speed;
  54. // currently displayed frame. Float to allow H3-style animations where frames
  55. // don't display for integer number of frames
  56. float currentFrame;
  57. // cumulative, real-time duration of animation. Used for effects like selection border
  58. float elapsedTime;
  59. CCreatureAnim::EAnimType type; //type of animation being displayed
  60. // border color, disabled if alpha = 0
  61. SDL_Color border;
  62. TSpeedController speedController;
  63. bool once; // animation will be played once and the reset to idling
  64. void endAnimation();
  65. void genBorderPalette(IImage::BorderPallete & target);
  66. public:
  67. // function(s) that will be called when animation ends, after reset to 1st frame
  68. // NOTE that these function will be fired only once
  69. CFunctionList<void()> onAnimationReset;
  70. int getWidth() const;
  71. int getHeight() const;
  72. /// Constructor
  73. /// name - path to .def file, relative to SPRITES/ directory
  74. /// controller - function that will return for how long *each* frame
  75. /// in specified group of animation should be played, measured in seconds
  76. CCreatureAnimation(const std::string & name_, TSpeedController speedController);
  77. void setType(CCreatureAnim::EAnimType type); //sets type of animation and cleares framecount
  78. CCreatureAnim::EAnimType getType() const; //returns type of animation
  79. void nextFrame(std::shared_ptr<CCanvas> canvas, bool facingRight);
  80. // should be called every frame, return true when animation was reset to beginning
  81. bool incrementFrame(float timePassed);
  82. void setBorderColor(SDL_Color palette);
  83. // tint color effect
  84. void shiftColor(const ColorShifter * shifter);
  85. float getCurrentFrame() const; // Gets the current frame ID relative to frame group.
  86. void playOnce(CCreatureAnim::EAnimType type); //plays once given stage of animation, then resets to 2
  87. int framesInGroup(CCreatureAnim::EAnimType group) const;
  88. void pause();
  89. void play();
  90. //helpers. TODO: move them somewhere else
  91. bool isDead() const;
  92. bool isDying() const;
  93. bool isDeadOrDying() const;
  94. bool isIdle() const;
  95. bool isMoving() const;
  96. bool isShooting() const;
  97. };