FEATURE: rough collision support between Entities
[Tsunagari.git] / src / tile.h
blob51a040f2e15957be5d489534668ac7165cebe859
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** tile.h **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 #ifndef TILE_H
8 #define TILE_H
10 #include <string>
11 #include <vector>
13 #include <boost/optional.hpp>
15 #include "animation.h"
16 #include "resourcer.h" // for TiledImage
17 #include "scriptinst.h"
19 class Area;
20 class Entity;
21 class TileType;
23 //! List of possible flags that can be attached to a tile.
24 /*!
25 Flags are attached to tiles and denote special behavior for
26 the tile they are bound to.
28 see AreaTMX::splitTileFlags().
30 #define TILE_NOWALK 0x001
31 #define TILE_NOWALK_PLAYER 0x002
32 #define TILE_NOWALK_NPC 0x004
33 #define TILE_NOWALK_BCO_ENTITY 0x008
35 // Indexes into Exit and layermod arrays found in class Tile.
36 enum ExitDirection {
37 EXIT_NORMAL,
38 EXIT_UP,
39 EXIT_DOWN,
40 EXIT_LEFT,
41 EXIT_RIGHT
44 /**
45 * Independant object that can manipulate a Tile's flags.
47 class FlagManip
49 public:
50 FlagManip(unsigned* flags);
52 bool isNowalk() const;
53 bool isNowalkPlayer() const;
54 bool isNowalkNPC() const;
55 bool isNowalkBCOEntity() const;
57 void setNowalk(bool nowalk);
58 void setNowalkPlayer(bool nowalk);
59 void setNowalkNPC(bool nowalk);
61 private:
62 unsigned* flags;
65 //! Convenience trigger for inter-area teleportation.
66 /*!
67 Tiles with a exit trigger attached can teleport the player to a
68 new area in the World. The Exit struct contains the destination
69 area and coordinates.
71 class Exit {
72 public:
73 Exit();
74 Exit(const std::string area, int x, int y, double z);
76 public:
77 std::string area;
78 vicoord coords;
81 class TileBase
83 public:
84 TileBase();
86 //! Determines whether this tile or one of its parent types embodies a
87 //! flag.
88 bool hasFlag(unsigned flag) const;
89 FlagManip flagManip();
91 TileType* getType() const;
92 void setType(TileType* type);
94 void onEnterScripts(Entity* triggeredBy);
95 void onLeaveScripts(Entity* triggeredBy);
96 void onUseScripts(Entity* triggeredBy);
98 private:
99 //! Runs scripts in a vector.
100 void runScripts(Entity* triggeredBy, std::vector<ScriptInst>& events);
102 public:
103 TileBase* parent;
104 unsigned flags;
105 std::vector<ScriptInst> onEnter, onLeave, onUse;
108 //! Contains properties unique to this tile.
110 This struct contains local tile properties for a single tile in
111 the area. As opposed to global properties which apply to all
112 tiles of the same type, these properties will only apply to one
113 tile.
115 class Tile : public TileBase
117 public:
118 Tile(); // Should not be used. Wanted by std::containers.
119 Tile(Area* area, int x, int y, int z);
121 Tile& offset(int x, int y);
123 double getZ();
125 Exit* getNormalExit() const;
126 void setNormalExit(Exit exit);
128 Exit* exitAt(ivec2 dir) const;
129 boost::optional<double> layermodAt(ivec2 dir) const;
131 public:
132 Area* area;
133 int x, y, z;
134 Exit* exits[5];
135 boost::optional<double> layermods[5];
138 //! Contains the properties shared by all tiles of a certain type.
140 This struct contains global tile properties for a tile of a
141 certain type. As opposed to local properties for a single tile,
142 all tiles of this type will share the defined characteristics.
144 class TileType : public TileBase
146 public:
147 TileType();
148 TileType(ImageRef& img);
150 //! Returns true if onscreen and we need to update our animation.
151 bool needsRedraw() const;
153 public:
154 Animation anim; //! Graphics for tiles of this type.
155 std::vector<Tile*> allOfType;
158 class TileSet
160 public:
161 TileSet();
162 TileSet(int width, int height);
164 void add(TileType* type);
165 void set(int idx, TileType* type);
166 TileType& get(int x, int y);
167 int getWidth() const;
168 int getHeight() const;
170 //! Throws a Python exception if out of bounds.
171 TileType& pyGet(int x, int y);
173 private:
174 size_t idx(int x, int y) const;
176 std::vector<TileType*> types;
177 int width, height;
180 void exportTile();
182 #endif