1 /******************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
13 #include <boost/optional.hpp>
14 #include <boost/scoped_ptr.hpp>
15 #include <Gosu/Audio.hpp> // for Gosu::SampleInstance
16 #include <Gosu/Graphics.hpp> // for Gosu::Transform
17 #include <libxml/tree.h>
19 #include "animation.h"
22 #include "resourcer.h"
38 //! This class is responsible for each map, or area in a Tsunagari World.
40 The Area class handles the parsing of TMX-format Area descriptor files,
41 the initialization, placement, and drawing of tiles, and various
42 Area-related functionality.
48 // when changing TileFlags, be sure to make updates to
49 // Area::splitTileFlags()
50 //! List of possible flags that can be attached to a tile.
52 Flags are attached to tiles, and denote special behavior for
53 the tile they are bound to.
57 player_nowalk
= 0x0002,
59 player_event
= 0x0008,
64 //! List of possible triggers for tile events.
66 Triggers describe the conditions for the activation of a
67 tile-bound event script funtion.
69 enum TileEventTriggers
{
76 //! Stores info for an event attached to a tile.
78 Events are attached to tiles, and parsed into this struct from a
79 TMX-format area descriptor file. The event is executed when the
80 condition for its trigger is met. The event function name and
81 the function's arguments are stored in argv.
84 TileEventTriggers trigger
;
85 std::string argv
; // Function name and arguments.
88 //! Convenience trigger for inter-area teleportation.
90 Tiles with a door trigger attached can teleport the player to a
91 new area in the World. The Door struct contains the destination
99 //! Contains the properties shared by all tiles of a certain type.
101 This struct contains global tile properties for a tile of a
102 certain type. As opposed to local properties for a single tile,
103 all tiles of this type will share the defined characteristics.
107 Animation anim
; // Graphics for tiles of this type.
108 std::vector
<TileEvent
> events
;
109 std::vector
<Tile
*> allOfType
;
110 unsigned flags
; // bitflags for each option in TileFlags enum
111 // TODO: boost::scoped_ptr<Door> door
114 //! Contains properties unique to this tile.
116 This struct contains local tile properties for a single tile in
117 the area. As opposed to global properties which apply to all
118 tiles of the same type, these properties will only apply to one
123 std::vector
<TileEvent
> events
;
124 unsigned flags
; // bitflags for each option in TileFlags enum
125 boost::optional
<Door
> door
;
130 Area(Resourcer
* rc
, World
* world
, Player
* player
,
131 const std::string
& filename
);
136 //! Function that must be called after the constructor.
140 void buttonDown(const Gosu::Button btn
);
141 void buttonUp(const Gosu::Button btn
);
147 bool needsRedraw() const;
150 void update(unsigned long dt
);
152 coord_t
getDimensions() const;
153 coord_t
getTileDimensions() const;
154 const Tile
& getTile(coord_t c
) const;
155 Tile
& getTile(coord_t c
);
160 Stores info for a tileset, and global settings for tiles.
164 coord_t tileDim
; // Dimensions per tile
165 std::vector
<TileType
> tileTypes
; // Global tile properties
170 Stores info for the intro or main music files.
174 std::string filename
;
180 const coord_t
viewportOffset() const;
181 const Gosu::Transform
viewportTransform() const;
183 cube_t
visibleTiles() const;
184 bool tileTypeOnScreen(const TileType
& type
) const;
186 //! XML descriptor parsing function.
187 bool processDescriptor();
189 //! XML descriptor parsing function.
190 bool processMapProperties(xmlNode
* node
);
192 //! XML descriptor parsing function.
193 bool processTileSet(xmlNode
* node
);
195 //! Constructs a tile of default type.
196 TileType
defaultTileType(TileSet
& set
);
198 //! XML descriptor parsing function.
199 bool processTileType(xmlNode
* node
, TileSet
& ts
);
201 //! XML descriptor parsing function.
202 bool processLayer(xmlNode
* node
);
204 //! XML descriptor parsing function.
205 bool processLayerProperties(xmlNode
* node
);
207 //! XML descriptor parsing function.
208 bool processLayerData(xmlNode
* node
);
210 //! XML descriptor parsing function.
211 bool processObjectGroup(xmlNode
* node
);
213 //! XML descriptor parsing function.
214 bool processObjectGroupProperties(xmlNode
* node
, int* zpos
);
216 //! XML descriptor parsing function.
217 bool processObject(xmlNode
* node
, int zpos
);
219 //! Split a tile's flags into individuals.
220 unsigned splitTileFlags(const std::string strOfFlags
);
222 //! Process a door convenience trigger.
223 Door
parseDoor(const std::string dest
);
229 const std::string descriptor
;
232 SampleRef introMusic
, mainMusic
;
233 boost::optional
<Gosu::SampleInstance
> musicInst
;
235 typedef std::vector
<Tile
> row_t
;
236 typedef std::vector
<row_t
> grid_t
;
237 typedef std::vector
<grid_t
> tilematrix_t
;
239 /* All layers in the map must be in the range of [0, n]. There cannot be
247 std::vector
<TileSet
> tilesets
;
249 std::string onLoadEvents
;