BattleAnimationClasses.h 12 KB

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