BattleStacksController.h 5.3 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. struct BattleHex;
  14. class BattleAction;
  15. class CStack;
  16. class CSpell;
  17. class SpellID;
  18. class Point;
  19. VCMI_LIB_NAMESPACE_END
  20. struct StackAttackedInfo;
  21. struct StackAttackInfo;
  22. class ColorFilter;
  23. class Canvas;
  24. class BattleInterface;
  25. class BattleAnimation;
  26. class CreatureAnimation;
  27. class BattleAnimation;
  28. class BattleRenderer;
  29. class IImage;
  30. struct BattleStackFilterEffect
  31. {
  32. ColorFilter effect;
  33. const CStack * target;
  34. const CSpell * source;
  35. bool persistent;
  36. };
  37. /// Class responsible for handling stacks in battle
  38. /// Handles ordering of stacks animation
  39. /// As well as rendering of stacks, their amount boxes
  40. /// And any other effect applied to stacks
  41. class BattleStacksController
  42. {
  43. BattleInterface & owner;
  44. std::shared_ptr<IImage> amountNormal;
  45. std::shared_ptr<IImage> amountNegative;
  46. std::shared_ptr<IImage> amountPositive;
  47. std::shared_ptr<IImage> amountEffNeutral;
  48. /// currently displayed animations <anim, initialized>
  49. std::vector<BattleAnimation *> currentAnimations;
  50. /// currently active color effects on stacks, in order of their addition (which corresponds to their apply order)
  51. std::vector<BattleStackFilterEffect> stackFilterEffects;
  52. /// animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  53. std::map<int32_t, std::shared_ptr<CreatureAnimation>> stackAnimation;
  54. /// <creatureID, if false reverse creature's animation> //TODO: move it to battle callback
  55. std::map<int, bool> stackFacingRight;
  56. /// currently active stack; nullptr - no one
  57. const CStack *activeStack;
  58. /// stacks or their battle queue images below mouse pointer (multiple stacks possible while spellcasting), used for border animation
  59. std::vector<const CStack *> mouseHoveredStacks;
  60. ///when animation is playing, we should wait till the end to make the next stack active; nullptr of none
  61. const CStack *stackToActivate;
  62. /// stack that was selected for multi-target spells - Teleport / Sacrifice
  63. const CStack *selectedStack;
  64. /// for giving IDs for animations
  65. ui32 animIDhelper;
  66. bool stackNeedsAmountBox(const CStack * stack) const;
  67. void showStackAmountBox(Canvas & canvas, const CStack * stack);
  68. BattleHex getStackCurrentPosition(const CStack * stack) const;
  69. std::shared_ptr<IImage> getStackAmountBox(const CStack * stack);
  70. void removeExpiredColorFilters();
  71. void initializeBattleAnimations();
  72. void stepFrameBattleAnimations();
  73. void updateBattleAnimations();
  74. void updateHoveredStacks();
  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, std::vector<BattleHex> destHex, int distance); //stack with id number moved to destHex
  86. void stackTeleported(const CStack *stack, std::vector<BattleHex> 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 setSelectedStack(const CStack *stack);
  95. void showAliveStack(Canvas & canvas, const CStack * stack);
  96. void showStack(Canvas & canvas, const CStack * stack);
  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 CStack* getSelectedStack() const;
  105. const std::vector<uint32_t> getHoveredStacksUnitIds() const;
  106. void update();
  107. /// returns position of animation needed to place stack in specific hex
  108. Point getStackPositionAtHex(BattleHex hexNum, const CStack * creature) const;
  109. friend class BattleAnimation; // for exposing pendingAnims/creAnims/creDir to animations
  110. };