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
));
42 if (!player
->init(xml
.playerentity
))
44 player
->setPhase("down");
46 wnd
->setCaption(Gosu::widen(xml
.name
));
47 return loadArea(xml
.entry
.area
, xml
.entry
.coords
);
50 void World::buttonDown(const Gosu::Button btn
)
52 area
->buttonDown(btn
);
55 void World::buttonUp(const Gosu::Button btn
)
65 bool World::needsRedraw() const
67 return area
->needsRedraw();
70 void World::update(unsigned long dt
)
75 bool World::processDescriptor()
77 static const std::string descriptor
= "world.conf";
80 XMLDocRef doc
= rc
->getXMLDoc(descriptor
, "world.dtd");
83 const xmlNode
* root
= xmlDocGetRootElement(doc
.get()); // <world>
87 xmlNode
* node
= root
->xmlChildrenNode
; // children of <world>
88 for (; node
!= NULL
; node
= node
->next
) {
89 if (!xmlStrncmp(node
->name
, BAD_CAST("name"), 5)) {
90 str
= xmlNodeGetContent(node
);
91 xml
.name
= (char*)str
;
93 if (!xmlStrncmp(node
->name
, BAD_CAST("author"), 7)) {
94 str
= xmlNodeGetContent(node
);
95 xml
.author
= (char*)str
;
97 if (!xmlStrncmp(node
->name
, BAD_CAST("player"), 7)) {
98 str
= xmlNodeGetContent(node
);
99 xml
.playerentity
= (char*)str
;
101 if (!xmlStrncmp(node
->name
, BAD_CAST("type"), 5)) {
102 str
= xmlGetProp(node
, BAD_CAST("locality"));
104 if (xmlStrncmp(str
, BAD_CAST("local"), 6))
105 xml
.locality
= LOCAL
;
107 else if (xmlStrncmp(str
, BAD_CAST("network"), 8))
108 xml
.locality
= NETWORK
;
111 Log::err(descriptor
, "Invalid <type> value");
115 str
= xmlGetProp(node
, BAD_CAST("movement"));
117 if (xmlStrncmp(str
, BAD_CAST("turn"), 5))
120 else if (xmlStrncmp(str
, BAD_CAST("tile"), 5))
123 else if (xmlStrncmp(str
, BAD_CAST("notile"), 7))
124 xml
.movement
= NOTILE
;
127 Log::err(descriptor
, "Invalid <locality> value");
131 if (!xmlStrncmp(node
->name
, BAD_CAST("entrypoint"), 11)) {
132 str
= xmlGetProp(node
, BAD_CAST("area"));
133 xml
.entry
.area
= (char*)str
;
135 str
= xmlGetProp(node
, BAD_CAST("x"));
136 xml
.entry
.coords
.x
= atol((char*)str
);
138 str
= xmlGetProp(node
, BAD_CAST("y"));
139 xml
.entry
.coords
.y
= atol((char*)str
);
141 str
= xmlGetProp(node
, BAD_CAST("z"));
142 xml
.entry
.coords
.z
= atol((char*)str
);
144 if (!xmlStrncmp(node
->name
, BAD_CAST("scripts"), 13)) {
145 node
= node
->xmlChildrenNode
; // decend
147 if (!xmlStrncmp(node
->name
, BAD_CAST("script"), 7)) {
148 str
= xmlNodeGetContent(node
);
149 xml
.scripts
.push_back((char*)str
);
156 bool World::loadArea(const std::string
& areaName
, coord_t playerPos
)
158 Area
* newArea
= new Area(rc
, this, player
.get(), areaName
);
163 player
->setArea(area
);
164 player
->setCoordsByTile(playerPos
);