1 /******************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <Gosu/Utility.hpp>
8 #include <libxml/tree.h>
13 #include "resourcer.h"
17 static World
* globalWorld
= NULL
;
19 World
* World::getWorld()
24 World::World(Resourcer
* rc
, GameWindow
* wnd
)
25 : rc(rc
), wnd(wnd
), area(NULL
)
37 if (!processDescriptor()) // Try to load in descriptor.
40 // FIXME The player entity doesn't have a descriptor yet.
41 player
.reset(new Player(rc
, NULL
, xml
.playerentity
));
45 wnd
->setCaption(Gosu::widen(xml
.name
));
46 return loadArea(xml
.entry
.area
, xml
.entry
.coords
);
49 void World::buttonDown(const Gosu::Button btn
)
51 area
->buttonDown(btn
);
59 bool World::needsRedraw() const
61 return area
->needsRedraw();
69 bool World::processDescriptor()
71 static const std::string descriptor
= "world.conf";
74 XMLDocRef doc
= rc
->getXMLDoc(descriptor
, "dtd/world.dtd");
77 const xmlNode
* root
= xmlDocGetRootElement(doc
.get()); // <world>
81 xmlNode
* node
= root
->xmlChildrenNode
; // children of <world>
82 for (; node
!= NULL
; node
= node
->next
) {
83 if (!xmlStrncmp(node
->name
, BAD_CAST("name"), 5)) {
84 str
= xmlNodeGetContent(node
);
85 xml
.name
= (char*)str
;
87 if (!xmlStrncmp(node
->name
, BAD_CAST("author"), 7)) {
88 str
= xmlNodeGetContent(node
);
89 xml
.author
= (char*)str
;
91 if (!xmlStrncmp(node
->name
, BAD_CAST("player"), 7)) {
92 str
= xmlNodeGetContent(node
);
93 xml
.playerentity
= (char*)str
;
95 if (!xmlStrncmp(node
->name
, BAD_CAST("type"), 5)) {
96 str
= xmlGetProp(node
, BAD_CAST("locality"));
98 if (xmlStrncmp(str
, BAD_CAST("local"), 6))
101 else if (xmlStrncmp(str
, BAD_CAST("network"), 8))
102 xml
.locality
= NETWORK
;
105 Log::err(descriptor
, "Invalid <type> value");
109 str
= xmlGetProp(node
, BAD_CAST("movement"));
111 if (xmlStrncmp(str
, BAD_CAST("turn"), 5))
114 else if (xmlStrncmp(str
, BAD_CAST("tile"), 5))
117 else if (xmlStrncmp(str
, BAD_CAST("notile"), 7))
118 xml
.movement
= NOTILE
;
121 Log::err(descriptor
, "Invalid <locality> value");
125 if (!xmlStrncmp(node
->name
, BAD_CAST("entrypoint"), 11)) {
126 str
= xmlGetProp(node
, BAD_CAST("area"));
127 xml
.entry
.area
= (char*)str
;
129 str
= xmlGetProp(node
, BAD_CAST("x"));
130 xml
.entry
.coords
.x
= atol((char*)str
);
132 str
= xmlGetProp(node
, BAD_CAST("y"));
133 xml
.entry
.coords
.y
= atol((char*)str
);
135 str
= xmlGetProp(node
, BAD_CAST("z"));
136 xml
.entry
.coords
.z
= atol((char*)str
);
138 if (!xmlStrncmp(node
->name
, BAD_CAST("scripts"), 13)) {
139 node
= node
->xmlChildrenNode
; // decend
141 if (!xmlStrncmp(node
->name
, BAD_CAST("script"), 7)) {
142 str
= xmlNodeGetContent(node
);
143 xml
.scripts
.push_back((char*)str
);
150 bool World::loadArea(const std::string
& areaName
, coord_t playerPos
)
152 Area
* newArea
= new Area(rc
, this, player
.get(), areaName
);
157 player
->setArea(area
);
158 player
->setCoordsByTile(playerPos
);