BattleFieldController.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * BattleFieldController.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/BattleHexArray.h"
  12. #include "../../lib/Point.h"
  13. #include "../gui/CIntObject.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CStack;
  16. class Rect;
  17. VCMI_LIB_NAMESPACE_END
  18. class BattleHero;
  19. class CAnimation;
  20. class Canvas;
  21. class IImage;
  22. class BattleInterface;
  23. /// Handles battlefield grid as well as rendering of background layer of battle interface
  24. class BattleFieldController : public CIntObject
  25. {
  26. BattleInterface & owner;
  27. std::shared_ptr<IImage> background;
  28. std::shared_ptr<IImage> cellBorder;
  29. std::shared_ptr<IImage> cellUnitMovementHighlight;
  30. std::shared_ptr<IImage> cellUnitMaxMovementHighlight;
  31. std::shared_ptr<IImage> cellShade;
  32. std::shared_ptr<CAnimation> rangedFullDamageLimitImages;
  33. std::shared_ptr<CAnimation> shootingRangeLimitImages;
  34. std::shared_ptr<CAnimation> attackCursors;
  35. std::shared_ptr<CAnimation> spellCursors;
  36. /// Canvas that contains background, hex grid (if enabled), absolute obstacles and movement range of active stack
  37. std::unique_ptr<Canvas> backgroundWithHexes;
  38. /// direction which will be used to perform attack with current cursor position
  39. Point currentAttackOriginPoint;
  40. /// hex currently under mouse hover
  41. BattleHex hoveredHex;
  42. /// hexes to which currently active stack can move
  43. BattleHexArray occupiableHexes;
  44. /// hexes that when in front of a unit cause it's amount box to move back
  45. std::array<bool, GameConstants::BFIELD_SIZE> stackCountOutsideHexes;
  46. void showHighlightedHex(Canvas & to, std::shared_ptr<IImage> highlight, const BattleHex & hex, bool darkBorder);
  47. BattleHexArray getHighlightedHexesForActiveStack();
  48. BattleHexArray getMovementRangeForHoveredStack();
  49. BattleHexArray getHighlightedHexesForSpellRange();
  50. BattleHexArray getHighlightedHexesForMovementTarget();
  51. // Range limit highlight helpers
  52. /// get all hexes within a certain distance of given hex
  53. BattleHexArray getRangeHexes(const BattleHex & sourceHex, uint8_t distance) const;
  54. /// get only hexes at the limit of a range
  55. BattleHexArray getRangeLimitHexes(const BattleHex & hoveredHex, const BattleHexArray & hexRange, uint8_t distanceToLimit) const;
  56. /// calculate if a hex is in range limit and return its index in range
  57. bool isHexInRangeLimit(const BattleHex & hex, const BattleHexArray & rangeLimitHexes, int * hexIndexInRangeLimit) const;
  58. /// get an array that has for each hex in range, an array with all directions where an outside neighbour hex exists
  59. std::vector<std::vector<BattleHex::EDir>> getOutsideNeighbourDirectionsForLimitHexes(const BattleHexArray & rangeHexes, const BattleHexArray & rangeLimitHexes) const;
  60. /// calculates what image to use as range limit, depending on the direction of neighbours
  61. /// a mask is used internally to mark the directions of all neighbours
  62. /// based on this mask the corresponding image is selected
  63. std::vector<std::shared_ptr<IImage>> calculateRangeLimitHighlightImages(std::vector<std::vector<BattleHex::EDir>> hexesNeighbourDirections, std::shared_ptr<CAnimation> limitImages);
  64. /// calculates all hexes for a range limit and what images to be shown as highlight for each of the hexes
  65. void calculateRangeLimitAndHighlightImages(uint8_t distance, std::shared_ptr<CAnimation> rangeLimitImages, BattleHexArray & rangeLimitHexes, std::vector<std::shared_ptr<IImage>> & rangeLimitHexesHighlights);
  66. void showBackground(Canvas & canvas);
  67. void showBackgroundImage(Canvas & canvas);
  68. void showBackgroundImageWithHexes(Canvas & canvas);
  69. void showHighlightedHexes(Canvas & canvas);
  70. void updateAccessibleHexes();
  71. BattleHex getHexAtPosition(Point hoverPosition);
  72. /// Checks whether selected pixel is transparent, uses local coordinates of a hex
  73. bool isPixelInHex(Point const & position);
  74. size_t selectBattleCursor(const BattleHex & myNumber);
  75. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  76. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  77. void mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance) override;
  78. void clickPressed(const Point & cursorPosition) override;
  79. void showPopupWindow(const Point & cursorPosition) override;
  80. void activate() override;
  81. void showAll(Canvas & to) override;
  82. void show(Canvas & to) override;
  83. void tick(uint32_t msPassed) override;
  84. bool receiveEvent(const Point & position, int eventType) const override;
  85. public:
  86. BattleFieldController(BattleInterface & owner);
  87. void createHeroes();
  88. void redrawBackgroundWithHexes();
  89. void renderBattlefield(Canvas & canvas);
  90. /// Returns position of hex relative to owner (BattleInterface)
  91. Rect hexPositionLocal(const BattleHex & hex) const;
  92. /// Returns position of hex relative to game window
  93. Rect hexPositionAbsolute(const BattleHex & hex) const;
  94. /// Returns ID of currently hovered hex or BattleHex::INVALID if none
  95. BattleHex getHoveredHex();
  96. /// Returns the currently hovered stack
  97. const CStack* getHoveredStack();
  98. /// returns true if selected tile can be attacked in melee by current stack
  99. bool isTileAttackable(const BattleHex & number) const;
  100. /// returns true if stack should render its stack count image in default position - outside own hex
  101. bool stackCountOutsideHex(const BattleHex & number) const;
  102. BattleHex::EDir selectAttackDirection(const BattleHex & myNumber) const;
  103. BattleHex fromWhichHexAttack(const BattleHex & myNumber);
  104. };