CreatureAnimation.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 CreatureAnimation;
  16. class Canvas;
  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<CreatureAnimation> 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 CreatureAnimation * anim, ECreatureAnimType groupID);
  28. /// returns how far projectile should move per second
  29. float getProjectileSpeed();
  30. /// returns speed of catapult projectile, in pixels per second (horizontal axis only)
  31. float getCatapultSpeed();
  32. /// returns speed of any spell effects, including any special effects like morale (in frames per second)
  33. float getSpellEffectSpeed();
  34. /// returns duration of full movement animation, in seconds. Needed to move animation on screen
  35. float getMovementDuration(const CCreature * creature);
  36. /// Returns distance on which flying creatures should during one animation loop
  37. float getFlightDistance(const CCreature * creature);
  38. /// Returns total time for full fade-in effect on newly summoned creatures, in seconds
  39. float getFadeInDuration();
  40. /// Returns animation speed for obstacles, in frames per second
  41. float getObstaclesSpeed();
  42. }
  43. /// Class which manages animations of creatures/units inside battles
  44. /// TODO: split into constant image container and class that does *control* of animation
  45. class CreatureAnimation : public CIntObject
  46. {
  47. public:
  48. typedef std::function<float(CreatureAnimation *, ECreatureAnimType)> TSpeedController;
  49. private:
  50. std::string name;
  51. /// animation for rendering stack in default orientation - facing right
  52. std::shared_ptr<CAnimation> forward;
  53. /// animation that has all its frames flipped for rendering stack facing left
  54. std::shared_ptr<CAnimation> reverse;
  55. int fullWidth;
  56. int fullHeight;
  57. /// speed of animation, measure in frames per second
  58. float speed;
  59. /// currently displayed frame. Float to allow H3-style animations where frames
  60. /// don't display for integer number of frames
  61. float currentFrame;
  62. /// cumulative, real-time duration of animation. Used for effects like selection border
  63. float elapsedTime;
  64. ///type of animation being displayed
  65. ECreatureAnimType type;
  66. /// current value of shadow transparency
  67. uint8_t shadowAlpha;
  68. /// border color, disabled if alpha = 0
  69. SDL_Color border;
  70. TSpeedController speedController;
  71. /// animation will be played once and the reset to idling
  72. bool once;
  73. void endAnimation();
  74. void genSpecialPalette(IImage::SpecialPalette & target);
  75. public:
  76. /// function(s) that will be called when animation ends, after reset to 1st frame
  77. /// NOTE that these functions will be fired only once
  78. CFunctionList<void()> onAnimationReset;
  79. int getWidth() const;
  80. int getHeight() const;
  81. /// Constructor
  82. /// name - path to .def file, relative to SPRITES/ directory
  83. /// controller - function that will return for how long *each* frame
  84. /// in specified group of animation should be played, measured in seconds
  85. CreatureAnimation(const std::string & name_, TSpeedController speedController);
  86. /// sets type of animation and resets framecount
  87. void setType(ECreatureAnimType type);
  88. /// returns currently rendered type of animation
  89. ECreatureAnimType getType() const;
  90. void nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight);
  91. /// should be called every frame, return true when animation was reset to beginning
  92. bool incrementFrame(float timePassed);
  93. void setBorderColor(SDL_Color palette);
  94. /// Gets the current frame ID within current group.
  95. float getCurrentFrame() const;
  96. /// plays once given type of animation, then resets to idle
  97. void playOnce(ECreatureAnimType type);
  98. /// returns number of frames in selected animation type
  99. int framesInGroup(ECreatureAnimType group) const;
  100. void pause();
  101. void play();
  102. /// helpers to classify current type of animation
  103. bool isDead() const;
  104. bool isDying() const;
  105. bool isDeadOrDying() const;
  106. bool isIdle() const;
  107. bool isMoving() const;
  108. bool isShooting() const;
  109. };