TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_tile_system.hpp
1 
9 #ifndef TurtleBrains_TileSystem_hpp
10 #define TurtleBrains_TileSystem_hpp
11 
12 #include <turtle_brains/core/tb_string.hpp>
13 #include <turtle_brains/core/tb_dynamic_structure.hpp>
14 #include <turtle_brains/graphics/tb_graphic_list.hpp>
15 #include <turtle_brains/graphics/tb_sprite_map.hpp>
16 #include <turtle_brains/math/tb_vector.hpp>
17 
18 #include <vector>
19 #include <map>
20 
21 namespace TurtleBrains::Graphics::Implementation { class TileSystemRenderer; };
22 namespace TurtleBrains::Game::Unstable { class TileSystemCollider; };
23 namespace TurtleBrains::Math::Unstable { class BoundingVolume; };
24 
25 namespace TurtleBrains::Game
26 {
27  class Entity; //Forward declare for MoveEntity method.
28 
36 
40  extern const TileIndex kInvalidTileIndex;
41 
46 
47  extern const TileLocation kInvalidTileLocation;
48 
50 
51  extern const TileSetIndex kInvalidTileSetIndex;
52 
56  struct AboutTile
57  {
61  String mTileSetName;
62  String mLayerName;
63 
64  float mCenterX;
65  float mCenterY;
66  float mTop;
67  float mLeft;
68  float mWidth;
69  float mHeight;
71  };
72 
73  typedef std::vector<AboutTile> AboutTileContainer;
74 
85  {
86  public:
87 
91  TileSystem(void);
92 
96  virtual ~TileSystem(void);
97 
102  void ClearMap(void);
103 
107  String GetMapPropertyAsString(const String& propertyName) const;
108 
112  int GetMapPropertyAsInteger(const String& propertyName) const;
113 
117  bool GetMapPropertyAsBoolean(const String& propertyName) const;
118 
122  void SetPropertiesForMap(const tbCore::DynamicStructure& mapProperties);
123 
127  void AddTileSet(const String& tilesetName, const tbGraphics::SpriteMap& spriteMap,
129 
133 // void SetFlagsForTile(const String& tileSetName, const TileIndex& tileIndex, const TileFlags& tileFlags);
134 
138  AboutTileContainer FindTilesWithProperty(const String& tileProperty, bool onlyVisibleLayers = false) const;
139 
143  AboutTileContainer FindTilesWithPropertyEquals(const String& tileProperty, bool propertyValue, bool onlyVisibleLayers = false) const;
144 
148  AboutTileContainer FindTilesWithPropertyEquals(const String& tileProperty, int propertyValue, bool onlyVisibleLayers = false) const;
149 
153  AboutTileContainer FindTilesWithPropertyEquals(const String& tileProperty, const String& propertyValue, bool onlyVisibleLayers = false) const;
154 
158  void SetPropertiesForTile(const String& tileSetName, const TileIndex& tileIndex, const tbCore::DynamicStructure& tileProperties);
159 
163  String GetTilePropertyAsString(const String& tileSetName, const TileIndex& tileIndex, const String& propertyName) const;
164 
168  int GetTilePropertyAsInteger(const String& tileSetName, const TileIndex& tileIndex, const String& propertyName) const;
169 
173  bool GetTilePropertyAsBoolean(const String& tileSetName, const TileIndex& tileIndex, const String& propertyName) const;
174 
178  void SetTileProperty(const String& tileSetName, const TileIndex& tileIndex,
179  const String& propertyName, const String& propertyValue);
180 
184  void SetTileProperty(const String& tileSetName, const TileIndex& tileIndex,
185  const String& propertyName, const int propertyValue);
186 
190  void SetTileProperty(const String& tileSetName, const TileIndex& tileIndex,
191  const String& propertyName, const bool propertyValue);
192 
196  void AddTileLayer(const String& tileLayerName, const std::vector<TileIndex>& tileData,
197  const String& tileSetName, const TileLocation& columnCount, const TileLocation& rowCount);
198 
202  void AddTileLayer(const String& tileLayerName, const std::vector<TileIndex>& tileData,
203  const std::vector<TileSetIndex>& tileSetData, const TileLocation& columnCount, const TileLocation& rowCount);
204 
208  void SetLayerVisible(const String& tileLayerName, const bool isVisible);
209 
213  void SetTile(const String& tileLayerName, const TileLocation& tileColumn, const TileLocation& tileRow,
214  const TileIndex& toTileIndex, const TileSetIndex& toTileSetIndex = kInvalidTileSetIndex);
215 
219  //
220  //
221  //
222  //bool Move(const tbGame::Entity& entity, const tbMath::Vector2& from, tbMath::Vector2& to);
223  //bool Move(const tbMath::Unstable::BoundingVolume& boundingVolume, const tbMath::Vector2& centerFrom, tbMath::Vector2& centerTo)
224 
228  bool MoveEntity(const tbMath::Vector2& currentPosition, tbMath::Vector2& finalPosition,
229  const TurtleBrains::Math::Unstable::BoundingVolume& boundingVolume) const;
230 
234  bool MoveEntity(Entity& entity, const tbMath::Vector2& fromOldPosition, const tbMath::Vector2& toNewPosition) const;
235 
239  bool IsPointInSolid(const tbMath::Vector2& pointPosition) const;
240 
245 
247 // TileLocation PixelSpaceToColumn(const float locationX) const;
248 // TileLocation PixelSpaceToRow(const float locationY) const;
249 // float ColumnToPixelSpace(const TileLocation& column) const;
250 // float RowToPixelSpace(const TileLocation& row) const;
252 
253  protected:
254 
258  virtual void OnRender(void) const override;
259 
260  private:
261  const tbCore::DynamicStructure& GetTileProperties(const String& tileSetName, const TileIndex& tileIndex) const;
262  tbCore::DynamicStructure& GetTileProperties(const String& tileSetName, const TileIndex& tileIndex);
263 
264  struct TileLayer
265  {
266  tbMath::Vector2 mPosition;
267  TileLocation mTileColumns;
268  TileLocation mTileRows;
269  typedef std::vector<TileIndex> TileContainer;
270  TileContainer mTileData; //(TileIndex)-1 = no tile, 0 based on the TileSetData it lies upon.
271  typedef std::vector<TileSetIndex> TileSetContainer;
272  TileSetContainer mTileSetData; //Will be size 1 for a single Tileset, index into ordered names. Not yet implemented ...
273 
274  bool mIsVisible;
275  };
276 
277  struct TileSet
278  {
279  TileSet(const tbGraphics::SpriteMap& spriteMap);
280 
281  tbGraphics::SpriteMap mSpriteMap;
282  //TileIndex mStartTileID; //May be unneeded.
283  //TileIndex mFinalTileID; //May be unneeded.
284 
285  //Stored as the index of the tile in this tile set, so 0 would be top-left most tile in SpriteMap.
286 // typedef std::map<tbCore::uint16, std::pair<TileFlags, tbCore::DynamicStructure> > TilePropertyTable;
287  typedef std::map<tbCore::uint16, tbCore::DynamicStructure> TilePropertyTable;
288  TilePropertyTable mTileProperties;
289  tbCore::DynamicStructure mTileSetProperties;
290  };
291 
292  typedef std::vector<String> NameContainer;
293  typedef std::map<String, TileLayer> TileLayerTable;
294  TileLayerTable mTileLayers;
295  NameContainer mOrderedLayerNames;
296  NameContainer mOrderedTileSetNames;
297 
298  typedef std::map<String, TileSet> TileSetTable;
299  TileSetTable mTileSets;
300  tbCore::DynamicStructure mMapProperties;
301 
304  typedef std::map<String, TurtleBrains::Graphics::Implementation::TileSystemRenderer*> LayerRendererTable;
305  mutable LayerRendererTable mLayerRenderers;
306 
307  TurtleBrains::Game::Unstable::TileSystemCollider* mCollider;
308  };
309 
310 }; /* namespace TurtleBrains::Game */
311 
312 namespace tbGame = TurtleBrains::Game;
313 
314 #endif /* TurtleBrains_TileSystem_hpp */
Definition: tb_dynamic_structure.hpp:95
static const DynamicStructure kNullValue
Definition: tb_dynamic_structure.hpp:908
Definition: tb_noncopyable.hpp:21
Definition: tb_entity.hpp:47
Definition: tb_tile_system.hpp:85
void SetLayerVisible(const String &tileLayerName, const bool isVisible)
bool IsPointInSolid(const tbMath::Vector2 &pointPosition) const
virtual void OnRender(void) const override
void SetTileProperty(const String &tileSetName, const TileIndex &tileIndex, const String &propertyName, const int propertyValue)
void AddTileSet(const String &tilesetName, const tbGraphics::SpriteMap &spriteMap, const tbCore::DynamicStructure &tileSetProperties=tbCore::DynamicStructure::kNullValue)
AboutTileContainer FindTilesWithPropertyEquals(const String &tileProperty, int propertyValue, bool onlyVisibleLayers=false) const
void AddTileLayer(const String &tileLayerName, const std::vector< TileIndex > &tileData, const std::vector< TileSetIndex > &tileSetData, const TileLocation &columnCount, const TileLocation &rowCount)
String GetTilePropertyAsString(const String &tileSetName, const TileIndex &tileIndex, const String &propertyName) const
bool GetTilePropertyAsBoolean(const String &tileSetName, const TileIndex &tileIndex, const String &propertyName) const
AboutTileContainer FindTilesWithProperty(const String &tileProperty, bool onlyVisibleLayers=false) const
void AddTileLayer(const String &tileLayerName, const std::vector< TileIndex > &tileData, const String &tileSetName, const TileLocation &columnCount, const TileLocation &rowCount)
AboutTileContainer FindTilesWithPropertyEquals(const String &tileProperty, const String &propertyValue, bool onlyVisibleLayers=false) const
void SetTile(const String &tileLayerName, const TileLocation &tileColumn, const TileLocation &tileRow, const TileIndex &toTileIndex, const TileSetIndex &toTileSetIndex=kInvalidTileSetIndex)
int GetMapPropertyAsInteger(const String &propertyName) const
void SetTileProperty(const String &tileSetName, const TileIndex &tileIndex, const String &propertyName, const bool propertyValue)
void SetTileProperty(const String &tileSetName, const TileIndex &tileIndex, const String &propertyName, const String &propertyValue)
AboutTileContainer FindTilesWithPropertyEquals(const String &tileProperty, bool propertyValue, bool onlyVisibleLayers=false) const
void SetPropertiesForMap(const tbCore::DynamicStructure &mapProperties)
bool MoveEntity(Entity &entity, const tbMath::Vector2 &fromOldPosition, const tbMath::Vector2 &toNewPosition) const
bool MoveEntity(const tbMath::Vector2 &currentPosition, tbMath::Vector2 &finalPosition, const TurtleBrains::Math::Unstable::BoundingVolume &boundingVolume) const
void SetPropertiesForTile(const String &tileSetName, const TileIndex &tileIndex, const tbCore::DynamicStructure &tileProperties)
int GetTilePropertyAsInteger(const String &tileSetName, const TileIndex &tileIndex, const String &propertyName) const
bool GetMapPropertyAsBoolean(const String &propertyName) const
String GetMapPropertyAsString(const String &propertyName) const
Definition: tb_graphic.hpp:50
Definition: tb_sprite_map.hpp:22
Definition: tb_vector.hpp:99
Definition: tbu_bounding_volume.hpp:25
std::uint8_t uint8
Unsigned integer with a size of 8 bits. Supports values from 0 to 255.
Definition: tb_types.hpp:22
std::uint16_t uint16
Unsigned integer with a size of 16 bits. Supports values from 0 to 65535.
Definition: tb_types.hpp:25
This is the heart of TurtleBrains for game developers to create GameScenes and Entities to interact w...
tbCore::uint16 TileLocation
Definition: tb_tile_system.hpp:45
std::vector< AboutTile > AboutTileContainer
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:73
const TileLocation kInvalidTileLocation
TODO: TurtleBrains: Documentation: Teach the user how to use this.
const TileSetIndex kInvalidTileSetIndex
TODO: TurtleBrains: Documentation: Teach the user how to use this.
tbCore::uint8 TileSetIndex
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:49
tbCore::uint16 TileIndex
Definition: tb_tile_system.hpp:27
const TileIndex kInvalidTileIndex
Definition: tb_tile_system.hpp:57
tbCore::DynamicStructure mPropertyValue
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:70
float mCenterX
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:64
float mLeft
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:67
float mTop
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:66
TileLocation mRow
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:58
String mLayerName
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:62
TileIndex mTileIndex
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:60
float mWidth
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:68
String mTileSetName
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:61
float mHeight
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:69
float mCenterY
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:65
TileLocation mColumn
TODO: TurtleBrains: Documentation: Teach the user how to use this.
Definition: tb_tile_system.hpp:59