CBattleAnimations.h 7.8 KB

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