BattleAnimationClasses.h 11 KB

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