BattleAnimationClasses.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * BattleAnimations.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 "BattleConstants.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CStack;
  15. class CCreature;
  16. class CSpell;
  17. VCMI_LIB_NAMESPACE_END
  18. class BattleHero;
  19. class CAnimation;
  20. class BattleInterface;
  21. class CreatureAnimation;
  22. struct StackAttackedInfo;
  23. struct Point;
  24. /// Base class of battle animations
  25. class BattleAnimation
  26. {
  27. protected:
  28. BattleInterface & owner;
  29. bool initialized;
  30. std::vector<BattleAnimation *> & pendingAnimations();
  31. std::shared_ptr<CreatureAnimation> stackAnimation(const CStack * stack) const;
  32. bool stackFacingRight(const CStack * stack);
  33. void setStackFacingRight(const CStack * stack, bool facingRight);
  34. virtual bool init() = 0; //to be called - if returned false, call again until returns true
  35. public:
  36. ui32 ID; //unique identifier
  37. bool isInitialized();
  38. bool tryInitialize();
  39. virtual void nextFrame() {} //call every new frame
  40. virtual ~BattleAnimation();
  41. BattleAnimation(BattleInterface & owner);
  42. };
  43. /// Sub-class which is responsible for managing the battle stack animation.
  44. class BattleStackAnimation : public BattleAnimation
  45. {
  46. public:
  47. std::shared_ptr<CreatureAnimation> myAnim; //animation for our stack, managed by BattleInterface
  48. const CStack * stack; //id of stack whose animation it is
  49. BattleStackAnimation(BattleInterface & owner, const CStack * _stack);
  50. void rotateStack(BattleHex hex);
  51. };
  52. /// This class is responsible for managing the battle attack animation
  53. class AttackAnimation : public BattleStackAnimation
  54. {
  55. bool soundPlayed;
  56. protected:
  57. BattleHex dest; //attacked hex
  58. ECreatureAnimType::Type group;
  59. const CStack *defendingStack;
  60. const CStack *attackingStack;
  61. int attackingStackPosBeforeReturn; //for stacks with return_after_strike feature
  62. const CCreature * getCreature() const;
  63. public:
  64. virtual void playSound() = 0;
  65. void nextFrame() override;
  66. AttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender);
  67. ~AttackAnimation();
  68. };
  69. /// Animation of a defending unit
  70. class DefenceAnimation : public BattleStackAnimation
  71. {
  72. public:
  73. bool init() override;
  74. DefenceAnimation(BattleInterface & owner, const CStack * stack);
  75. };
  76. /// Animation of a hit unit
  77. class HittedAnimation : public BattleStackAnimation
  78. {
  79. public:
  80. HittedAnimation(BattleInterface & owner, const CStack * stack);
  81. bool init() override;
  82. };
  83. /// Animation of a dying unit
  84. class DeathAnimation : public BattleStackAnimation
  85. {
  86. bool rangedAttack;
  87. ECreatureAnimType::Type getMyAnimType();
  88. public:
  89. bool init() override;
  90. DeathAnimation(BattleInterface & owner, const CStack * stack, bool ranged);
  91. ~DeathAnimation();
  92. };
  93. class DummyAnimation : public BattleAnimation
  94. {
  95. private:
  96. int counter;
  97. int howMany;
  98. public:
  99. bool init() override;
  100. void nextFrame() override;
  101. DummyAnimation(BattleInterface & owner, int howManyFrames);
  102. };
  103. /// Hand-to-hand attack
  104. class MeleeAttackAnimation : public AttackAnimation
  105. {
  106. public:
  107. bool init() override;
  108. void nextFrame() override;
  109. void playSound() override;
  110. MeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked);
  111. };
  112. /// Base class for all animations that play during stack movement
  113. class StackMoveAnimation : public BattleStackAnimation
  114. {
  115. public:
  116. BattleHex currentHex;
  117. protected:
  118. StackMoveAnimation(BattleInterface & owner, const CStack * _stack, BattleHex _currentHex);
  119. };
  120. /// Move animation of a creature
  121. class MovementAnimation : public StackMoveAnimation
  122. {
  123. private:
  124. std::vector<BattleHex> destTiles; //full path, includes already passed hexes
  125. ui32 curentMoveIndex; // index of nextHex in destTiles
  126. BattleHex oldPos; //position of stack before move
  127. double begX, begY; // starting position
  128. double distanceX, distanceY; // full movement distance, may be negative if creture moves topleft
  129. double timeToMove; // full length of movement animation
  130. double progress; // range 0 -> 1, indicates move progrees. 0 = movement starts, 1 = move ends
  131. public:
  132. bool init() override;
  133. void nextFrame() override;
  134. MovementAnimation(BattleInterface & owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance);
  135. ~MovementAnimation();
  136. };
  137. /// Move end animation of a creature
  138. class MovementEndAnimation : public StackMoveAnimation
  139. {
  140. public:
  141. bool init() override;
  142. MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile);
  143. ~MovementEndAnimation();
  144. };
  145. /// Move start animation of a creature
  146. class MovementStartAnimation : public StackMoveAnimation
  147. {
  148. public:
  149. bool init() override;
  150. MovementStartAnimation(BattleInterface & owner, const CStack * _stack);
  151. };
  152. /// Class responsible for animation of stack chaning direction (left <-> right)
  153. class ReverseAnimation : public StackMoveAnimation
  154. {
  155. void setupSecondPart();
  156. public:
  157. bool init() override;
  158. ReverseAnimation(BattleInterface & owner, const CStack * stack, BattleHex dest);
  159. };
  160. /// Resurrects stack from dead state
  161. class ResurrectionAnimation : public BattleStackAnimation
  162. {
  163. public:
  164. bool init() override;
  165. ResurrectionAnimation(BattleInterface & owner, const CStack * _stack);
  166. };
  167. class RangedAttackAnimation : public AttackAnimation
  168. {
  169. void setAnimationGroup();
  170. void initializeProjectile();
  171. void emitProjectile();
  172. void emitExplosion();
  173. protected:
  174. bool projectileEmitted;
  175. virtual ECreatureAnimType::Type getUpwardsGroup() const = 0;
  176. virtual ECreatureAnimType::Type getForwardGroup() const = 0;
  177. virtual ECreatureAnimType::Type getDownwardsGroup() const = 0;
  178. virtual void createProjectile(const Point & from, const Point & dest) const = 0;
  179. virtual uint32_t getAttackClimaxFrame() const = 0;
  180. public:
  181. RangedAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  182. ~RangedAttackAnimation();
  183. bool init() override;
  184. void nextFrame() override;
  185. void playSound() override;
  186. };
  187. /// Shooting attack
  188. class ShootingAnimation : public RangedAttackAnimation
  189. {
  190. ECreatureAnimType::Type getUpwardsGroup() const override;
  191. ECreatureAnimType::Type getForwardGroup() const override;
  192. ECreatureAnimType::Type getDownwardsGroup() const override;
  193. void createProjectile(const Point & from, const Point & dest) const override;
  194. uint32_t getAttackClimaxFrame() const override;
  195. public:
  196. ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  197. };
  198. /// Catapult attack
  199. class CatapultAnimation : public ShootingAnimation
  200. {
  201. private:
  202. bool explosionEmitted;
  203. int catapultDamage;
  204. public:
  205. CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender, int _catapultDmg = 0);
  206. void createProjectile(const Point & from, const Point & dest) const override;
  207. void nextFrame() override;
  208. };
  209. class CastAnimation : public RangedAttackAnimation
  210. {
  211. const CSpell * spell;
  212. ECreatureAnimType::Type findValidGroup( const std::vector<ECreatureAnimType::Type> candidates ) const;
  213. ECreatureAnimType::Type getUpwardsGroup() const override;
  214. ECreatureAnimType::Type getForwardGroup() const override;
  215. ECreatureAnimType::Type getDownwardsGroup() const override;
  216. void createProjectile(const Point & from, const Point & dest) const override;
  217. uint32_t getAttackClimaxFrame() const override;
  218. public:
  219. CastAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell);
  220. };
  221. /// Class that plays effect at one or more positions along with (single) sound effect
  222. class PointEffectAnimation : public BattleAnimation
  223. {
  224. std::string soundName;
  225. bool soundPlayed;
  226. bool soundFinished;
  227. bool effectFinished;
  228. int effectFlags;
  229. std::shared_ptr<CAnimation> animation;
  230. std::vector<Point> positions;
  231. std::vector<BattleHex> battlehexes;
  232. bool alignToBottom() const;
  233. bool waitForSound() const;
  234. bool forceOnTop() const;
  235. bool screenFill() const;
  236. void onEffectFinished();
  237. void onSoundFinished();
  238. void clearEffect();
  239. void playSound();
  240. void playEffect();
  241. public:
  242. enum EEffectFlags
  243. {
  244. ALIGN_TO_BOTTOM = 1,
  245. WAIT_FOR_SOUND = 2, // Unused, can be removed
  246. FORCE_ON_TOP = 4,
  247. SCREEN_FILL = 8,
  248. };
  249. /// Create animation with screen-wide effect
  250. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, int effects = 0);
  251. /// Create animation positioned at point(s). Note that positions must be are absolute, including battleint position offset
  252. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos , int effects = 0);
  253. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<Point> pos , int effects = 0);
  254. /// Create animation positioned at certain hex(es)
  255. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, BattleHex hex , int effects = 0);
  256. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<BattleHex> hex, int effects = 0);
  257. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, BattleHex hex, int effects = 0);
  258. ~PointEffectAnimation();
  259. bool init() override;
  260. void nextFrame() override;
  261. };
  262. class HeroCastAnimation : public BattleAnimation
  263. {
  264. std::shared_ptr<BattleHero> hero;
  265. const CStack * target;
  266. const CSpell * spell;
  267. BattleHex tile;
  268. bool projectileEmitted;
  269. void initializeProjectile();
  270. void emitProjectile();
  271. void emitAnimationEvent();
  272. public:
  273. HeroCastAnimation(BattleInterface & owner, std::shared_ptr<BattleHero> hero, BattleHex dest, const CStack * defender, const CSpell * spell);
  274. void nextFrame() override;
  275. bool init() override;
  276. };