BattleAnimationClasses.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. ECreatureAnimType::Type findValidGroup( const std::vector<ECreatureAnimType::Type> candidates ) const;
  64. public:
  65. virtual void playSound() = 0;
  66. void nextFrame() override;
  67. AttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender);
  68. ~AttackAnimation();
  69. };
  70. /// Animation of a defending unit
  71. class DefenceAnimation : public BattleStackAnimation
  72. {
  73. public:
  74. bool init() override;
  75. DefenceAnimation(BattleInterface & owner, const CStack * stack);
  76. };
  77. /// Animation of a hit unit
  78. class HittedAnimation : public BattleStackAnimation
  79. {
  80. public:
  81. HittedAnimation(BattleInterface & owner, const CStack * stack);
  82. bool init() override;
  83. };
  84. /// Animation of a dying unit
  85. class DeathAnimation : public BattleStackAnimation
  86. {
  87. bool rangedAttack;
  88. ECreatureAnimType::Type getMyAnimType();
  89. public:
  90. bool init() override;
  91. DeathAnimation(BattleInterface & owner, const CStack * stack, bool ranged);
  92. ~DeathAnimation();
  93. };
  94. class DummyAnimation : public BattleAnimation
  95. {
  96. private:
  97. int counter;
  98. int howMany;
  99. public:
  100. bool init() override;
  101. void nextFrame() override;
  102. DummyAnimation(BattleInterface & owner, int howManyFrames);
  103. };
  104. /// Hand-to-hand attack
  105. class MeleeAttackAnimation : public AttackAnimation
  106. {
  107. bool multiAttack;
  108. ECreatureAnimType::Type getUpwardsGroup() const;
  109. ECreatureAnimType::Type getForwardGroup() const;
  110. ECreatureAnimType::Type getDownwardsGroup() const;
  111. public:
  112. bool init() override;
  113. void nextFrame() override;
  114. void playSound() override;
  115. MeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool multiAttack);
  116. };
  117. /// Base class for all animations that play during stack movement
  118. class StackMoveAnimation : public BattleStackAnimation
  119. {
  120. public:
  121. BattleHex currentHex;
  122. protected:
  123. StackMoveAnimation(BattleInterface & owner, const CStack * _stack, BattleHex _currentHex);
  124. };
  125. /// Move animation of a creature
  126. class MovementAnimation : public StackMoveAnimation
  127. {
  128. private:
  129. std::vector<BattleHex> destTiles; //full path, includes already passed hexes
  130. ui32 curentMoveIndex; // index of nextHex in destTiles
  131. BattleHex oldPos; //position of stack before move
  132. double begX, begY; // starting position
  133. double distanceX, distanceY; // full movement distance, may be negative if creture moves topleft
  134. double timeToMove; // full length of movement animation
  135. double progress; // range 0 -> 1, indicates move progrees. 0 = movement starts, 1 = move ends
  136. public:
  137. bool init() override;
  138. void nextFrame() override;
  139. MovementAnimation(BattleInterface & owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance);
  140. ~MovementAnimation();
  141. };
  142. /// Move end animation of a creature
  143. class MovementEndAnimation : public StackMoveAnimation
  144. {
  145. public:
  146. bool init() override;
  147. MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile);
  148. ~MovementEndAnimation();
  149. };
  150. /// Move start animation of a creature
  151. class MovementStartAnimation : public StackMoveAnimation
  152. {
  153. public:
  154. bool init() override;
  155. MovementStartAnimation(BattleInterface & owner, const CStack * _stack);
  156. };
  157. /// Class responsible for animation of stack chaning direction (left <-> right)
  158. class ReverseAnimation : public StackMoveAnimation
  159. {
  160. void setupSecondPart();
  161. public:
  162. bool init() override;
  163. ReverseAnimation(BattleInterface & owner, const CStack * stack, BattleHex dest);
  164. };
  165. /// Resurrects stack from dead state
  166. class ResurrectionAnimation : public BattleStackAnimation
  167. {
  168. public:
  169. bool init() override;
  170. ResurrectionAnimation(BattleInterface & owner, const CStack * _stack);
  171. };
  172. /// Performs fade-in or fade-out animation on stack
  173. class FadingAnimation : public BattleStackAnimation
  174. {
  175. float progress;
  176. uint8_t from;
  177. uint8_t dest;
  178. public:
  179. bool init() override;
  180. void nextFrame() override;
  181. FadingAnimation(BattleInterface & owner, const CStack * _stack, uint8_t alphaFrom, uint8_t alphaDest);
  182. };
  183. class RangedAttackAnimation : public AttackAnimation
  184. {
  185. void setAnimationGroup();
  186. void initializeProjectile();
  187. void emitProjectile();
  188. void emitExplosion();
  189. protected:
  190. bool projectileEmitted;
  191. virtual ECreatureAnimType::Type getUpwardsGroup() const = 0;
  192. virtual ECreatureAnimType::Type getForwardGroup() const = 0;
  193. virtual ECreatureAnimType::Type getDownwardsGroup() const = 0;
  194. virtual void createProjectile(const Point & from, const Point & dest) const = 0;
  195. virtual uint32_t getAttackClimaxFrame() const = 0;
  196. public:
  197. RangedAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  198. ~RangedAttackAnimation();
  199. bool init() override;
  200. void nextFrame() override;
  201. void playSound() override;
  202. };
  203. /// Shooting attack
  204. class ShootingAnimation : public RangedAttackAnimation
  205. {
  206. ECreatureAnimType::Type getUpwardsGroup() const override;
  207. ECreatureAnimType::Type getForwardGroup() const override;
  208. ECreatureAnimType::Type getDownwardsGroup() const override;
  209. void createProjectile(const Point & from, const Point & dest) const override;
  210. uint32_t getAttackClimaxFrame() const override;
  211. public:
  212. ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  213. };
  214. /// Catapult attack
  215. class CatapultAnimation : public ShootingAnimation
  216. {
  217. private:
  218. bool explosionEmitted;
  219. int catapultDamage;
  220. public:
  221. CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender, int _catapultDmg = 0);
  222. void createProjectile(const Point & from, const Point & dest) const override;
  223. void nextFrame() override;
  224. };
  225. class CastAnimation : public RangedAttackAnimation
  226. {
  227. const CSpell * spell;
  228. ECreatureAnimType::Type getUpwardsGroup() const override;
  229. ECreatureAnimType::Type getForwardGroup() const override;
  230. ECreatureAnimType::Type getDownwardsGroup() const override;
  231. void createProjectile(const Point & from, const Point & dest) const override;
  232. uint32_t getAttackClimaxFrame() const override;
  233. public:
  234. CastAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell);
  235. };
  236. /// Class that plays effect at one or more positions along with (single) sound effect
  237. class PointEffectAnimation : public BattleAnimation
  238. {
  239. std::string soundName;
  240. bool soundPlayed;
  241. bool soundFinished;
  242. bool effectFinished;
  243. int effectFlags;
  244. std::shared_ptr<CAnimation> animation;
  245. std::vector<Point> positions;
  246. std::vector<BattleHex> battlehexes;
  247. bool alignToBottom() const;
  248. bool waitForSound() const;
  249. bool forceOnTop() const;
  250. bool screenFill() const;
  251. void onEffectFinished();
  252. void onSoundFinished();
  253. void clearEffect();
  254. void playSound();
  255. void playEffect();
  256. public:
  257. enum EEffectFlags
  258. {
  259. ALIGN_TO_BOTTOM = 1,
  260. WAIT_FOR_SOUND = 2, // Unused, can be removed
  261. FORCE_ON_TOP = 4,
  262. SCREEN_FILL = 8,
  263. };
  264. /// Create animation with screen-wide effect
  265. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, int effects = 0);
  266. /// Create animation positioned at point(s). Note that positions must be are absolute, including battleint position offset
  267. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos , int effects = 0);
  268. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<Point> pos , int effects = 0);
  269. /// Create animation positioned at certain hex(es)
  270. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, BattleHex hex , int effects = 0);
  271. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<BattleHex> hex, int effects = 0);
  272. PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, BattleHex hex, int effects = 0);
  273. ~PointEffectAnimation();
  274. bool init() override;
  275. void nextFrame() 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 nextFrame() override;
  290. bool init() override;
  291. };