Makefile: can make any world
[Tsunagari.git] / src / area.h
blobea3ff9c54ae1617de5048fd54c73e8ead28ab6fb
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/scoped_ptr.hpp>
14 #include <Gosu/Graphics.hpp> // for Gosu::Transform
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 //! Gosu Callback
143 void update();
145 coord_t getDimensions() const;
146 Tile* getTile(coord_t c);
148 private:
149 //! Tileset
151 Stores info for a tileset, and global settings for tiles.
153 struct Tileset {
154 Gosu::Bitmap* source; // TODO: make shared_ptr for memory sureness, also delete after done loading tileset
155 coord_t tiledim; // Dimensions per tile
156 std::vector<TileType> defaults; // Global tile properties
159 //! Music
161 Stores info for the intro or main music files.
163 struct Music {
164 bool loop;
165 std::string filename;
168 //! XML descriptor parsing function.
169 bool processDescriptor();
171 //! XML descriptor parsing function.
172 bool processMapProperties(xmlNode* node);
174 //! Constructs a tile of default type.
175 TileType defaultTileType(const Gosu::Bitmap* source, coord_t tiledim,
176 int id);
178 //! XML descriptor parsing function.
179 bool processTileset(xmlNode* node);
181 //! XML descriptor parsing function.
182 bool processTileType(xmlNode* node, Tileset& ts);
184 //! XML descriptor parsing function.
185 bool processLayer(xmlNode* node);
187 //! XML descriptor parsing function.
188 bool processLayerProperties(xmlNode* node);
190 //! XML descriptor parsing function.
191 bool processLayerData(xmlNode* node);
193 //! XML descriptor parsing function.
194 bool processObjectGroup(xmlNode* node);
196 //! XML descriptor parsing function.
197 bool processObjectGroupProperties(xmlNode* node, int* zpos);
199 //! XML descriptor parsing function.
200 bool processObject(xmlNode* node, int zpos);
202 //! Split a tile's flags into individuals.
203 unsigned splitTileFlags(const std::string strOfFlags);
205 //! Process a door convenience trigger.
206 Door* parseDoor(const std::string dest);
208 Gosu::Transform translateCoords();
211 Resourcer* rc;
212 World* world;
213 Entity* player;
214 const std::string descriptor;
216 bool onIntro;
217 SampleRef introMusic, mainMusic;
218 boost::scoped_ptr<Gosu::SampleInstance> musicInst;
220 typedef std::vector<Tile*> row_t;
221 typedef std::vector<row_t> grid_t;
222 typedef std::vector<grid_t> tilematrix_t;
224 /* All layers in the map must be in the range of [0, n]. There cannot be
225 * any gaps.
227 tilematrix_t map;
228 coord_t dim;
230 std::string name;
231 std::string author;
232 std::vector<Tileset> tilesets;
233 std::string scripts;
234 std::string onLoadEvents;
237 #endif