tmxparser  2.1.0
 All Classes Functions Variables Pages
TmxTile.h
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2010-2014, Tamir Atias
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 //-----------------------------------------------------------------------------
24 #pragma once
25 
26 #include <vector>
27 
28 #include "TmxPropertySet.h"
29 #include "TmxImage.h"
30 #include "TmxObjectGroup.h"
31 #include <stdexcept>
32 #include <string>
33 
34 namespace tinyxml2
35 {
36  class XMLNode;
37 }
38 
39 namespace Tmx
40 {
41  class AnimationFrame;
42  class Object;
43 
44  //-------------------------------------------------------------------------
50  //-------------------------------------------------------------------------
51  class Tile
52  {
53  public:
55  Tile(int id);
56  Tile();
57  ~Tile();
58 
60  void Parse(const tinyxml2::XMLNode *tileNode);
61 
63  int GetId() const
64  {
65  return id;
66  }
67 
69  bool IsAnimated() const
70  {
71  return isAnimated;
72  }
73 
75  int GetFrameCount() const
76  {
77  return frames.size();
78  }
79 
82  unsigned int GetTotalDuration() const
83  {
84  return totalDuration;
85  }
86 
88  const Tmx::Image* GetImage() const
89  {
90  return image;
91  }
92 
94  std::string GetType() const
95  {
96  return type;
97  }
98 
100  const std::vector<AnimationFrame> &GetFrames() const
101  {
102  return frames;
103  }
104 
107  {
108  return properties;
109  }
110 
112  const Tmx::ObjectGroup *GetObjectGroup() const
113  {
114  return objectGroup;
115  }
116 
118  const Tmx::PropertySet &GetObjectGroupProperties() const
119  {
120  if (!objectGroup) throw std::runtime_error ("Tile has no ObjectGroup on attempt to get ObjectGroup properties. Cannot return null ref.");
121  return objectGroup->GetProperties();
122  }
123 
125  std::vector<Tmx::Object*> GetObjects() const
126  {
127  if (!objectGroup) throw std::out_of_range ("Tile has no objectGroup");
128  return objectGroup->GetObjects();
129  }
130 
132  bool HasObjects() const
133  {
134  return hasObjects;
135  }
136 
138  const Tmx::Object *GetObject(int index) const
139  {
140  if (!objectGroup) throw std::out_of_range ("Tile has no objectGroup");
141  return objectGroup->GetObject(index);
142  }
143 
145  int GetNumObjects() const
146  {
147  if (!objectGroup) throw std::out_of_range ("Tile has no objectGroup");
148  return objectGroup->GetNumObjects();
149  }
150 
151  private:
152  int id;
153 
154  Tmx::PropertySet properties;
155 
156  bool isAnimated;
157  bool hasObjects;
158  bool hasObjectGroup;
159  Tmx::ObjectGroup *objectGroup;
160  unsigned int totalDuration;
161  std::vector<AnimationFrame> frames;
162  Tmx::Image* image;
163  std::string type;
164  };
165 
166  //-------------------------------------------------------------------------
170  //-------------------------------------------------------------------------
172  {
173  public:
176  tileID(-1), duration(0)
177  {
178  }
179 
181  AnimationFrame(int tileID, unsigned int duration) :
182  tileID(tileID), duration(duration)
183  {
184  }
185 
187  int GetTileID() const
188  {
189  return tileID;
190  }
191 
193  unsigned int GetDuration() const
194  {
195  return duration;
196  }
197 
198  private:
199  int tileID;
200  unsigned int duration;
201  };
202 }
bool HasObjects() const
Returns true if tile has Collision Objects.
Definition: TmxTile.h:132
const Tmx::Object * GetObject(int index) const
Get a single object.
Definition: TmxObjectGroup.h:63
const Tmx::PropertySet & GetProperties() const
Get a set of properties regarding the tile.
Definition: TmxTile.h:106
const Tmx::PropertySet & GetProperties() const
Get the property set.
Definition: TmxObjectGroup.h:75
A class used for holding a list of objects.
Definition: TmxObjectGroup.h:48
std::vector< Tmx::Object * > GetObjects() const
Get set of Collision Objects, convenience function.
Definition: TmxTile.h:125
void Parse(const tinyxml2::XMLNode *tileNode)
Parse a tile node.
Definition: TmxTile.cpp:58
const Tmx::Image * GetImage() const
Returns the tile image if defined.
Definition: TmxTile.h:88
unsigned int GetTotalDuration() const
Returns the total duration of the animation, in milliseconds, or 0 if the tile is not animated...
Definition: TmxTile.h:82
int GetId() const
Get the Id. (relative to the tileset)
Definition: TmxTile.h:63
bool IsAnimated() const
Returns true if the tile is animated (has one or more animation frames)
Definition: TmxTile.h:69
int GetNumObjects() const
Get the number of objects in the list.
Definition: TmxTile.h:145
This class contains a map of properties.
Definition: TmxPropertySet.h:47
Class used for representing a single object from the objectgroup.
Definition: TmxObject.h:48
const Tmx::Object * GetObject(int index) const
Get a single object.
Definition: TmxTile.h:138
const std::vector< Tmx::Object * > & GetObjects() const
Get the whole list of objects.
Definition: TmxObjectGroup.h:72
unsigned int GetDuration() const
Get the duration of this frame in milliseconds.
Definition: TmxTile.h:193
Class containing information about an animated tile.
Definition: TmxTile.h:171
int GetTileID() const
Get the tile id of this frame, relative to the containing tileset.
Definition: TmxTile.h:187
AnimationFrame()
This constructor shouldn't be used, ideally.
Definition: TmxTile.h:175
const std::vector< AnimationFrame > & GetFrames() const
Returns the frames of the animation.
Definition: TmxTile.h:100
Class to contain information about every tile in the tileset/tiles element.
Definition: TmxTile.h:51
std::string GetType() const
Returns the object type of the tile.
Definition: TmxTile.h:94
int GetFrameCount() const
Returns the number of frames of the animation. If the tile is not animated, returns 0...
Definition: TmxTile.h:75
A class used for storing information about an image within a tileset.
Definition: TmxImage.h:42
AnimationFrame(int tileID, unsigned int duration)
Create a new animation frame with a specified tile id and duration.
Definition: TmxTile.h:181
int GetNumObjects() const
Get the number of objects in the list.
Definition: TmxObjectGroup.h:66