Py: rename entity.{animation,phase}
[Tsunagari.git] / src / entity.h
blobab5a581a773b99046ae9044172e920a8ef1f22a3
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 "python.h"
20 class Animation;
21 class Area;
22 class Resourcer;
24 namespace Gosu {
25 class Bitmap;
26 class Image;
29 //! An Entity represents one 'thing' that will be rendered to the screen.
30 /*!
31 An Entity might be a dynamic game object such as a monster, NPC, or
32 item. Entity can handle animated images that cycle through their
33 frames over time. It also has the capacity to switch between a couple
34 different images on demand.
36 For example, you might have a Entity for a player character with
37 animated models for walking in each possible movement direction (up,
38 down, left, right) along with static standing-still images for each
39 direction.
41 class Entity
43 public:
44 Entity();
45 ~Entity();
47 //! Entity Initializer
48 bool init(const std::string& descriptor);
50 //! Gosu Callback
51 void draw();
52 bool needsRedraw() const;
54 void update(unsigned long dt);
55 void updateTurn(unsigned long dt);
56 void updateTile(unsigned long dt);
57 void updateNoTile(unsigned long dt);
58 void onUpdateScripts();
60 const std::string getFacing() const;
62 //! Change the graphic. Returns true if it was changed to something
63 //! different.
64 bool setPhase(const std::string& name);
66 std::string getPhase() const;
69 //! The offset from the upper-left of the Area to the upper-left of the
70 //! Tile the Entity is standing on.
71 rcoord getPixelCoord() const;
73 //! Retrieve position within Area.
74 icoord getTileCoords_i() const;
75 vicoord getTileCoords_vi() const;
77 //! Set location within Area.
78 void setTileCoords(int x, int y);
79 void setTileCoords(int x, int y, double z);
80 void setTileCoords(icoord phys);
81 void setTileCoords(vicoord virt);
84 //! Indicates whether we are in the middle of transitioning between
85 //! tiles.
86 bool isMoving() const;
88 //! Initiate a movement within the Area.
89 void moveByTile(int x, int y);
90 void moveByTile(ivec2 delta);
93 //! Gets the Entity's current Area.
94 Area* getArea();
96 //! Specifies the Area object this entity will ask when looking for
97 //! nearby Tiles. Doesn't change x,y,z position.
98 void setArea(Area* area);
101 //! Gets speed multiplier.
102 double getSpeed() const;
104 //! Sets speed multiplier.
105 void setSpeed(double multiplier);
108 //! Get the Tile we are standing on.
109 Tile& getTile() const;
110 Tile& getTile();
112 virtual void setFrozen(bool b);
113 bool getFrozen();
116 // Python-specific interface
119 //! Exempt ourselves from TILE_NOWALK et al.
120 FlagManip exemptManip();
122 void addOnUpdateListener(boost::python::object callable);
124 protected:
125 std::vector<icoord> frontTiles() const;
127 //! Calculate what the draw offset should be and saves it in 'doff'.
128 void calcDoff();
130 //! Retrieves a sound custom-defined within this Entity's descriptor
131 //! file.
132 SampleRef getSound(const std::string& name) const;
134 //! Normalize each of the X-Y axes into [-1, 0, or 1] and saves value
135 //! to 'facing'.
136 ivec2 setFacing(ivec2 facing);
138 //! Gets a string describing a direction.
139 const std::string& directionStr(ivec2 facing) const;
141 //! Returns true if we can move in the desired direction.
142 virtual bool canMove(icoord delta);
144 bool nowalked(Tile& t);
146 //! Called right before starting to moving onto another tile.
147 virtual void preMove();
149 //! Called after we have arrived at another tile.
150 virtual void postMove();
152 void tileExitScript();
153 void tileEntryScript();
155 // XML parsing functions used in constructing an Entity
156 bool processDescriptor();
157 bool processSprite(XMLNode node);
158 bool processPhases(XMLNode node, const TiledImage& tiles);
159 bool processPhase(const XMLNode node, const TiledImage& tiles);
160 bool processMembers(XMLNode node, Animation& anim,
161 const TiledImage& tiles);
162 bool processMember(const XMLNode node, Animation& anim,
163 const TiledImage& tiles);
164 bool processSounds(XMLNode node);
165 bool processSound(const XMLNode node);
166 bool processScripts(XMLNode node);
167 bool processScript(const XMLNode node);
170 //! Set to true if the Entity wants the screen to be redrawn.
171 bool redraw;
173 typedef boost::unordered_map<std::string, Animation> AnimationMap;
174 typedef boost::unordered_map<std::string, SampleRef> SampleMap;
175 typedef boost::unordered_map<std::string, std::string> StringMap;
177 int imgw, imgh;
178 AnimationMap phases;
179 Animation* phase;
180 std::string phaseName;
181 ivec2 facing;
183 //! List of sounds this Entity knows about.
184 SampleMap sounds;
185 //! List of scripts this Entity knows about.
186 StringMap scripts;
188 std::vector<boost::python::object> updateListenerFns;
190 double baseSpeed; //!< Original speed, specified in descriptor.
191 double speedMul; //!< Speed multiplier.
192 double speed; //!< Effective speed = original speed * multiplier
194 //! True if currently moving between two Tiles in an Area. Only used in
195 //! TILE game mode.
196 bool moving;
197 /** Hack flag can be used to not stop the moving animation in-between
198 * tiles. */
199 bool stillMoving;
200 unsigned nowalkFlags;
201 unsigned nowalkExempt;
203 vicoord deltaCoord;
204 rcoord fromCoord;
205 rcoord destCoord;
206 Tile* fromTile;
207 Tile* destTile;
209 //! Pointer to Area this Entity is located on.
210 Area* area;
211 rcoord r; //!< real x,y position: hold partial pixel transversal
212 rcoord doff; //!< Drawing offset to center entity on tile.
214 std::string descriptor;
216 bool frozen;
219 void exportEntity();
221 #endif