viewport centers on player, doesn't go off edges
[Tsunagari.git] / src / world.cpp
blob02baa62dfc26c3e64fa35963a99ece4da94f429e
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** world.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 "log.h"
13 #include "resourcer.h"
14 #include "window.h"
15 #include "world.h"
17 World::World(Resourcer* rc, GameWindow* wnd)
18 : rc(rc), wnd(wnd), area(NULL), player(NULL)
22 World::~World()
24 delete player;
25 delete area;
28 bool World::init()
30 if (!processDescriptor()) // Try to load in descriptor.
31 return false;
33 // FIXME The player entity doesn't have a descriptor yet.
34 player = new Entity(rc, "_NONE_", xml.playersprite);
35 if (!player->init())
36 return false;
38 area = new Area(rc, player, xml.entry.area);
40 wnd->setCaption(Gosu::widen(xml.name));
41 return area->init();
44 void World::buttonDown(const Gosu::Button btn)
46 area->buttonDown(btn);
49 void World::draw()
51 area->draw();
54 bool World::needsRedraw() const
56 return area->needsRedraw();
59 bool World::processDescriptor()
61 static const std::string descriptor = "world.conf";
62 xmlChar* str;
64 xmlDoc* doc = rc->getXMLDoc(descriptor);
65 if (!doc)
66 return false;
67 const xmlNode* root = xmlDocGetRootElement(doc);
68 if (!root) {
69 xmlFreeDoc(doc);
70 return false;
73 xmlNode* node = root->xmlChildrenNode; // <world>
74 node = node->xmlChildrenNode; // decend into children of <world>
75 for (; node != NULL; node = node->next) {
76 if (!xmlStrncmp(node->name, BAD_CAST("name"), 5)) {
77 str = xmlNodeGetContent(node);
78 xml.name = (char*)str;
80 if (!xmlStrncmp(node->name, BAD_CAST("author"), 7)) {
81 str = xmlNodeGetContent(node);
82 xml.author = (char*)str;
84 if (!xmlStrncmp(node->name, BAD_CAST("type"), 5)) {
85 str = xmlNodeGetContent(node);
87 if (xmlStrncmp(str, BAD_CAST("local"), 6))
88 xml.type = LOCAL;
90 if (xmlStrncmp(str, BAD_CAST("network"), 8))
91 xml.type = NETWORK;
93 else {
94 xmlFreeDoc(doc);
95 Log::err(descriptor, "Invalid <type> value");
96 return false;
99 if (!xmlStrncmp(node->name, BAD_CAST("player"), 7)) {
100 str = xmlGetProp(node, BAD_CAST("sprite"));
101 xml.playersprite = (char*)str;
103 if (!xmlStrncmp(node->name, BAD_CAST("entrypoint"), 11)) {
104 str = xmlGetProp(node, BAD_CAST("area"));
105 xml.entry.area = (char*)str;
107 str = xmlGetProp(node, BAD_CAST("x"));
108 xml.entry.coords.x = atol((char*)str);
110 str = xmlGetProp(node, BAD_CAST("y"));
111 xml.entry.coords.y = atol((char*)str);
113 str = xmlGetProp(node, BAD_CAST("z"));
114 xml.entry.coords.z = atol((char*)str);
116 if (!xmlStrncmp(node->name, BAD_CAST("eventscripts"), 13)) {
117 node = node->xmlChildrenNode; // decend
119 if (!xmlStrncmp(node->name, BAD_CAST("script"), 7)) {
120 str = xmlNodeGetContent(node);
121 xml.scripts.push_back((char*)str);
124 xmlFreeDoc(doc);
125 return true;