ObstacleHandler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * ObstacleHandler.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 <vcmi/EntityService.h>
  12. #include <vcmi/Entity.h>
  13. #include "GameConstants.h"
  14. #include "IHandlerBase.h"
  15. #include "Terrain.h"
  16. #include "battle/BattleHex.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class DLL_LINKAGE ObstacleInfo : public EntityT<Obstacle>
  19. {
  20. public:
  21. ObstacleInfo(): obstacle(-1), width(0), height(0), isAbsoluteObstacle(false), iconIndex(0)
  22. {}
  23. ObstacleInfo(Obstacle obstacle, std::string identifier)
  24. : obstacle(obstacle), identifier(identifier), iconIndex(obstacle.getNum()), width(0), height(0), isAbsoluteObstacle(false)
  25. {
  26. }
  27. Obstacle obstacle;
  28. si32 iconIndex;
  29. std::string identifier;
  30. std::string appearSound, appearAnimation, triggerAnimation, triggerSound, animation;
  31. std::vector<TerrainId> allowedTerrains;
  32. std::vector<std::string> allowedSpecialBfields;
  33. //TODO: here is extra field to implement it's logic in the future but save backward compatibility
  34. int obstacleType = -1;
  35. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  36. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  37. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  38. int32_t getIndex() const override;
  39. int32_t getIconIndex() const override;
  40. const std::string & getJsonKey() const override;
  41. const std::string & getName() const override;
  42. void registerIcons(const IconRegistar & cb) const override;
  43. Obstacle getId() const override;
  44. std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
  45. bool isAppropriate(const TerrainId terrainType, const BattleField & specialBattlefield) const;
  46. template <typename Handler> void serialize(Handler &h, const int version)
  47. {
  48. h & obstacle;
  49. h & obstacleType;
  50. h & iconIndex;
  51. h & identifier;
  52. h & animation;
  53. h & appearSound;
  54. h & appearAnimation;
  55. h & triggerSound;
  56. h & triggerAnimation;
  57. h & allowedTerrains;
  58. h & allowedSpecialBfields;
  59. h & isAbsoluteObstacle;
  60. h & width;
  61. h & height;
  62. h & blockedTiles;
  63. }
  64. };
  65. class DLL_LINKAGE ObstacleService : public EntityServiceT<Obstacle, ObstacleInfo>
  66. {
  67. public:
  68. };
  69. class ObstacleHandler: public CHandlerBase<Obstacle, ObstacleInfo, ObstacleInfo, ObstacleService>
  70. {
  71. public:
  72. ObstacleInfo * loadFromJson(const std::string & scope,
  73. const JsonNode & json,
  74. const std::string & identifier,
  75. size_t index) override;
  76. const std::vector<std::string> & getTypeNames() const override;
  77. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  78. std::vector<bool> getDefaultAllowed() const override;
  79. template <typename Handler> void serialize(Handler & h, const int version)
  80. {
  81. h & objects;
  82. }
  83. };
  84. VCMI_LIB_NAMESPACE_END