Merge branch 'cache'
[Tsunagari.git] / src / area.h
blob94413b39e5c753828052d3b694bdbeb709487a91
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 <Gosu/Graphics.hpp> // for Gosu::Transform
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
17 #include "common.h"
18 #include "resourcer.h"
20 namespace Gosu {
21 class Bitmap;
22 class Button;
23 class Image;
24 class Sample;
25 class SampleInstance;
28 class Entity;
29 class GameWindow;
30 class Resourcer;
31 class Sprite;
32 class World;
34 //! This class is responsible for each map, or area in a Tsunagari World.
35 /*!
36 The Area class handles the parsing of TMX-format Area descriptor files,
37 the initialization, placement, and drawing of tiles, and various
38 Area-related functionality.
40 class Area
42 public:
44 // when changing TileFlags, be sure to make updates to Area::splitTileFlags()
45 //! List of possible flags that can be attached to a tile.
46 /*!
47 Flags are attached to tiles, and denote special behavior for
48 the tile they are bound to.
50 enum TileFlags {
51 nowalk = 0x0001,
52 player_nowalk = 0x0002,
53 npc_nowalk = 0x0004,
54 player_event = 0x0008,
55 npc_event = 0x0010,
56 temp_event = 0x0020
59 //! List of possible triggers for tile events.
60 /*!
61 Triggers describe the conditions for the activation of a
62 tile-bound event script funtion.
64 enum TileEventTriggers {
65 onUse,
66 onEnter,
67 onLeave,
68 door
71 //! Stores info for an event attached to a tile.
72 /*!
73 Events are attached to tiles, and parsed into this struct from a
74 TMX-format area descriptor file. The event is executed when the
75 condition for its trigger is met. The event function name and
76 the function's arguments are stored in argv.
78 struct TileEvent {
79 TileEventTriggers trigger;
80 std::string argv; // Function name and arguments.
83 //! Convenience trigger for inter-area teleportation.
84 /*!
85 Tiles with a door trigger attached can teleport the player to a
86 new area in the World. The Door struct contains the destination
87 area and coordinates.
89 struct Door {
90 std::string area;
91 coord_t coord;
94 //! Contains the properties shared by all tiles of a certain type.
95 /*!
96 This struct contains global tile properties for a tile of a
97 certain type. As opposed to local properties for a single tile,
98 all tiles of this type will share the defined characteristics.
100 struct TileType {
101 std::vector<Gosu::Image*> graphics;
102 bool animated; // Is the tile animated?
103 double ani_speed; // Speed of animation in hertz
104 std::vector<TileEvent> events;
105 unsigned flags; // bitflags for each option in TileFlags enum
106 // TODO: Door* door
109 //! Contains properties unique to this tile.
111 This struct contains local tile properties for a single tile in
112 the area. As opposed to global properties which apply to all
113 tiles of the same type, these properties will only apply to one
114 tile.
116 struct Tile {
117 TileType* type;
118 std::vector<TileEvent> events;
119 unsigned flags; // bitflags for each option in TileFlags enum
120 Door* door;
124 //! Area Constructor
125 Area(Resourcer* rc, World* world, Entity* player, const std::string& filename);
127 //! Area Destructor
128 ~Area();
130 //! Function that must be called after the constructor.
131 bool init();
133 //! Gosu Callback
134 void buttonDown(const Gosu::Button btn);
136 //! Gosu Callback
137 void draw();
139 //! Gosu Callback
140 bool needsRedraw() const;
142 coord_t getDimensions() const;
143 Tile* getTile(coord_t c);
145 private:
146 //! Tileset
148 Stores info for a tileset, and global settings for tiles.
150 struct Tileset {
151 Gosu::Bitmap* source; // TODO: make shared_ptr for memory sureness, also delete after done loading tileset
152 coord_t tiledim; // Dimensions per tile
153 std::vector<TileType> defaults; // Global tile properties
156 //! Music
158 Stores info for the intro or main music files.
160 struct Music {
161 bool loop;
162 std::string filename;
165 //! XML descriptor parsing function.
166 bool processDescriptor();
168 //! XML descriptor parsing function.
169 bool processMapProperties(xmlNode* node);
171 //! Constructs a tile of default type.
172 TileType defaultTileType(const Gosu::Bitmap* source, coord_t tiledim,
173 int id);
175 //! XML descriptor parsing function.
176 bool processTileset(xmlNode* node);
178 //! XML descriptor parsing function.
179 bool processTileType(xmlNode* node, Tileset& ts);
181 //! XML descriptor parsing function.
182 bool processLayer(xmlNode* node);
184 //! XML descriptor parsing function.
185 bool processLayerProperties(xmlNode* node);
187 //! XML descriptor parsing function.
188 bool processLayerData(xmlNode* node);
190 //! XML descriptor parsing function.
191 bool processObjectGroup(xmlNode* node);
193 //! XML descriptor parsing function.
194 bool processObjectGroupProperties(xmlNode* node, int* zpos);
196 //! XML descriptor parsing function.
197 bool processObject(xmlNode* node, int zpos);
199 //! Split a tile's flags into individuals.
200 unsigned splitTileFlags(const std::string strOfFlags);
202 //! Process a door convenience trigger.
203 Door* parseDoor(const std::string dest);
205 Gosu::Transform translateCoords();
208 Resourcer* rc;
209 World* world;
210 Entity* player;
211 const std::string descriptor;
212 SampleRef music_buf;
213 Gosu::SampleInstance* music_inst;
215 typedef std::vector<Tile*> row_t;
216 typedef std::vector<row_t> grid_t;
217 typedef std::vector<grid_t> tilematrix_t;
219 /* All layers in the map must be in the range of [0, n]. There cannot be
220 * any gaps.
222 tilematrix_t map;
223 coord_t dim;
225 std::string name;
226 std::string author;
227 std::vector<Tileset> tilesets;
228 Music music_intro;
229 Music music_main;
230 std::string scripts;
231 std::string onLoadEvents;
234 #endif