Entity descriptor has scripts
[Tsunagari.git] / src / entity.h
bloba323f88c480a91fb8b80ac2985b950ba93612f1b
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** entity.h **
4 ** Copyright 2011 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 "area-structs.h" // for enum TileEventTriggers
17 #include "common.h"
18 #include "resourcer.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(Resourcer* rc, Area* area, ClientValues* conf);
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);
56 //! Change the graphic. Returns true if it was changed to something
57 // different.
58 bool setPhase(const std::string& name);
60 //! Retrieve position within Area.
61 coord_t getCoordsByPixel() const;
62 coord_t getCoordsByTile() const;
64 //! Set location within Area.
65 void setCoordsByPixel(coord_t c);
66 void setCoordsByTile(coord_t c);
68 //! Move within Area.
69 void moveByPixel(coord_t delta);
70 void moveByTile(coord_t delta);
72 //! Sets the Area object this entity will ask when looking for
73 // nearby Tiles. Doesn't change x,y,z position.
74 void setArea(Area* area);
77 // Lua callback targets
80 //! Move to the upper left corner. Sets x,y tile positions to 1,1.
81 void gotoRandomTile();
83 protected:
84 //! Get the Tile we are standing on.
85 Tile& getTile();
87 SampleRef getSound(const std::string& name);
89 //! Calculate which way to face based upon a movement delta.
90 void calculateFacing(coord_t delta);
92 //! Called right before starting to moving onto another tile.
93 virtual void preMove(coord_t delta);
94 virtual void preMoveLua();
96 //! Called after we have arrived at another tile.
97 virtual void postMove();
98 virtual void postMoveLua();
100 void tileScripts(Tile& tile, std::vector<TileEvent>& events, TileEventTriggers trigger);
101 void runTileLua(Tile& tile, const std::string& script);
103 // XML parsing functions used in constructing an Entity
104 bool processDescriptor();
105 bool processSprite(const xmlNode* sprite);
106 bool processPhases(const xmlNode* phases);
107 bool processPhase(xmlNode* phase, const TiledImage& tiles);
108 bool processMember(xmlNode* phase, Animation& anim,
109 const TiledImage& tiles);
110 bool processSounds(const xmlNode* sounds);
111 bool processSound(xmlNode* sound);
112 void processScripts(const xmlNode* scripts);
113 void processScript(xmlNode* script);
116 Resourcer* rc;
118 boost::unordered_map<std::string, Animation> phases;
119 Animation* phase;
120 bool redraw;
121 std::string facing;
123 boost::unordered_map<std::string, SampleRef> sounds;
124 boost::unordered_map<std::string, std::string> scripts;
126 bool moving;
127 coord_t dest;
128 double speed;
129 Tile* movingFrom;
131 Area* area;
132 coord_t c;
133 double rx, ry, rz; // real x,y position: hold partial pixel transversal
135 std::string descriptor;
137 ClientValues* conf;
139 //! SpriteValues XML Storage Struct
141 Main XML storage struct for Sprite.
143 struct SpriteValues {
144 std::string sheet;
145 coord_t tileSize;
146 boost::unordered_map<std::string, unsigned> phases;
147 } xml;
150 #endif