ObstacleHandler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "battle/BattleHex.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class DLL_LINKAGE ObstacleInfo : public EntityT<Obstacle>
  18. {
  19. public:
  20. ObstacleInfo(): obstacle(-1), width(0), height(0), isAbsoluteObstacle(false), iconIndex(0)
  21. {}
  22. ObstacleInfo(Obstacle obstacle, std::string identifier)
  23. : obstacle(obstacle), identifier(identifier), iconIndex(obstacle.getNum()), width(0), height(0), isAbsoluteObstacle(false)
  24. {
  25. }
  26. Obstacle obstacle;
  27. si32 iconIndex;
  28. std::string identifier;
  29. std::string appearSound, appearAnimation, triggerAnimation, triggerSound, animation;
  30. std::vector<TerrainId> allowedTerrains;
  31. std::vector<std::string> allowedSpecialBfields;
  32. //TODO: here is extra field to implement it's logic in the future but save backward compatibility
  33. int obstacleType = -1;
  34. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  35. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  36. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  37. int32_t getIndex() const override;
  38. int32_t getIconIndex() const override;
  39. std::string getJsonKey() const override;
  40. std::string getNameTranslated() const override;
  41. std::string getNameTextID() 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 & appearAnimation;
  54. h & triggerAnimation;
  55. if (version > 806)
  56. {
  57. h & appearSound;
  58. h & triggerSound;
  59. }
  60. h & allowedTerrains;
  61. h & allowedSpecialBfields;
  62. h & isAbsoluteObstacle;
  63. h & width;
  64. h & height;
  65. h & blockedTiles;
  66. }
  67. };
  68. class DLL_LINKAGE ObstacleService : public EntityServiceT<Obstacle, ObstacleInfo>
  69. {
  70. public:
  71. };
  72. class ObstacleHandler: public CHandlerBase<Obstacle, ObstacleInfo, ObstacleInfo, ObstacleService>
  73. {
  74. public:
  75. ObstacleInfo * loadFromJson(const std::string & scope,
  76. const JsonNode & json,
  77. const std::string & identifier,
  78. size_t index) override;
  79. const std::vector<std::string> & getTypeNames() const override;
  80. std::vector<JsonNode> loadLegacyData() override;
  81. std::vector<bool> getDefaultAllowed() const override;
  82. template <typename Handler> void serialize(Handler & h, const int version)
  83. {
  84. h & objects;
  85. }
  86. };
  87. VCMI_LIB_NAMESPACE_END