This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
''' | ||
@author: Vincent Petry <PVince81@yahoo.fr> | ||
''' | ||
from xml.etree import ElementTree | ||
|
||
import model | ||
|
||
class WorldLoader(object): | ||
def __init__(self, dataPath = "data/"): | ||
self.dataPath = dataPath | ||
|
||
def _processNodes(self, world, nodesElement): | ||
nodes = {} | ||
for nodeElement in nodesElement.iter(): | ||
if not nodeElement.tag == "node": | ||
continue | ||
id = nodeElement.get("id") | ||
x = int(nodeElement.get("x", 0)) | ||
y = int(nodeElement.get("y", 0)) | ||
node = world.createNode((x, y)) | ||
if id: | ||
nodes[id] = node | ||
return nodes | ||
|
||
def _processEdges(self, world, nodes, edgesElement): | ||
for edgeElement in edgesElement.iter(): | ||
if not edgeElement.tag == "edge": | ||
continue | ||
source = edgeElement.get("source") | ||
dest = edgeElement.get("dest") | ||
reverse = edgeElement.get("reverse", False) | ||
if nodes.has_key(source) and nodes.has_key(dest): | ||
world.connectNodeWithJoint(nodes[source], nodes[dest], reverse) | ||
|
||
def _processEntities(self, world, nodes, entitiesElement): | ||
for entityElement in entitiesElement.iter(): | ||
startNodeId = entityElement.get("node") | ||
startNode = None | ||
if nodes.has_key(startNodeId): | ||
startNode = nodes[startNodeId] | ||
|
||
if entityElement.tag == "player": | ||
world.startNode = startNode | ||
elif entityElement.tag == "foe": | ||
foeType = entityElement.get("type") | ||
if foeType == "simple" or foeType == "0": | ||
foe = world.createSimpleFoe(startNode) | ||
else: | ||
foe = world.createTrackingFoe(startNode) | ||
|
||
def loadWorld(self, num): | ||
tree = ElementTree.parse("%slevel%i.xml" % (self.dataPath, num)) | ||
root = tree.getroot() | ||
world = model.World() | ||
for element in root.iter(): | ||
if element.tag == "nodes": | ||
nodesElement = element | ||
elif element.tag == "edges": | ||
edgesElement = element | ||
elif element.tag == "entities": | ||
entitiesElement = element | ||
|
||
nodes = self._processNodes(world, nodesElement) | ||
self._processEdges(world, nodes, edgesElement) | ||
self._processEntities(world, nodes, entitiesElement) | ||
|
||
return world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<level> | ||
<nodes> | ||
<node id="1" x="100" y="100" /> | ||
<node id="2" x="200" y="100" /> | ||
<node id="3" x="140" y="160" /> | ||
<node id="4" x="140" y="200" /> | ||
<node id="5" x="220" y="220" /> | ||
<node id="6" x="70" y="180" /> | ||
<node id="7" x="260" y="200" /> | ||
<node id="8" x="260" y="80" /> | ||
<node id="9" x="140" y="50" /> | ||
</nodes> | ||
<edges> | ||
<edge source="1" dest="2" /> | ||
<edge source="1" dest="3" /> | ||
<edge source="2" dest="3" /> | ||
<edge source="3" dest="4" /> | ||
<edge source="4" dest="5" /> | ||
<edge source="2" dest="5" reverse="true"/> | ||
<edge source="4" dest="6" reverse="true"/> | ||
<edge source="6" dest="1" /> | ||
<edge source="4" dest="7" /> | ||
<edge source="7" dest="5" /> | ||
<edge source="7" dest="8" /> | ||
<edge source="2" dest="8" /> | ||
<edge source="9" dest="8" reverse="true"/> | ||
<edge source="9" dest="1" reverse="true"/> | ||
<edge source="9" dest="3" reverse="true"/> | ||
</edges> | ||
<entities> | ||
<player node="4"/> | ||
<foe type="simple" node="1"/> | ||
<foe type="tracking" node="2"/> | ||
<foe type="tracking" node="9"/> | ||
</entities> | ||
</level> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.