factored generic Animation class out of Area::TileType
[Tsunagari.git] / src / area.h
blob64103e1a3fbc514f13748b63d8149c0262a0cebc
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** area.h **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #ifndef AREA_H
8 #define AREA_H
10 #include <string>
11 #include <vector>
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"
20 #include "common.h"
21 #include "player.h"
22 #include "resourcer.h"
24 namespace Gosu {
25 class Bitmap;
26 class Button;
27 class Image;
28 class Sample;
29 class SampleInstance;
32 class Entity;
33 class GameWindow;
34 class Resourcer;
35 class Sprite;
36 class World;
38 //! This class is responsible for each map, or area in a Tsunagari World.
39 /*!
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.
44 class Area
46 public:
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.
51 /*!
52 Flags are attached to tiles, and denote special behavior for
53 the tile they are bound to.
55 enum TileFlags {
56 nowalk = 0x0001,
57 player_nowalk = 0x0002,
58 npc_nowalk = 0x0004,
59 player_event = 0x0008,
60 npc_event = 0x0010,
61 temp_event = 0x0020
64 //! List of possible triggers for tile events.
65 /*!
66 Triggers describe the conditions for the activation of a
67 tile-bound event script funtion.
69 enum TileEventTriggers {
70 onUse,
71 onEnter,
72 onLeave,
73 door
76 //! Stores info for an event attached to a tile.
77 /*!
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.
83 struct TileEvent {
84 TileEventTriggers trigger;
85 std::string argv; // Function name and arguments.
88 //! Convenience trigger for inter-area teleportation.
89 /*!
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
92 area and coordinates.
94 struct Door {
95 std::string area;
96 coord_t coord;
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.
105 struct Tile;
106 struct TileType {
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
119 tile.
121 struct Tile {
122 TileType* type;
123 std::vector<TileEvent> events;
124 unsigned flags; // bitflags for each option in TileFlags enum
125 boost::optional<Door> door;
129 //! Area Constructor
130 Area(Resourcer* rc, World* world, Player* player,
131 const std::string& filename);
133 //! Area Destructor
134 ~Area();
136 //! Function that must be called after the constructor.
137 bool init();
139 //! Gosu Callback
140 void buttonDown(const Gosu::Button btn);
141 void buttonUp(const Gosu::Button btn);
143 //! Gosu Callback
144 void draw();
146 //! Gosu Callback
147 bool needsRedraw() const;
149 //! Gosu Callback
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);
157 private:
158 //! TileSet
160 Stores info for a tileset, and global settings for tiles.
162 struct TileSet {
163 TiledImage tiles;
164 coord_t tileDim; // Dimensions per tile
165 std::vector<TileType> tileTypes; // Global tile properties
168 //! Music
170 Stores info for the intro or main music files.
172 struct Music {
173 bool loop;
174 std::string filename;
177 void drawTiles();
178 void drawEntities();
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);
226 Resourcer* rc;
227 World* world;
228 Player* player;
229 const std::string descriptor;
231 bool onIntro;
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
240 * any gaps.
242 tilematrix_t map;
243 coord_t dim;
245 std::string name;
246 std::string author;
247 std::vector<TileSet> tilesets;
248 std::string scripts;
249 std::string onLoadEvents;
252 #endif