CBattleFieldController.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * CBattleFieldController.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 "../gui/CIntObject.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CStack;
  15. VCMI_LIB_NAMESPACE_END
  16. struct SDL_Surface;
  17. struct Rect;
  18. struct Point;
  19. class CClickableHex;
  20. class CCanvas;
  21. class IImage;
  22. class CBattleInterface;
  23. enum class EBattleFieldLayer {
  24. // confirmed ordering requirements:
  25. OBSTACLES = 0,
  26. CORPSES = 0,
  27. WALLS = 1,
  28. HEROES = 1,
  29. STACKS = 1, // after corpses, obstacles
  30. BATTLEMENTS = 2, // after stacks
  31. STACK_AMOUNTS = 2, // after stacks, obstacles, corpses
  32. EFFECTS = 3, // after obstacles, battlements
  33. };
  34. class CBattleFieldRenderer
  35. {
  36. public:
  37. using RendererPtr = std::shared_ptr<CCanvas>;
  38. using RenderFunctor = std::function<void(RendererPtr)>;
  39. private:
  40. CBattleInterface * owner;
  41. struct RenderableInstance
  42. {
  43. RenderFunctor functor;
  44. EBattleFieldLayer layer;
  45. BattleHex tile;
  46. };
  47. std::vector<RenderableInstance> objects;
  48. void collectObjects();
  49. void sortObjects();
  50. void renderObjects(RendererPtr targetCanvas);
  51. public:
  52. CBattleFieldRenderer(CBattleInterface * owner);
  53. void insert(EBattleFieldLayer layer, BattleHex tile, RenderFunctor functor);
  54. void execute(RendererPtr targetCanvas);
  55. };
  56. class CBattleFieldController : public CIntObject
  57. {
  58. CBattleInterface * owner;
  59. std::shared_ptr<IImage> background;
  60. std::shared_ptr<IImage> cellBorder;
  61. std::shared_ptr<IImage> cellShade;
  62. std::shared_ptr<CCanvas> cellBorders;
  63. /// Canvas that contains background, hex grid (if enabled), absolute obstacles and movement range of active stack
  64. std::shared_ptr<CCanvas> backgroundWithHexes;
  65. //BattleHex previouslyHoveredHex; //number of hex that was hovered by the cursor a while ago
  66. //BattleHex currentlyHoveredHex; //number of hex that is supposed to be hovered (for a while it may be inappropriately set, but will be renewed soon)
  67. BattleHex attackingHex; //hex from which the stack would perform attack with current cursor
  68. std::vector<BattleHex> occupyableHexes; //hexes available for active stack
  69. std::array<bool, GameConstants::BFIELD_SIZE> stackCountOutsideHexes; // hexes that when in front of a unit cause it's amount box to move back
  70. std::vector<std::shared_ptr<CClickableHex>> bfield; //11 lines, 17 hexes on each
  71. void showHighlightedHex(std::shared_ptr<CCanvas> to, BattleHex hex, bool darkBorder);
  72. std::set<BattleHex> getHighlightedHexesStackRange();
  73. std::set<BattleHex> getHighlightedHexesSpellRange();
  74. void showBackground(std::shared_ptr<CCanvas> canvas);
  75. void showBackgroundImage(std::shared_ptr<CCanvas> canvas);
  76. void showBackgroundImageWithHexes(std::shared_ptr<CCanvas> canvas);
  77. void showHighlightedHexes(std::shared_ptr<CCanvas> canvas);
  78. public:
  79. CBattleFieldController(CBattleInterface * owner);
  80. void redrawBackgroundWithHexes();
  81. void renderBattlefield(std::shared_ptr<CCanvas> canvas);
  82. Rect hexPositionLocal(BattleHex hex) const;
  83. Rect hexPosition(BattleHex hex) const;
  84. bool isPixelInHex(Point const & position);
  85. BattleHex getHoveredHex();
  86. void setBattleCursor(BattleHex myNumber);
  87. BattleHex fromWhichHexAttack(BattleHex myNumber);
  88. bool isTileAttackable(const BattleHex & number) const;
  89. bool stackCountOutsideHex(const BattleHex & number) const;
  90. };