1 /*********************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
13 #include <boost/unordered_map.hpp>
14 #include <libxml/parser.h>
16 #include "tile.h" // for enum TileEventTrigger
17 #include "resourcer.h"
29 //! An Entity represents one 'thing' that will be rendered to the screen.
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
47 //! Entity Initializer
48 bool init(const std::string
& descriptor
);
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
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
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.
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;
112 virtual void setFrozen(bool b
);
116 // Python-specific interface
119 //! Exempt ourselves from TILE_NOWALK et al.
120 FlagManip
exemptManip();
122 void addOnUpdateListener(boost::python::object callable
);
125 std::vector
<icoord
> frontTiles() const;
127 //! Calculate what the draw offset should be and saves it in 'doff'.
130 //! Retrieves a sound custom-defined within this Entity's descriptor
132 SampleRef
getSound(const std::string
& name
) const;
134 //! Normalize each of the X-Y axes into [-1, 0, or 1] and saves value
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.
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
;
180 std::string phaseName
;
183 //! List of sounds this Entity knows about.
185 //! List of scripts this Entity knows about.
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
197 /** Hack flag can be used to not stop the moving animation in-between
200 unsigned nowalkFlags
;
201 unsigned nowalkExempt
;
209 //! Pointer to Area this Entity is located on.
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
;