CBattleAnimations.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * CBattleAnimations.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/battle/BattleHex.h"
  12. #include "../widgets/Images.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CStack;
  15. VCMI_LIB_NAMESPACE_END
  16. class CBattleInterface;
  17. class CCreatureAnimation;
  18. class CBattleAnimation;
  19. struct CatapultProjectileInfo;
  20. struct StackAttackedInfo;
  21. /// Base class of battle animations
  22. class CBattleAnimation
  23. {
  24. protected:
  25. CBattleInterface * owner;
  26. std::list<std::pair<CBattleAnimation *, bool>> & pendingAnimations();
  27. std::shared_ptr<CCreatureAnimation> stackAnimation(const CStack * stack);
  28. bool stackFacingRight(const CStack * stack);
  29. void setStackFacingRight(const CStack * stack, bool facingRight);
  30. ui32 maxAnimationID();
  31. public:
  32. virtual bool init() = 0; //to be called - if returned false, call again until returns true
  33. virtual void nextFrame() {} //call every new frame
  34. virtual void endAnim(); //to be called mostly internally; in this class it removes animation from pendingAnims list
  35. virtual ~CBattleAnimation();
  36. bool isEarliest(bool perStackConcurrency); //determines if this animation is earliest of all
  37. ui32 ID; //unique identifier
  38. CBattleAnimation(CBattleInterface * _owner);
  39. };
  40. /// Sub-class which is responsible for managing the battle stack animation.
  41. class CBattleStackAnimation : public CBattleAnimation
  42. {
  43. public:
  44. std::shared_ptr<CCreatureAnimation> myAnim; //animation for our stack, managed by CBattleInterface
  45. const CStack * stack; //id of stack whose animation it is
  46. CBattleStackAnimation(CBattleInterface * _owner, const CStack * _stack);
  47. void shiftColor(const ColorShifter * shifter);
  48. void rotateStack(BattleHex hex);
  49. };
  50. /// This class is responsible for managing the battle attack animation
  51. class CAttackAnimation : public CBattleStackAnimation
  52. {
  53. bool soundPlayed;
  54. protected:
  55. BattleHex dest; //attacked hex
  56. bool shooting;
  57. CCreatureAnim::EAnimType group; //if shooting is true, print this animation group
  58. const CStack *attackedStack;
  59. const CStack *attackingStack;
  60. int attackingStackPosBeforeReturn; //for stacks with return_after_strike feature
  61. public:
  62. void nextFrame() override;
  63. void endAnim() override;
  64. bool checkInitialConditions();
  65. CAttackAnimation(CBattleInterface *_owner, const CStack *attacker, BattleHex _dest, const CStack *defender);
  66. };
  67. /// Animation of a defending unit
  68. class CDefenceAnimation : public CBattleStackAnimation
  69. {
  70. CCreatureAnim::EAnimType getMyAnimType();
  71. std::string getMySound();
  72. void startAnimation();
  73. const CStack * attacker; //attacking stack
  74. bool rangedAttack; //if true, stack has been attacked by shooting
  75. bool killed; //if true, stack has been killed
  76. float timeToWait; // for how long this animation should be paused
  77. public:
  78. bool init() override;
  79. void nextFrame() override;
  80. void endAnim() override;
  81. CDefenceAnimation(StackAttackedInfo _attackedInfo, CBattleInterface * _owner);
  82. virtual ~CDefenceAnimation(){};
  83. };
  84. class CDummyAnimation : public CBattleAnimation
  85. {
  86. private:
  87. int counter;
  88. int howMany;
  89. public:
  90. bool init() override;
  91. void nextFrame() override;
  92. void endAnim() override;
  93. CDummyAnimation(CBattleInterface * _owner, int howManyFrames);
  94. virtual ~CDummyAnimation(){}
  95. };
  96. /// Hand-to-hand attack
  97. class CMeleeAttackAnimation : public CAttackAnimation
  98. {
  99. public:
  100. bool init() override;
  101. void endAnim() override;
  102. CMeleeAttackAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked);
  103. virtual ~CMeleeAttackAnimation(){};
  104. };
  105. /// Move animation of a creature
  106. class CMovementAnimation : public CBattleStackAnimation
  107. {
  108. private:
  109. std::vector<BattleHex> destTiles; //full path, includes already passed hexes
  110. ui32 curentMoveIndex; // index of nextHex in destTiles
  111. BattleHex oldPos; //position of stack before move
  112. double begX, begY; // starting position
  113. double distanceX, distanceY; // full movement distance, may be negative if creture moves topleft
  114. double timeToMove; // full length of movement animation
  115. double progress; // range 0 -> 1, indicates move progrees. 0 = movement starts, 1 = move ends
  116. public:
  117. BattleHex nextHex; // next hex, to which creature move right now
  118. bool init() override;
  119. void nextFrame() override;
  120. void endAnim() override;
  121. CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance);
  122. virtual ~CMovementAnimation(){};
  123. };
  124. /// Move end animation of a creature
  125. class CMovementEndAnimation : public CBattleStackAnimation
  126. {
  127. private:
  128. BattleHex destinationTile;
  129. public:
  130. bool init() override;
  131. void endAnim() override;
  132. CMovementEndAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex destTile);
  133. virtual ~CMovementEndAnimation(){};
  134. };
  135. /// Move start animation of a creature
  136. class CMovementStartAnimation : public CBattleStackAnimation
  137. {
  138. public:
  139. bool init() override;
  140. void endAnim() override;
  141. CMovementStartAnimation(CBattleInterface * _owner, const CStack * _stack);
  142. virtual ~CMovementStartAnimation(){};
  143. };
  144. /// Class responsible for animation of stack chaning direction (left <-> right)
  145. class CReverseAnimation : public CBattleStackAnimation
  146. {
  147. BattleHex hex;
  148. public:
  149. bool priority; //true - high, false - low
  150. bool init() override;
  151. void setupSecondPart();
  152. void endAnim() override;
  153. CReverseAnimation(CBattleInterface * _owner, const CStack * stack, BattleHex dest, bool _priority);
  154. virtual ~CReverseAnimation(){};
  155. };
  156. class CRangedAttackAnimation : public CAttackAnimation
  157. {
  158. public:
  159. CRangedAttackAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender);
  160. protected:
  161. };
  162. /// Shooting attack
  163. class CShootingAnimation : public CRangedAttackAnimation
  164. {
  165. private:
  166. int catapultDamage;
  167. public:
  168. bool init() override;
  169. void nextFrame() override;
  170. void endAnim() override;
  171. //last two params only for catapult attacks
  172. CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest,
  173. const CStack * _attacked, bool _catapult = false, int _catapultDmg = 0);
  174. virtual ~CShootingAnimation(){};
  175. };
  176. class CCastAnimation : public CRangedAttackAnimation
  177. {
  178. public:
  179. CCastAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender);
  180. bool init() override;
  181. void nextFrame() override;
  182. void endAnim() override;
  183. };
  184. /// This class manages effect animation
  185. class CEffectAnimation : public CBattleAnimation
  186. {
  187. private:
  188. BattleHex destTile;
  189. std::shared_ptr<CAnimation> customAnim;
  190. int x, y, dx, dy;
  191. bool Vflip;
  192. bool alignToBottom;
  193. public:
  194. bool init() override;
  195. void nextFrame() override;
  196. void endAnim() override;
  197. CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx = 0, int _dy = 0, bool _Vflip = false, bool _alignToBottom = false);
  198. CEffectAnimation(CBattleInterface * _owner, std::shared_ptr<CAnimation> _customAnim, int _x, int _y, int _dx = 0, int _dy = 0);
  199. CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, BattleHex _destTile, bool _Vflip = false, bool _alignToBottom = false);
  200. virtual ~CEffectAnimation(){};
  201. };