1 /******************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <Gosu/Utility.hpp>
8 #include <libxml/parser.h>
9 #include <libxml/tree.h>
14 #include "resourcer.h"
18 static World
* globalWorld
= NULL
;
20 World
* World::getWorld()
25 World::World(Resourcer
* rc
, GameWindow
* wnd
)
26 : rc(rc
), wnd(wnd
), area(NULL
), player(NULL
)
39 if (!processDescriptor()) // Try to load in descriptor.
42 // FIXME The player entity doesn't have a descriptor yet.
43 player
= new Entity(rc
, NULL
, "_NONE_", xml
.playersprite
);
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
);
61 bool World::needsRedraw() const
63 return area
->needsRedraw();
66 bool World::processDescriptor()
68 static const std::string descriptor
= "world.conf";
71 XMLDocRef doc
= rc
->getXMLDoc(descriptor
);
74 const xmlNode
* root
= xmlDocGetRootElement(doc
.get());
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))
95 else if (xmlStrncmp(str
, BAD_CAST("network"), 8))
96 xml
.locality
= NETWORK
;
99 Log::err(descriptor
, "Invalid <type> value");
103 str
= xmlGetProp(node
, BAD_CAST("movement"));
105 if (xmlStrncmp(str
, BAD_CAST("turn"), 5))
108 else if (xmlStrncmp(str
, BAD_CAST("tile"), 5))
111 else if (xmlStrncmp(str
, BAD_CAST("notile"), 7))
112 xml
.movement
= NOTILE
;
115 Log::err(descriptor
, "Invalid <locality> value");
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
);
148 bool World::loadArea(const std::string
& areaName
, coord_t playerPos
)
150 Area
* newArea
= new Area(rc
, this, player
, areaName
);
155 player
->setArea(area
);
156 player
->setCoordsByTile(playerPos
);