BattleAnimationClasses.h 11 KB

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