BattleFieldController.h 5.7 KB

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