BattleAnimationClasses.h 11 KB

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