CBattleAnimations.h 7.9 KB

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