BattleStacksController.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * BattleStacksController.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 "../render/ColorFilter.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class BattleHex;
  14. class BattleHexArray;
  15. class BattleAction;
  16. class CStack;
  17. class CSpell;
  18. class SpellID;
  19. class Point;
  20. VCMI_LIB_NAMESPACE_END
  21. struct StackAttackedInfo;
  22. struct StackAttackInfo;
  23. class ColorFilter;
  24. class Canvas;
  25. class BattleInterface;
  26. class BattleAnimation;
  27. class CreatureAnimation;
  28. class BattleAnimation;
  29. class BattleRenderer;
  30. class IImage;
  31. struct BattleStackFilterEffect
  32. {
  33. ColorFilter effect;
  34. const CStack * target;
  35. const CSpell * source;
  36. bool persistent;
  37. };
  38. /// Class responsible for handling stacks in battle
  39. /// Handles ordering of stacks animation
  40. /// As well as rendering of stacks, their amount boxes
  41. /// And any other effect applied to stacks
  42. class BattleStacksController
  43. {
  44. BattleInterface & owner;
  45. std::shared_ptr<IImage> amountNormal;
  46. std::shared_ptr<IImage> amountNegative;
  47. std::shared_ptr<IImage> amountPositive;
  48. std::shared_ptr<IImage> amountEffNeutral;
  49. /// currently displayed animations <anim, initialized>
  50. std::vector<BattleAnimation *> currentAnimations;
  51. /// currently active color effects on stacks, in order of their addition (which corresponds to their apply order)
  52. std::vector<BattleStackFilterEffect> stackFilterEffects;
  53. /// animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  54. std::map<int32_t, std::shared_ptr<CreatureAnimation>> stackAnimation;
  55. /// <creatureID, if false reverse creature's animation> //TODO: move it to battle callback
  56. std::map<int, bool> stackFacingRight;
  57. /// Stacks have amount box hidden due to ongoing animations
  58. std::set<int> stackAmountBoxHidden;
  59. /// currently active stack; nullptr - no one
  60. const CStack *activeStack;
  61. /// stacks or their battle queue images below mouse pointer (multiple stacks possible while spellcasting), used for border animation
  62. std::vector<const CStack *> mouseHoveredStacks;
  63. ///when animation is playing, we should wait till the end to make the next stack active; nullptr of none
  64. const CStack *stackToActivate;
  65. /// for giving IDs for animations
  66. ui32 animIDhelper;
  67. bool stackNeedsAmountBox(const CStack * stack) const;
  68. void showStackAmountBox(Canvas & canvas, const CStack * stack);
  69. BattleHex getStackCurrentPosition(const CStack * stack) const;
  70. std::shared_ptr<IImage> getStackAmountBox(const CStack * stack);
  71. void removeExpiredColorFilters();
  72. void initializeBattleAnimations();
  73. void tickFrameBattleAnimations(uint32_t msPassed);
  74. void updateBattleAnimations(uint32_t msPassed);
  75. std::vector<const CStack *> selectHoveredStacks();
  76. bool shouldAttackFacingRight(const CStack * attacker, const CStack * defender);
  77. public:
  78. BattleStacksController(BattleInterface & owner);
  79. bool shouldRotate(const CStack * stack, const BattleHex & oldPos, const BattleHex & nextHex) const;
  80. bool facingRight(const CStack * stack) const;
  81. void stackReset(const CStack * stack);
  82. void stackAdded(const CStack * stack, bool instant); //new stack appeared on battlefield
  83. void stackRemoved(uint32_t stackID); //stack disappeared from batlefiled
  84. void stackActivated(const CStack *stack); //active stack has been changed
  85. void stackMoved(const CStack *stack, const BattleHexArray & destHex, int distance); //stack with id number moved to destHex
  86. void stackTeleported(const CStack *stack, const BattleHexArray & destHex, int distance); //stack with id number moved to destHex
  87. void stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos); //called when a certain amount of stacks has been attacked
  88. void stackAttacking(const StackAttackInfo & info); //called when stack with id ID is attacking something on hex dest
  89. void startAction(const BattleAction & action);
  90. void endAction(const BattleAction & action);
  91. void deactivateStack(); //copy activeStack to stackToActivate, then set activeStack to nullptr to temporary disable current stack
  92. void activateStack(); //copy stackToActivate to activeStack to enable controls of the stack
  93. void setActiveStack(const CStack *stack);
  94. void showAliveStack(Canvas & canvas, const CStack * stack);
  95. void showStack(Canvas & canvas, const CStack * stack);
  96. void updateHoveredStacks();
  97. void collectRenderableObjects(BattleRenderer & renderer);
  98. /// Adds new color filter effect targeting stack
  99. /// Effect will last as long as stack is affected by specified spell (unless effect is persistent)
  100. /// If effect from same (target, source) already exists, it will be updated
  101. void setStackColorFilter(const ColorFilter & effect, const CStack * target, const CSpell *source, bool persistent);
  102. void addNewAnim(BattleAnimation *anim); //adds new anim to pendingAnims
  103. const CStack* getActiveStack() const;
  104. const std::vector<uint32_t> getHoveredStacksUnitIds() const;
  105. void tick(uint32_t msPassed);
  106. /// returns position of animation needed to place stack in specific hex
  107. Point getStackPositionAtHex(BattleHex hexNum, const CStack * creature) const;
  108. friend class BattleAnimation; // for exposing pendingAnims/creAnims/creDir to animations
  109. };