Merge branch 'sphase'
[Tsunagari.git] / src / entity.cpp
blob19fb1cbaf36ff08bf6a8fd07e58f593bcd4a7c7f
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** entity.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
10 #include "area.h"
11 #include "entity.h"
12 #include "sprite.h"
13 #include "world.h"
15 Entity::Entity(Resourcer* rc,
16 Area* area,
17 const std::string& descriptor)
18 : rc(rc),
19 sprite(rc),
20 area(area),
21 redraw(true),
22 descriptor(descriptor)
26 Entity::~Entity()
30 /**
31 * Try to load in descriptor.
33 bool Entity::processDescriptor()
35 xmlChar* str;
37 XMLDocRef doc = rc->getXMLDoc(descriptor, "dtd/entity.dtd");
38 if (!doc)
39 return false;
40 const xmlNode* root = xmlDocGetRootElement(doc.get()); // <entity>
41 if (!root)
42 return false;
44 str = xmlGetProp(const_cast<xmlNode*>(root), BAD_CAST("type")); // Entity type
45 if (!xmlStrncmp(str, BAD_CAST("player"), 7)) {
46 type = PLAYER;
47 if (!processPlayerDescriptor(root))
48 return false;
50 else {
51 Log::err(descriptor, "unknown entity type");
52 return false;
54 return true;
57 bool Entity::processPlayerDescriptor(const xmlNode* root)
59 xmlChar* str;
61 xmlNode* node = root->xmlChildrenNode; // children of <entity>
62 for (; node != NULL; node = node->next) {
63 if (!xmlStrncmp(node->name, BAD_CAST("sprite"), 7)) {
64 str = xmlNodeGetContent(node);
65 spriteDescriptor = (char*)str;
67 //TODO: <sounds>
69 return true;
72 bool Entity::init()
74 return processDescriptor() && sprite.init(spriteDescriptor);
77 void Entity::draw()
79 redraw = false;
80 sprite.draw();
83 bool Entity::needsRedraw() const
85 return redraw;
88 coord_t Entity::getCoordsByPixel()
90 return sprite.getCoordsByPixel();
93 coord_t Entity::getCoordsByTile()
95 return sprite.getCoordsByTile();
98 void Entity::moveByTile(coord_t delta)
100 coord_t newCoord = sprite.getCoordsByTile();
101 newCoord.x += delta.x;
102 newCoord.y += delta.y;
103 newCoord.z += delta.z;
104 Area::Tile* dest = area->getTile(newCoord);
105 if ((dest->flags & Area::nowalk) != 0 ||
106 (dest->type->flags & Area::nowalk) != 0) {
107 // The tile we're trying to move onto is set as nowalk.
108 // Stop here.
109 return;
111 sprite.moveByTile(delta);
112 redraw = true;
113 postMove();
116 void Entity::setCoordsByTile(coord_t pos)
118 sprite.setCoordsByTile(pos);
119 redraw = true;
122 void Entity::setArea(Area* area)
124 this->area = area;
127 void Entity::postMove()