remove +x from source
[Tsunagari.git] / src / entity.h
blobaeb9c201121b0c0c35130c4c531558cd552f447a
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** entity.h **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 #ifndef ENTITY_H
8 #define ENTITY_H
10 #include <string>
11 #include <vector>
13 #include <boost/unordered_map.hpp>
14 #include <libxml/parser.h>
16 #include "tile.h" // for enum TileEventTrigger
17 #include "resourcer.h"
18 #include "scriptinst.h"
19 #include "python.h"
21 class Animation;
22 class Area;
23 class Resourcer;
25 namespace Gosu {
26 class Bitmap;
27 class Image;
30 //! An Entity represents one 'thing' that will be rendered to the screen.
31 /*!
32 An Entity might be a dynamic game object such as a monster, NPC, or
33 item. Entity can handle animated images that cycle through their
34 frames over time. It also has the capacity to switch between a couple
35 different images on demand.
37 For example, you might have a Entity for a player character with
38 animated models for walking in each possible movement direction (up,
39 down, left, right) along with static standing-still images for each
40 direction.
42 class Entity
44 public:
45 Entity();
46 virtual ~Entity();
48 //! Entity initializer
49 virtual bool init(const std::string& descriptor);
51 //! Entity destroyer.
52 virtual void destroy();
54 //! Gosu Callback
55 void draw();
56 bool needsRedraw() const;
58 virtual void tick(unsigned long dt);
59 void tickTurn(unsigned long dt);
60 void tickTile(unsigned long dt);
61 void tickNoTile(unsigned long dt);
63 void turn();
65 const std::string getFacing() const;
67 //! Change the graphic. Returns true if it was changed to something
68 //! different.
69 bool setPhase(const std::string& name);
71 std::string getPhase() const;
74 //! The offset from the upper-left of the Area to the upper-left of the
75 //! Tile the Entity is standing on.
76 rcoord getPixelCoord() const;
78 //! Retrieve position within Area.
79 icoord getTileCoords_i() const;
80 vicoord getTileCoords_vi() const;
82 //! Set location within Area.
83 void setTileCoords(int x, int y);
84 void setTileCoords(int x, int y, double z);
85 void setTileCoords(icoord phys);
86 void setTileCoords(vicoord virt);
87 void setTileCoords(rcoord virt);
89 //! Abstract, Python-specific method.
90 virtual void teleport(int x, int y);
93 //! Indicates which coordinate we will move into if we proceed in
94 //! direction specified.
95 icoord moveDest(ivec2 facing);
96 vicoord moveDest(Tile* tile, int dx, int dy); // Python-specific version
98 //! Returns true if we can move in the desired direction.
99 bool canMove(int x, int y, double z); // Python-specific version.
100 bool canMove(icoord dest);
101 bool canMove(vicoord dest);
103 //! Indicates whether we are in the middle of transitioning between
104 //! tiles.
105 bool isMoving() const;
107 //! Initiate a movement within the Area.
108 void moveByTile(int x, int y);
109 void moveByTile(ivec2 delta);
111 //! Abstract, Python-specific method.
112 virtual void move(int x, int y);
115 //! Gets the Entity's current Area.
116 Area* getArea();
118 //! Specifies the Area object this entity will ask when looking for
119 //! nearby Tiles. Doesn't change x,y,z position.
120 void setArea(Area* area);
123 //! Gets speed multiplier.
124 double getSpeed() const;
126 //! Sets speed multiplier.
127 void setSpeed(double multiplier);
130 //! Get the Tile that we are standing on.
131 Tile* getTile() const;
132 Tile* getTile();
134 virtual void setFrozen(bool b);
135 bool getFrozen();
139 // Python-specific interface
142 //! Exempt ourselves from TILE_NOWALK et al.
143 FlagManip exemptManip();
147 // Variables public for Python scripts
150 //! Script hooks.
151 ScriptInst tickScript, turnScript, tileEntryScript,
152 tileExitScript, deleteScript;
155 protected:
156 virtual void erase();
158 //! Calculate what the draw offset should be and saves it in 'doff'.
159 void calcDoff();
161 //! Retrieves a sound custom-defined within this Entity's descriptor
162 //! file.
163 SampleRef getSound(const std::string& name) const;
165 //! Normalize each of the X-Y axes into [-1, 0, or 1] and saves value
166 //! to 'facing'.
167 ivec2 setFacing(ivec2 facing);
169 //! Gets a string describing a direction.
170 const std::string& directionStr(ivec2 facing) const;
172 bool nowalked(Tile& t);
174 //! Called right before starting to moving onto another tile.
175 virtual void preMove();
177 //! Called after we have arrived at another tile.
178 virtual void postMove();
180 void leaveTile();
181 void enterTile();
182 void enterTile(Tile* t);
184 void runTickScript();
185 void runTurnScript();
186 void runTileExitScript();
187 void runTileEntryScript();
189 // XML parsing functions used in constructing an Entity
190 bool processDescriptor();
191 bool processSprite(XMLNode node);
192 bool processPhases(XMLNode node, const TiledImage& tiles);
193 bool processPhase(const XMLNode node, const TiledImage& tiles);
194 bool processMembers(XMLNode node, std::vector<ImageRef>& frames,
195 const TiledImage& tiles);
196 bool processMember(const XMLNode node, std::vector<ImageRef>& frames,
197 const TiledImage& tiles);
198 bool processSounds(XMLNode node);
199 bool processSound(const XMLNode node);
200 bool processScripts(XMLNode node);
201 bool processScript(const XMLNode node);
202 bool setScript(const std::string& trigger, ScriptInst& script);
205 protected:
206 typedef boost::unordered_map<std::string, Animation> AnimationMap;
207 typedef boost::unordered_map<std::string, SampleRef> SampleMap;
210 //! Set to true if the Entity wants the screen to be redrawn.
211 bool redraw;
213 //! Pointer to Area this Entity is located on.
214 Area* area;
215 rcoord r; //!< real x,y position: hold partial pixel transversal
216 rcoord doff; //!< Drawing offset to center entity on tile.
218 std::string descriptor;
220 bool frozen;
222 double baseSpeed; //!< Original speed, specified in descriptor.
223 double speedMul; //!< Speed multiplier.
224 double speed; //!< Effective speed = original speed * multiplier
226 //! True if currently moving between two Tiles in an Area. Only used in
227 //! TILE game mode.
228 bool moving;
229 /** Hack flag can be used to not stop the moving animation in-between
230 * tiles. */
231 bool stillMoving;
232 unsigned nowalkFlags;
233 unsigned nowalkExempt;
235 vicoord deltaCoord;
236 rcoord fromCoord;
237 rcoord destCoord;
238 Tile* fromTile;
239 Tile* destTile;
241 int imgw, imgh;
242 AnimationMap phases;
243 Animation* phase;
244 std::string phaseName;
245 ivec2 facing;
247 //! List of sounds this Entity knows about.
248 SampleMap sounds;
251 void exportEntity();
253 #endif