CBattleAnimations.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. bool shouldRotate();
  101. std::vector<BattleHex> destTiles; //full path, includes already passed hexes
  102. ui32 curentMoveIndex; // index of nextHex in destTiles
  103. BattleHex oldPos; //position of stack before move
  104. double begX, begY; // starting position
  105. double distanceX, distanceY; // full movement distance, may be negative if creture moves topleft
  106. double timeToMove; // full length of movement animation
  107. double progress; // range 0 -> 1, indicates move progrees. 0 = movement starts, 1 = move ends
  108. public:
  109. BattleHex nextHex; // next hex, to which creature move right now
  110. bool init() override;
  111. void nextFrame() override;
  112. void endAnim() override;
  113. CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance);
  114. virtual ~CMovementAnimation(){};
  115. };
  116. /// Move end animation of a creature
  117. class CMovementEndAnimation : public CBattleStackAnimation
  118. {
  119. private:
  120. BattleHex destinationTile;
  121. public:
  122. bool init() override;
  123. void endAnim() override;
  124. CMovementEndAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex destTile);
  125. virtual ~CMovementEndAnimation(){};
  126. };
  127. /// Move start animation of a creature
  128. class CMovementStartAnimation : public CBattleStackAnimation
  129. {
  130. public:
  131. bool init() override;
  132. void endAnim() override;
  133. CMovementStartAnimation(CBattleInterface * _owner, const CStack * _stack);
  134. virtual ~CMovementStartAnimation(){};
  135. };
  136. /// Class responsible for animation of stack chaning direction (left <-> right)
  137. class CReverseAnimation : public CBattleStackAnimation
  138. {
  139. BattleHex hex;
  140. public:
  141. bool priority; //true - high, false - low
  142. bool init() override;
  143. static void rotateStack(CBattleInterface * owner, const CStack * stack, BattleHex hex);
  144. void setupSecondPart();
  145. void endAnim() override;
  146. CReverseAnimation(CBattleInterface * _owner, const CStack * stack, BattleHex dest, bool _priority);
  147. virtual ~CReverseAnimation(){};
  148. };
  149. /// Small struct which contains information about the position and the velocity of a projectile
  150. struct ProjectileInfo
  151. {
  152. double x, y; //position on the screen
  153. double dx, dy; //change in position in one step
  154. int step, lastStep; //to know when finish showing this projectile
  155. int creID; //ID of creature that shot this projectile
  156. int stackID; //ID of stack
  157. int frameNum; //frame to display form projectile animation
  158. //bool spin; //if true, frameNum will be increased
  159. int animStartDelay; //frame of shooter animation when projectile should appear
  160. bool shotDone; // actual shot already done, projectile is flying
  161. bool reverse; //if true, projectile will be flipped by vertical asix
  162. std::shared_ptr<CatapultProjectileInfo> catapultInfo; // holds info about the parabolic trajectory of the cannon
  163. };
  164. class CRangedAttackAnimation : public CAttackAnimation
  165. {
  166. public:
  167. CRangedAttackAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender);
  168. protected:
  169. };
  170. /// Shooting attack
  171. class CShootingAnimation : public CRangedAttackAnimation
  172. {
  173. private:
  174. int catapultDamage;
  175. public:
  176. bool init() override;
  177. void nextFrame() override;
  178. void endAnim() override;
  179. //last two params only for catapult attacks
  180. CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest,
  181. const CStack * _attacked, bool _catapult = false, int _catapultDmg = 0);
  182. virtual ~CShootingAnimation(){};
  183. };
  184. class CCastAnimation : public CRangedAttackAnimation
  185. {
  186. public:
  187. CCastAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender);
  188. bool init() override;
  189. void nextFrame() override;
  190. void endAnim() override;
  191. };
  192. /// This class manages effect animation
  193. class CEffectAnimation : public CBattleAnimation
  194. {
  195. private:
  196. BattleHex destTile;
  197. std::shared_ptr<CAnimation> customAnim;
  198. int x, y, dx, dy;
  199. bool Vflip;
  200. bool alignToBottom;
  201. public:
  202. bool init() override;
  203. void nextFrame() override;
  204. void endAnim() override;
  205. CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx = 0, int _dy = 0, bool _Vflip = false, bool _alignToBottom = false);
  206. CEffectAnimation(CBattleInterface * _owner, std::shared_ptr<CAnimation> _customAnim, int _x, int _y, int _dx = 0, int _dy = 0);
  207. CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, BattleHex _destTile, bool _Vflip = false, bool _alignToBottom = false);
  208. virtual ~CEffectAnimation(){};
  209. };