finish move to Python-handled imports
[Tsunagari.git] / src / tile.h
blobd43e8b3f588fecba4260fec3c54251cdb72dc4bc
1 /***************************************
2 ** Tsunagari Tile Engine **
3 ** tile.h **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
7 // **********
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // **********
27 #ifndef TILE_H
28 #define TILE_H
30 #include <string>
31 #include <vector>
33 class Tile;
34 class TileType;
35 class TileSet;
37 #include "area.h"
38 #include "animation.h"
39 #include "reader.h" // for TiledImage
40 #include "script.h"
41 #include "vec.h"
43 class Area;
44 class Entity;
45 class TileType;
47 //! List of possible flags that can be attached to a tile.
48 /*!
49 Flags are attached to tiles and denote special behavior for
50 the tile they are bound to.
52 see AreaTMX::splitTileFlags().
55 /**
56 * TILE_NOWALK
57 * Neither the player nor NPCs can walk here.
59 #define TILE_NOWALK 0x001
61 /**
62 * TILE_NOWALK_PLAYER
63 * The player cannot walk here. NPCs can, though.
65 #define TILE_NOWALK_PLAYER 0x002
67 /**
68 * TILE_NOWALK_NPC
69 * NPCs cannot walk here. The player can, though.
71 #define TILE_NOWALK_NPC 0x004
73 /**
74 * TILE_NOWALK_EXIT
75 * This Tile is an Exit. Please take appropriate action when entering this Tile,
76 * usually by transferring to another Area.
78 * This flag is not carried by actual Tiles, but can instead be flipped in an
79 * Entity's "exempt" flag which will be read elsewhere in the engine.
81 #define TILE_NOWALK_EXIT 0x008
83 /**
84 * TILE_NOWALK_AREA_BOUND
85 * This Tile is at the edge of an Area. If you step here, please handle it
86 * appropriately.
88 * (Usually if one moves off a map bound, one will either transfer to another
89 * Area, or will be destroyed.)
91 * This flag is not carried by actual Tiles, but can instead be flipped in an
92 * Entity's "exempt" flag which will be read elsewhere in the engine.
94 #define TILE_NOWALK_AREA_BOUND 0x016
97 /**
98 * Types of exits.
100 enum ExitDirection {
102 * An Exit that is taken upon arriving at the Tile.
104 EXIT_NORMAL,
106 * An Exit that is taken when leaving in the upwards
107 * direction from a Tile.
109 EXIT_UP,
111 * An Exit that is taken when leaving in the downwards
112 * direction from a Tile.
114 EXIT_DOWN,
116 * An Exit that is taken when leaving to the left from
117 * a Tile.
119 EXIT_LEFT,
121 * An Exit that is taken when leaving to the right from
122 * a Tile.
124 EXIT_RIGHT,
125 EXITS_LENGTH
129 * Independant object that can manipulate a Tile's flags.
131 class FlagManip
133 public:
134 FlagManip(unsigned* flags);
136 bool isNowalk() const;
137 bool isNowalkPlayer() const;
138 bool isNowalkNPC() const;
139 bool isNowalkExit() const;
140 bool isNowalkAreaBound() const;
142 void setNowalk(bool nowalk);
143 void setNowalkPlayer(bool nowalk);
144 void setNowalkNPC(bool nowalk);
145 void setNowalkExit(bool nowalk);
146 void setNowalkAreaBound(bool nowalk);
148 private:
149 unsigned* flags;
152 //! Convenience trigger for inter-area teleportation.
154 Tiles with a exit trigger attached can teleport the player to a
155 new area in the World. The Exit struct contains the destination
156 area and coordinates.
158 class Exit {
159 public:
160 Exit();
161 Exit(const std::string area, int x, int y, double z);
163 public:
164 std::string area;
165 vicoord coords;
168 class TileBase
170 public:
171 TileBase();
173 FlagManip flagManip();
175 //! Determines whether this tile or one of its parent types embodies a
176 //! flag.
177 bool hasFlag(unsigned flag) const;
179 TileType* getType() const;
180 void setType(TileType* type);
182 void runEnterScript(Entity* triggeredBy);
183 void runLeaveScript(Entity* triggeredBy);
184 void runUseScript(Entity* triggeredBy);
186 private:
187 void runScript(Entity* triggeredBy, ScriptRef& script);
189 public:
190 TileBase* parent;
191 unsigned flags;
192 ScriptRef enterScript, leaveScript, useScript;
195 //! Contains properties unique to this tile.
197 This struct contains local tile properties for a single tile in
198 the area. As opposed to global properties which apply to all
199 tiles of the same type, these properties will only apply to one
200 tile.
202 class Tile : public TileBase
204 public:
205 Tile(); // Should not be used. Wanted by std::containers.
206 Tile(Area* area, int x, int y, int z);
209 * Gets the correct destination for an Entity wanting to
210 * move off of this tile in <code>facing</code>
211 * direction.
213 * This call is necessary to handle layermod.
215 * @param here area-space coordinate for this Tile
216 * @param facing facing vector
218 icoord moveDest(icoord here, ivec2 facing) const;
219 Tile* offset(int x, int y) const;
221 double getZ() const;
223 Exit* getNormalExit() const;
224 void setNormalExit(Exit exit);
226 Exit* exitAt(ivec2 dir) const;
227 double* layermodAt(ivec2 dir) const;
229 public:
230 Area* area;
233 * The grid-space coordinates of this Tile.
235 * Looping Areas utilize area-space components. These
236 * cannot be losslessly transformed into area-space.
238 int x, y, z;
239 Exit* exits[EXITS_LENGTH];
240 double* layermods[EXITS_LENGTH];
241 int entCnt; //!< Number of entities on this Tile.
244 //! Contains the properties shared by all tiles of a certain type.
246 This struct contains global tile properties for a tile of a
247 certain type. As opposed to local properties for a single tile,
248 all tiles of this type will share the defined characteristics.
250 class TileType : public TileBase
252 public:
253 TileType();
254 TileType(ImageRef& img);
256 //! Returns true if onscreen and we need to update our animation.
257 bool needsRedraw() const;
259 public:
260 Animation anim; //! Graphics for tiles of this type.
261 std::vector<Tile*> allOfType;
264 class TileSet
266 public:
267 TileSet();
268 TileSet(int width, int height);
270 void add(TileType* type);
271 void set(int idx, TileType* type);
272 TileType* get(int x, int y);
273 int getWidth() const;
274 int getHeight() const;
276 private:
277 size_t idx(int x, int y) const;
279 std::vector<TileType*> types;
280 int width, height;
283 void exportTile();
285 #endif