CreatureAnimation.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "../../lib/Color.h"
  13. #include "../widgets/Images.h"
  14. #include "../render/CAnimation.h"
  15. #include "../render/IImage.h"
  16. class CIntObject;
  17. class CreatureAnimation;
  18. class Canvas;
  19. /// Namespace for some common controls of animations
  20. namespace AnimationControls
  21. {
  22. /// get color for creature selection borders
  23. ColorRGBA getBlueBorder();
  24. ColorRGBA getGoldBorder();
  25. ColorRGBA getNoBorder();
  26. /// returns animation speed factor according to game settings,
  27. /// slow speed is considered to be "base speed" and will return 1.0
  28. float getAnimationSpeedFactor();
  29. /// creates animation object with preset speed control
  30. std::shared_ptr<CreatureAnimation> getAnimation(const CCreature * creature);
  31. /// returns animation speed of specific group, taking in mind game setting (in frames per second)
  32. float getCreatureAnimationSpeed(const CCreature * creature, const CreatureAnimation * anim, ECreatureAnimType groupID);
  33. /// returns how far projectile should move per second, in pixels per second
  34. float getProjectileSpeed();
  35. /// returns how far projectile should move per second, in pixels per second
  36. float getRayProjectileSpeed();
  37. /// returns speed of catapult projectile, in pixels per second, on a straight line, without parabola correction
  38. float getCatapultSpeed();
  39. /// returns speed of any spell effects, including any special effects like morale (in frames per second)
  40. float getSpellEffectSpeed();
  41. /// returns speed of movement animation across the screen, in tiles per second
  42. float getMovementDistance(const CCreature * creature);
  43. /// returns speed of movement animation across the screen, in pixels per seconds
  44. float getFlightDistance(const CCreature * creature);
  45. /// Returns total time for full fade-in effect on newly summoned creatures, in seconds
  46. float getFadeInDuration();
  47. /// Returns animation speed for obstacles, in frames per second
  48. float getObstaclesSpeed();
  49. }
  50. /// Class which manages animations of creatures/units inside battles
  51. /// TODO: split into constant image container and class that does *control* of animation
  52. class CreatureAnimation : public CIntObject
  53. {
  54. public:
  55. using TSpeedController = std::function<float(CreatureAnimation *, ECreatureAnimType)>;
  56. private:
  57. AnimationPath name;
  58. /// animation for rendering stack in default orientation - facing right
  59. std::shared_ptr<CAnimation> forward;
  60. /// animation that has all its frames flipped for rendering stack facing left
  61. std::shared_ptr<CAnimation> reverse;
  62. int fullWidth;
  63. int fullHeight;
  64. /// speed of animation, measure in frames per second
  65. float speed;
  66. /// currently displayed frame. Float to allow H3-style animations where frames
  67. /// don't display for integer number of frames
  68. float currentFrame;
  69. float animationEnd;
  70. /// cumulative, real-time duration of animation. Used for effects like selection border
  71. float elapsedTime;
  72. ///type of animation being displayed
  73. ECreatureAnimType type;
  74. /// current value of shadow transparency
  75. uint8_t shadowAlpha;
  76. /// border color, disabled if alpha = 0
  77. ColorRGBA border;
  78. TSpeedController speedController;
  79. /// animation will be played once and the reset to idling
  80. bool once;
  81. void endAnimation();
  82. void genSpecialPalette(IImage::SpecialPalette & target);
  83. public:
  84. /// function(s) that will be called when animation ends, after reset to 1st frame
  85. /// NOTE that these functions will be fired only once
  86. CFunctionList<void()> onAnimationReset;
  87. int getWidth() const;
  88. int getHeight() const;
  89. /// Constructor
  90. /// name - path to .def file, relative to SPRITES/ directory
  91. /// controller - function that will return for how long *each* frame
  92. /// in specified group of animation should be played, measured in seconds
  93. CreatureAnimation(const AnimationPath & name_, TSpeedController speedController);
  94. /// sets type of animation and resets framecount
  95. void setType(ECreatureAnimType type);
  96. /// returns currently rendered type of animation
  97. ECreatureAnimType getType() const;
  98. void nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight);
  99. /// should be called every frame, return true when animation was reset to beginning
  100. bool incrementFrame(float timePassed);
  101. void setBorderColor(ColorRGBA palette);
  102. /// Gets the current frame ID within current group.
  103. float getCurrentFrame() const;
  104. /// plays once given type of animation, then resets to idle
  105. void playOnce(ECreatureAnimType type);
  106. /// returns number of frames in selected animation type
  107. int framesInGroup(ECreatureAnimType group) const;
  108. void playUntil(size_t frameIndex);
  109. /// helpers to classify current type of animation
  110. bool isDead() const;
  111. bool isDying() const;
  112. bool isDeadOrDying() const;
  113. bool isIdle() const;
  114. bool isMoving() const;
  115. bool isShooting() const;
  116. };