BattleStacksController.h 5.2 KB

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