BattleAnimationClasses.h 10 KB

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