fix input duplication bug
[Tsunagari.git] / src / world.cpp
blob81b80d768541621b1111488f7acec22b10442e8a
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** world.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <Gosu/Utility.hpp>
8 #include <libxml/parser.h>
9 #include <libxml/tree.h>
11 #include "area.h"
12 #include "entity.h"
13 #include "log.h"
14 #include "resourcer.h"
15 #include "window.h"
16 #include "world.h"
18 static World* globalWorld = NULL;
20 World* World::getWorld()
22 return globalWorld;
25 World::World(Resourcer* rc, GameWindow* wnd)
26 : rc(rc), wnd(wnd), area(NULL), player(NULL)
28 globalWorld = this;
31 World::~World()
33 delete player;
34 delete area;
37 bool World::init()
39 if (!processDescriptor()) // Try to load in descriptor.
40 return false;
42 // FIXME The player entity doesn't have a descriptor yet.
43 player = new Entity(rc, NULL, "_NONE_", xml.playersprite);
44 if (!player->init())
45 return false;
47 wnd->setCaption(Gosu::widen(xml.name));
48 return loadArea(xml.entry.area, xml.entry.coords);
51 void World::buttonDown(const Gosu::Button btn)
53 area->buttonDown(btn);
56 void World::draw()
58 area->draw();
61 bool World::needsRedraw() const
63 return area->needsRedraw();
66 bool World::processDescriptor()
68 static const std::string descriptor = "world.conf";
69 xmlChar* str;
71 XMLDocRef doc = rc->getXMLDoc(descriptor);
72 if (!doc)
73 return false;
74 const xmlNode* root = xmlDocGetRootElement(doc.get());
75 if (!root)
76 return false;
78 xmlNode* node = root->xmlChildrenNode; // <world>
79 node = node->xmlChildrenNode; // decend into children of <world>
80 for (; node != NULL; node = node->next) {
81 if (!xmlStrncmp(node->name, BAD_CAST("name"), 5)) {
82 str = xmlNodeGetContent(node);
83 xml.name = (char*)str;
85 if (!xmlStrncmp(node->name, BAD_CAST("author"), 7)) {
86 str = xmlNodeGetContent(node);
87 xml.author = (char*)str;
89 if (!xmlStrncmp(node->name, BAD_CAST("type"), 5)) {
90 str = xmlGetProp(node, BAD_CAST("locality"));
92 if (xmlStrncmp(str, BAD_CAST("local"), 6))
93 xml.locality = LOCAL;
95 else if (xmlStrncmp(str, BAD_CAST("network"), 8))
96 xml.locality = NETWORK;
98 else {
99 Log::err(descriptor, "Invalid <type> value");
100 return false;
103 str = xmlGetProp(node, BAD_CAST("movement"));
105 if (xmlStrncmp(str, BAD_CAST("turn"), 5))
106 xml.movement = TURN;
108 else if (xmlStrncmp(str, BAD_CAST("tile"), 5))
109 xml.movement = TILE;
111 else if (xmlStrncmp(str, BAD_CAST("notile"), 7))
112 xml.movement = NOTILE;
114 else {
115 Log::err(descriptor, "Invalid <locality> value");
116 return false;
119 if (!xmlStrncmp(node->name, BAD_CAST("player"), 7)) {
120 str = xmlGetProp(node, BAD_CAST("sprite"));
121 xml.playersprite = (char*)str;
123 if (!xmlStrncmp(node->name, BAD_CAST("entrypoint"), 11)) {
124 str = xmlGetProp(node, BAD_CAST("area"));
125 xml.entry.area = (char*)str;
127 str = xmlGetProp(node, BAD_CAST("x"));
128 xml.entry.coords.x = atol((char*)str);
130 str = xmlGetProp(node, BAD_CAST("y"));
131 xml.entry.coords.y = atol((char*)str);
133 str = xmlGetProp(node, BAD_CAST("z"));
134 xml.entry.coords.z = atol((char*)str);
136 if (!xmlStrncmp(node->name, BAD_CAST("eventscripts"), 13)) {
137 node = node->xmlChildrenNode; // decend
139 if (!xmlStrncmp(node->name, BAD_CAST("script"), 7)) {
140 str = xmlNodeGetContent(node);
141 xml.scripts.push_back((char*)str);
144 return true;
148 bool World::loadArea(const std::string& areaName, coord_t playerPos)
150 Area* newArea = new Area(rc, this, player, areaName);
151 delete area;
152 area = newArea;
153 if (!area->init())
154 return false;
155 player->setArea(area);
156 player->setCoordsByTile(playerPos);
157 return true;