BattleFieldController.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Canvas;
  20. class IImage;
  21. class BattleInterface;
  22. /// Handles battlefield grid as well as rendering of background layer of battle interface
  23. class BattleFieldController : public CIntObject
  24. {
  25. BattleInterface & owner;
  26. std::shared_ptr<IImage> background;
  27. std::shared_ptr<IImage> cellBorder;
  28. std::shared_ptr<IImage> cellUnitMovementHighlight;
  29. std::shared_ptr<IImage> cellUnitMaxMovementHighlight;
  30. std::shared_ptr<IImage> cellShade;
  31. std::unique_ptr<CAnimation> rangedFullDamageLimitImages;
  32. std::shared_ptr<CAnimation> attackCursors;
  33. /// Canvas that contains background, hex grid (if enabled), absolute obstacles and movement range of active stack
  34. std::unique_ptr<Canvas> backgroundWithHexes;
  35. /// direction which will be used to perform attack with current cursor position
  36. Point currentAttackOriginPoint;
  37. /// hex currently under mouse hover
  38. BattleHex hoveredHex;
  39. /// hexes to which currently active stack can move
  40. std::vector<BattleHex> occupiableHexes;
  41. /// hexes that when in front of a unit cause it's amount box to move back
  42. std::array<bool, GameConstants::BFIELD_SIZE> stackCountOutsideHexes;
  43. void showHighlightedHex(Canvas & to, std::shared_ptr<IImage> highlight, BattleHex hex, bool darkBorder);
  44. std::set<BattleHex> getHighlightedHexesForActiveStack();
  45. std::set<BattleHex> getMovementRangeForHoveredStack();
  46. std::set<BattleHex> getHighlightedHexesForSpellRange();
  47. std::set<BattleHex> getHighlightedHexesForMovementTarget();
  48. /// get all hexes where a ranged unit can do full damage
  49. std::vector<BattleHex> getRangedFullDamageHexes();
  50. /// get only hexes at the limit of a ranged unit's full damage range
  51. std::vector<BattleHex> getRangedFullDamageLimitHexes(std::vector<BattleHex> rangedFullDamageHexes);
  52. /// get an array that has for each hex in range, an aray with all directions where an ouside neighbour hex exists
  53. std::vector<std::vector<BattleHex::EDir>> getOutsideNeighbourDirectionsForLimitHexes(std::vector<BattleHex> rangedFullDamageHexes, std::vector<BattleHex> rangedFullDamageLimitHexes);
  54. /// calculates what image to use as range limit, depending on the direction of neighbors
  55. /// a mask is used internally to mark the directions of all neighbours
  56. /// based on this mask the corresponding image is selected
  57. std::vector<std::shared_ptr<IImage>> calculateRangedFullDamageHighlightImages(std::vector<std::vector<BattleHex::EDir>> fullRangeLimitHexesNeighbourDirections);
  58. /// to reduce the number of source images used, some images will be used as flipped versions of preloaded ones
  59. void flipRangedFullDamageLimitImagesIntoPositions();
  60. void showBackground(Canvas & canvas);
  61. void showBackgroundImage(Canvas & canvas);
  62. void showBackgroundImageWithHexes(Canvas & canvas);
  63. void showHighlightedHexes(Canvas & canvas);
  64. void updateAccessibleHexes();
  65. BattleHex getHexAtPosition(Point hoverPosition);
  66. /// Checks whether selected pixel is transparent, uses local coordinates of a hex
  67. bool isPixelInHex(Point const & position);
  68. size_t selectBattleCursor(BattleHex myNumber);
  69. void gesture(bool on, const Point & initialPosition, const Point & finalPosition) override;
  70. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  71. void mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance) override;
  72. void clickLeft(tribool down, bool previousState) override;
  73. void showPopupWindow() override;
  74. void activate() override;
  75. void showAll(Canvas & to) override;
  76. void show(Canvas & to) override;
  77. void tick(uint32_t msPassed) override;
  78. bool receiveEvent(const Point & position, int eventType) const override;
  79. public:
  80. BattleFieldController(BattleInterface & owner);
  81. void createHeroes();
  82. void redrawBackgroundWithHexes();
  83. void renderBattlefield(Canvas & canvas);
  84. /// Returns position of hex relative to owner (BattleInterface)
  85. Rect hexPositionLocal(BattleHex hex) const;
  86. /// Returns position of hex relative to game window
  87. Rect hexPositionAbsolute(BattleHex hex) const;
  88. /// Returns ID of currently hovered hex or BattleHex::INVALID if none
  89. BattleHex getHoveredHex();
  90. /// returns true if selected tile can be attacked in melee by current stack
  91. bool isTileAttackable(const BattleHex & number) const;
  92. /// returns true if stack should render its stack count image in default position - outside own hex
  93. bool stackCountOutsideHex(const BattleHex & number) const;
  94. BattleHex::EDir selectAttackDirection(BattleHex myNumber);
  95. BattleHex fromWhichHexAttack(BattleHex myNumber);
  96. };