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"
18 #include "scriptinst.h"
30 //! An Entity represents one 'thing' that will be rendered to the screen.
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
48 //! Entity initializer
49 virtual bool init(const std::string
& descriptor
);
52 virtual void destroy();
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
);
65 const std::string
getFacing() const;
67 //! Change the graphic. Returns true if it was changed to something
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
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.
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;
134 virtual void setFrozen(bool b
);
139 // Python-specific interface
142 //! Exempt ourselves from TILE_NOWALK et al.
143 FlagManip
exemptManip();
147 // Variables public for Python scripts
151 ScriptInst tickScript
, turnScript
, tileEntryScript
,
152 tileExitScript
, deleteScript
;
156 virtual void erase();
158 //! Calculate what the draw offset should be and saves it in 'doff'.
161 //! Retrieves a sound custom-defined within this Entity's descriptor
163 SampleRef
getSound(const std::string
& name
) const;
165 //! Normalize each of the X-Y axes into [-1, 0, or 1] and saves value
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();
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
);
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.
213 //! Pointer to Area this Entity is located on.
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
;
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
229 /** Hack flag can be used to not stop the moving animation in-between
232 unsigned nowalkFlags
;
233 unsigned nowalkExempt
;
244 std::string phaseName
;
247 //! List of sounds this Entity knows about.