BattleAnimationClasses.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "../../lib/CSoundBase.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. class CAnimation;
  20. class BattleInterface;
  21. class CreatureAnimation;
  22. class CBattleAnimation;
  23. struct CatapultProjectileInfo;
  24. struct StackAttackedInfo;
  25. struct Point;
  26. class ColorShifter;
  27. /// Base class of battle animations
  28. class CBattleAnimation
  29. {
  30. protected:
  31. BattleInterface & owner;
  32. bool initialized;
  33. std::vector<CBattleAnimation *> & 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. bool checkInitialConditions(); //determines if this animation is earliest of all
  39. public:
  40. ui32 ID; //unique identifier
  41. bool isInitialized();
  42. bool tryInitialize();
  43. virtual void nextFrame() {} //call every new frame
  44. virtual ~CBattleAnimation();
  45. CBattleAnimation(BattleInterface & owner);
  46. };
  47. /// Sub-class which is responsible for managing the battle stack animation.
  48. class CBattleStackAnimation : public CBattleAnimation
  49. {
  50. public:
  51. std::shared_ptr<CreatureAnimation> myAnim; //animation for our stack, managed by CBattleInterface
  52. const CStack * stack; //id of stack whose animation it is
  53. CBattleStackAnimation(BattleInterface & owner, const CStack * _stack);
  54. void shiftColor(const ColorShifter * shifter);
  55. void rotateStack(BattleHex hex);
  56. };
  57. /// This class is responsible for managing the battle attack animation
  58. class CAttackAnimation : public CBattleStackAnimation
  59. {
  60. bool soundPlayed;
  61. protected:
  62. BattleHex dest; //attacked hex
  63. bool shooting;
  64. ECreatureAnimType::Type group; //if shooting is true, print this animation group
  65. const CStack *attackedStack;
  66. const CStack *attackingStack;
  67. int attackingStackPosBeforeReturn; //for stacks with return_after_strike feature
  68. const CCreature * getCreature() const;
  69. public:
  70. void nextFrame() override;
  71. bool checkInitialConditions();
  72. CAttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender);
  73. ~CAttackAnimation();
  74. };
  75. /// Animation of a defending unit
  76. class CDefenceAnimation : public CBattleStackAnimation
  77. {
  78. ECreatureAnimType::Type getMyAnimType();
  79. std::string getMySound();
  80. void startAnimation();
  81. const CStack * attacker; //attacking stack
  82. bool rangedAttack; //if true, stack has been attacked by shooting
  83. bool killed; //if true, stack has been killed
  84. float timeToWait; // for how long this animation should be paused
  85. public:
  86. bool init() override;
  87. void nextFrame() override;
  88. CDefenceAnimation(StackAttackedInfo _attackedInfo, BattleInterface & owner);
  89. ~CDefenceAnimation();
  90. };
  91. class CDummyAnimation : public CBattleAnimation
  92. {
  93. private:
  94. int counter;
  95. int howMany;
  96. public:
  97. bool init() override;
  98. void nextFrame() override;
  99. CDummyAnimation(BattleInterface & owner, int howManyFrames);
  100. };
  101. /// Hand-to-hand attack
  102. class CMeleeAttackAnimation : public CAttackAnimation
  103. {
  104. public:
  105. bool init() override;
  106. CMeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked);
  107. };
  108. /// Base class for all animations that play during stack movement
  109. class CStackMoveAnimation : public CBattleStackAnimation
  110. {
  111. public:
  112. BattleHex currentHex;
  113. protected:
  114. CStackMoveAnimation(BattleInterface & owner, const CStack * _stack, BattleHex _currentHex);
  115. };
  116. /// Move animation of a creature
  117. class CMovementAnimation : public CStackMoveAnimation
  118. {
  119. private:
  120. std::vector<BattleHex> destTiles; //full path, includes already passed hexes
  121. ui32 curentMoveIndex; // index of nextHex in destTiles
  122. BattleHex oldPos; //position of stack before move
  123. double begX, begY; // starting position
  124. double distanceX, distanceY; // full movement distance, may be negative if creture moves topleft
  125. double timeToMove; // full length of movement animation
  126. double progress; // range 0 -> 1, indicates move progrees. 0 = movement starts, 1 = move ends
  127. public:
  128. bool init() override;
  129. void nextFrame() override;
  130. CMovementAnimation(BattleInterface & owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance);
  131. ~CMovementAnimation();
  132. };
  133. /// Move end animation of a creature
  134. class CMovementEndAnimation : public CStackMoveAnimation
  135. {
  136. public:
  137. bool init() override;
  138. CMovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile);
  139. ~CMovementEndAnimation();
  140. };
  141. /// Move start animation of a creature
  142. class CMovementStartAnimation : public CStackMoveAnimation
  143. {
  144. public:
  145. bool init() override;
  146. CMovementStartAnimation(BattleInterface & owner, const CStack * _stack);
  147. };
  148. /// Class responsible for animation of stack chaning direction (left <-> right)
  149. class CReverseAnimation : public CStackMoveAnimation
  150. {
  151. public:
  152. bool priority; //true - high, false - low
  153. bool init() override;
  154. void setupSecondPart();
  155. CReverseAnimation(BattleInterface & owner, const CStack * stack, BattleHex dest, bool _priority);
  156. ~CReverseAnimation();
  157. };
  158. /// Resurrects stack from dead state
  159. class CResurrectionAnimation : public CBattleStackAnimation
  160. {
  161. public:
  162. bool init() override;
  163. CResurrectionAnimation(BattleInterface & owner, const CStack * stack);
  164. ~CResurrectionAnimation();
  165. };
  166. class CRangedAttackAnimation : public CAttackAnimation
  167. {
  168. void setAnimationGroup();
  169. void initializeProjectile();
  170. void emitProjectile();
  171. void emitExplosion();
  172. protected:
  173. bool projectileEmitted;
  174. virtual ECreatureAnimType::Type getUpwardsGroup() const = 0;
  175. virtual ECreatureAnimType::Type getForwardGroup() const = 0;
  176. virtual ECreatureAnimType::Type getDownwardsGroup() const = 0;
  177. virtual void createProjectile(const Point & from, const Point & dest) const = 0;
  178. virtual uint32_t getAttackClimaxFrame() const = 0;
  179. public:
  180. CRangedAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  181. ~CRangedAttackAnimation();
  182. bool init() override;
  183. void nextFrame() override;
  184. };
  185. /// Shooting attack
  186. class CShootingAnimation : public CRangedAttackAnimation
  187. {
  188. ECreatureAnimType::Type getUpwardsGroup() const override;
  189. ECreatureAnimType::Type getForwardGroup() const override;
  190. ECreatureAnimType::Type getDownwardsGroup() const override;
  191. void createProjectile(const Point & from, const Point & dest) const override;
  192. uint32_t getAttackClimaxFrame() const override;
  193. public:
  194. CShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender);
  195. };
  196. /// Catapult attack
  197. class CCatapultAnimation : public CShootingAnimation
  198. {
  199. private:
  200. bool explosionEmitted;
  201. int catapultDamage;
  202. public:
  203. CCatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest, const CStack * defender, int _catapultDmg = 0);
  204. void createProjectile(const Point & from, const Point & dest) const override;
  205. void nextFrame() override;
  206. };
  207. class CCastAnimation : public CRangedAttackAnimation
  208. {
  209. const CSpell * spell;
  210. ECreatureAnimType::Type findValidGroup( const std::vector<ECreatureAnimType::Type> candidates ) const;
  211. ECreatureAnimType::Type getUpwardsGroup() const override;
  212. ECreatureAnimType::Type getForwardGroup() const override;
  213. ECreatureAnimType::Type getDownwardsGroup() const override;
  214. void createProjectile(const Point & from, const Point & dest) const override;
  215. uint32_t getAttackClimaxFrame() const override;
  216. public:
  217. CCastAnimation(BattleInterface & owner, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell);
  218. };
  219. struct CPointEffectParameters
  220. {
  221. std::vector<Point> positions;
  222. std::vector<BattleHex> tiles;
  223. std::string animation;
  224. soundBase::soundID sound = soundBase::invalid;
  225. BattleHex boundHex = BattleHex::INVALID;
  226. bool aligntoBottom = false;
  227. bool waitForSound = false;
  228. bool screenFill = false;
  229. };
  230. /// Class that plays effect at one or more positions along with (single) sound effect
  231. class CPointEffectAnimation : public CBattleAnimation
  232. {
  233. soundBase::soundID sound;
  234. bool soundPlayed;
  235. bool soundFinished;
  236. bool effectFinished;
  237. int effectFlags;
  238. std::shared_ptr<CAnimation> animation;
  239. std::vector<Point> positions;
  240. std::vector<BattleHex> battlehexes;
  241. bool alignToBottom() const;
  242. bool waitForSound() const;
  243. bool forceOnTop() const;
  244. bool screenFill() const;
  245. void onEffectFinished();
  246. void onSoundFinished();
  247. void clearEffect();
  248. void playSound();
  249. void playEffect();
  250. public:
  251. enum EEffectFlags
  252. {
  253. ALIGN_TO_BOTTOM = 1,
  254. WAIT_FOR_SOUND = 2,
  255. FORCE_ON_TOP = 4,
  256. SCREEN_FILL = 8,
  257. };
  258. /// Create animation with screen-wide effect
  259. CPointEffectAnimation(BattleInterface & owner, soundBase::soundID sound, std::string animationName, int effects = 0);
  260. /// Create animation positioned at point(s). Note that positions must be are absolute, including battleint position offset
  261. CPointEffectAnimation(BattleInterface & owner, soundBase::soundID sound, std::string animationName, Point pos , int effects = 0);
  262. CPointEffectAnimation(BattleInterface & owner, soundBase::soundID sound, std::string animationName, std::vector<Point> pos , int effects = 0);
  263. /// Create animation positioned at certain hex(es)
  264. CPointEffectAnimation(BattleInterface & owner, soundBase::soundID sound, std::string animationName, BattleHex hex , int effects = 0);
  265. CPointEffectAnimation(BattleInterface & owner, soundBase::soundID sound, std::string animationName, std::vector<BattleHex> hex, int effects = 0);
  266. CPointEffectAnimation(BattleInterface & owner, soundBase::soundID sound, std::string animationName, Point pos, BattleHex hex, int effects = 0);
  267. ~CPointEffectAnimation();
  268. bool init() override;
  269. void nextFrame() override;
  270. };
  271. /// Base class (e.g. for use in dynamic_cast's) for "animations" that wait for certain event
  272. class CWaitingAnimation : public CBattleAnimation
  273. {
  274. protected:
  275. CWaitingAnimation(BattleInterface & owner);
  276. public:
  277. void nextFrame() override;
  278. };
  279. /// Class that waits till projectile of certain shooter hits a target
  280. class CWaitingProjectileAnimation : public CWaitingAnimation
  281. {
  282. const CStack * shooter;
  283. public:
  284. CWaitingProjectileAnimation(BattleInterface & owner, const CStack * shooter);
  285. bool init() override;
  286. };