CBattleAnimations.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #pragma once
  2. #include "../../lib/battle/BattleHex.h"
  3. #include "../widgets/Images.h"
  4. class CBattleInterface;
  5. class CStack;
  6. class CCreatureAnimation;
  7. struct CatapultProjectileInfo;
  8. struct StackAttackedInfo;
  9. /*
  10. * CBattleAnimations.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  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. 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. };
  40. /// This class is responsible for managing the battle attack animation
  41. class CAttackAnimation : public CBattleStackAnimation
  42. {
  43. bool soundPlayed;
  44. protected:
  45. BattleHex dest; //attacked hex
  46. bool shooting;
  47. CCreatureAnim::EAnimType group; //if shooting is true, print this animation group
  48. const CStack *attackedStack;
  49. const CStack *attackingStack;
  50. int attackingStackPosBeforeReturn; //for stacks with return_after_strike feature
  51. public:
  52. void nextFrame() override;
  53. void endAnim() override;
  54. bool checkInitialConditions();
  55. CAttackAnimation(CBattleInterface *_owner, const CStack *attacker, BattleHex _dest, const CStack *defender);
  56. };
  57. /// Animation of a defending unit
  58. class CDefenceAnimation : public CBattleStackAnimation
  59. {
  60. CCreatureAnim::EAnimType getMyAnimType();
  61. std::string getMySound();
  62. void startAnimation();
  63. const CStack * attacker; //attacking stack
  64. bool rangedAttack; //if true, stack has been attacked by shooting
  65. bool killed; //if true, stack has been killed
  66. float timeToWait; // for how long this animation should be paused
  67. public:
  68. bool init() override;
  69. void nextFrame() override;
  70. void endAnim() override;
  71. CDefenceAnimation(StackAttackedInfo _attackedInfo, CBattleInterface * _owner);
  72. virtual ~CDefenceAnimation(){};
  73. };
  74. class CDummyAnimation : public CBattleAnimation
  75. {
  76. private:
  77. int counter;
  78. int howMany;
  79. public:
  80. bool init() override;
  81. void nextFrame() override;
  82. void endAnim() override;
  83. CDummyAnimation(CBattleInterface * _owner, int howManyFrames);
  84. virtual ~CDummyAnimation(){}
  85. };
  86. /// Hand-to-hand attack
  87. class CMeleeAttackAnimation : public CAttackAnimation
  88. {
  89. public:
  90. bool init() override;
  91. void endAnim() override;
  92. CMeleeAttackAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked);
  93. virtual ~CMeleeAttackAnimation(){};
  94. };
  95. /// Move animation of a creature
  96. class CMovementAnimation : public CBattleStackAnimation
  97. {
  98. private:
  99. bool shouldRotate();
  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. /// Shooting attack
  164. class CShootingAnimation : public CAttackAnimation
  165. {
  166. private:
  167. int catapultDamage;
  168. public:
  169. bool init() override;
  170. void nextFrame() override;
  171. void endAnim() override;
  172. //last two params only for catapult attacks
  173. CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest,
  174. const CStack * _attacked, bool _catapult = false, int _catapultDmg = 0);
  175. virtual ~CShootingAnimation(){};
  176. };
  177. /// This class manages a spell effect animation
  178. class CSpellEffectAnimation : public CBattleAnimation
  179. {
  180. private:
  181. ui32 effect;
  182. BattleHex destTile;
  183. std::string customAnim;
  184. int x, y, dx, dy;
  185. bool Vflip;
  186. bool alignToBottom;
  187. public:
  188. bool init() override;
  189. void nextFrame() override;
  190. void endAnim() override;
  191. CSpellEffectAnimation(CBattleInterface * _owner, ui32 _effect, BattleHex _destTile, int _dx = 0, int _dy = 0, bool _Vflip = false, bool _alignToBottom = false);
  192. CSpellEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx = 0, int _dy = 0, bool _Vflip = false, bool _alignToBottom = false);
  193. CSpellEffectAnimation(CBattleInterface * _owner, std::string _customAnim, BattleHex _destTile, bool _Vflip = false, bool _alignToBottom = false);
  194. virtual ~CSpellEffectAnimation(){};
  195. };