BattleAnimationClasses.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 nextFrame() {} //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 nextFrame() 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. 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. double timeToMove; // full length of movement animation
  121. double progress; // range 0 -> 1, indicates move progrees. 0 = movement starts, 1 = move ends
  122. public:
  123. bool init() override;
  124. void nextFrame() override;
  125. MovementAnimation(BattleInterface & owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance);
  126. ~MovementAnimation();
  127. };
  128. /// Move end animation of a creature
  129. class MovementEndAnimation : public StackMoveAnimation
  130. {
  131. public:
  132. bool init() override;
  133. MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile);
  134. ~MovementEndAnimation();
  135. };
  136. /// Move start animation of a creature
  137. class MovementStartAnimation : public StackMoveAnimation
  138. {
  139. public:
  140. bool init() override;
  141. MovementStartAnimation(BattleInterface & owner, const CStack * _stack);
  142. };
  143. /// Class responsible for animation of stack chaning direction (left <-> right)
  144. class ReverseAnimation : public StackMoveAnimation
  145. {
  146. void setupSecondPart();
  147. public:
  148. bool init() override;
  149. ReverseAnimation(BattleInterface & owner, const CStack * stack, BattleHex dest);
  150. };
  151. /// This class is responsible for managing the battle attack animation
  152. class AttackAnimation : public StackActionAnimation
  153. {
  154. protected:
  155. BattleHex dest; //attacked hex
  156. const CStack *defendingStack;
  157. const CStack *attackingStack;
  158. int attackingStackPosBeforeReturn; //for stacks with return_after_strike feature
  159. const CCreature * getCreature() const;
  160. ECreatureAnimType findValidGroup( const std::vector<ECreatureAnimType> candidates ) const;
  161. public:
  162. AttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender);
  163. };
  164. /// Hand-to-hand attack
  165. class MeleeAttackAnimation : public AttackAnimation
  166. {
  167. ECreatureAnimType getUpwardsGroup(bool multiAttack) const;
  168. ECreatureAnimType getForwardGroup(bool multiAttack) const;
  169. ECreatureAnimType getDownwardsGroup(bool multiAttack) const;
  170. ECreatureAnimType selectGroup(bool multiAttack);
  171. public:
  172. MeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool multiAttack);
  173. void nextFrame() override;
  174. };
  175. class RangedAttackAnimation : public AttackAnimation
  176. {
  177. void setAnimationGroup();
  178. void initializeProjectile();
  179. void emitProjectile();
  180. void emitExplosion();
  181. protected:
  182. bool projectileEmitted;
  183. virtual ECreatureAnimType getUpwardsGroup() const = 0;
  184. virtual ECreatureAnimType getForwardGroup() const = 0;
  185. virtual ECreatureAnimType getDownwardsGroup() const = 0;
  186. virtual void createProjectile(const Point & from, const Point & dest) const = 0;
  187. virtual uint32_t getAttackClimaxFrame() const = 0;
  188. public:
  189. RangedAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  190. ~RangedAttackAnimation();
  191. bool init() override;
  192. void nextFrame() override;
  193. };
  194. /// Shooting attack
  195. class ShootingAnimation : public RangedAttackAnimation
  196. {
  197. ECreatureAnimType getUpwardsGroup() const override;
  198. ECreatureAnimType getForwardGroup() const override;
  199. ECreatureAnimType getDownwardsGroup() const override;
  200. void createProjectile(const Point & from, const Point & dest) const override;
  201. uint32_t getAttackClimaxFrame() const override;
  202. public:
  203. ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  204. };
  205. /// Catapult attack
  206. class CatapultAnimation : public ShootingAnimation
  207. {
  208. private:
  209. bool explosionEmitted;
  210. int catapultDamage;
  211. public:
  212. CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender, int _catapultDmg = 0);
  213. void createProjectile(const Point & from, const Point & dest) const override;
  214. void nextFrame() override;
  215. };
  216. class CastAnimation : public RangedAttackAnimation
  217. {
  218. const CSpell * spell;
  219. ECreatureAnimType getUpwardsGroup() const override;
  220. ECreatureAnimType getForwardGroup() const override;
  221. ECreatureAnimType getDownwardsGroup() const override;
  222. void createProjectile(const Point & from, const Point & dest) const override;
  223. uint32_t getAttackClimaxFrame() const override;
  224. public:
  225. CastAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell);
  226. };
  227. class DummyAnimation : public BattleAnimation
  228. {
  229. private:
  230. int counter;
  231. int howMany;
  232. public:
  233. bool init() override;
  234. void nextFrame() override;
  235. DummyAnimation(BattleInterface & owner, int howManyFrames);
  236. };
  237. /// Class that plays effect at one or more positions along with (single) sound effect
  238. class EffectAnimation : public BattleAnimation
  239. {
  240. std::string soundName;
  241. bool effectFinished;
  242. int effectFlags;
  243. std::shared_ptr<CAnimation> animation;
  244. std::vector<Point> positions;
  245. std::vector<BattleHex> battlehexes;
  246. bool alignToBottom() const;
  247. bool waitForSound() const;
  248. bool forceOnTop() const;
  249. bool screenFill() const;
  250. void onEffectFinished();
  251. void clearEffect();
  252. void playEffect();
  253. public:
  254. enum EEffectFlags
  255. {
  256. ALIGN_TO_BOTTOM = 1,
  257. FORCE_ON_TOP = 2,
  258. SCREEN_FILL = 4,
  259. };
  260. /// Create animation with screen-wide effect
  261. EffectAnimation(BattleInterface & owner, std::string animationName, int effects = 0);
  262. /// Create animation positioned at point(s). Note that positions must be are absolute, including battleint position offset
  263. EffectAnimation(BattleInterface & owner, std::string animationName, Point pos , int effects = 0);
  264. EffectAnimation(BattleInterface & owner, std::string animationName, std::vector<Point> pos , int effects = 0);
  265. /// Create animation positioned at certain hex(es)
  266. EffectAnimation(BattleInterface & owner, std::string animationName, BattleHex hex , int effects = 0);
  267. EffectAnimation(BattleInterface & owner, std::string animationName, std::vector<BattleHex> hex, int effects = 0);
  268. EffectAnimation(BattleInterface & owner, std::string animationName, Point pos, BattleHex hex, int effects = 0);
  269. ~EffectAnimation();
  270. bool init() override;
  271. void nextFrame() override;
  272. };
  273. class HeroCastAnimation : public BattleAnimation
  274. {
  275. std::shared_ptr<BattleHero> hero;
  276. const CStack * target;
  277. const CSpell * spell;
  278. BattleHex tile;
  279. bool projectileEmitted;
  280. void initializeProjectile();
  281. void emitProjectile();
  282. void emitAnimationEvent();
  283. public:
  284. HeroCastAnimation(BattleInterface & owner, std::shared_ptr<BattleHero> hero, BattleHex dest, const CStack * defender, const CSpell * spell);
  285. void nextFrame() override;
  286. bool init() override;
  287. };