Skip to content
This repository has been archived by the owner on Sep 17, 2020. It is now read-only.

Commit

Permalink
Added level loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
PVince81 committed Mar 17, 2012
1 parent c02ba1c commit 55fc0a7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 54 deletions.
67 changes: 67 additions & 0 deletions WorldLoader.py
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
5 changes: 3 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self):
class Config:
def __init__(self):
self.fullScreen = False
self.screenSize = (800, 600)
self.screenSize = (640, 480)
self.fps = 60
self.keymap = KeyMap()
self.keymap = KeyMap()
self.dataPath = "data/"
36 changes: 36 additions & 0 deletions data/level1.xml
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>
13 changes: 7 additions & 6 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import model
import view
from config import Config

from WorldLoader import WorldLoader

class Game:
def __init__(self, config):
Expand All @@ -19,9 +19,10 @@ def __init__(self, config):
self._terminated = False
self._display = None
self._world = None
self._worldNum = 0
self._worldNum = 1
self._player = None

self._worldLoader = WorldLoader(self._config.dataPath)

def _init(self):
pygame.init()
flags = 0
Expand All @@ -36,7 +37,7 @@ def _init(self):

def _quit(self):
pygame.quit()

def _movePlayer(self, direction):
if self._player.moving:
return
Expand Down Expand Up @@ -70,13 +71,13 @@ def _handleLogic(self):
self._initWorld(self._worldNum)

def _initWorld(self, worldNum):
self._world = model.World.getWorld(worldNum)
self._world = self._worldLoader.loadWorld(worldNum)
self._player = model.Player()
self._player.setCurrentNode(self._world.startNode)
self._display.setWorld(self._world)
self._display.addEntityView( view.PlayerView(self._player) )
self._world.addEntity(self._player)

# set tracking foes to track the player
for entity in self._world.entities:
if entity.entityType == 1 and entity.foeType == 1:
Expand Down
53 changes: 7 additions & 46 deletions model/__init__.py → model.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,51 +236,6 @@ def connectNodeWithJoint(self, node1, node2, reverse = False):
def hasAllEdgesMarked(self):
return self.markedEdges == len(self.edges)

@staticmethod
def getWorld(worldNum):
world = World()

if worldNum == 0:
node1 = world.createNode((100, 100))
node2 = world.createNode((200, 100))
node3 = world.createNode((140, 160))
node4 = world.createNode((140, 200))
node5 = world.createNode((220, 220))
node6 = world.createNode((70, 180))
node7 = world.createNode((260, 200))
node8 = world.createNode((260, 80))
node9 = world.createNode((140, 50))

# TODO: should figure out the direction based on coordinates
world.connectNodeWithJoint(node1, node2)
world.connectNodeWithJoint(node1, node3)
world.connectNodeWithJoint(node2, node3)
world.connectNodeWithJoint(node3, node4)
world.connectNodeWithJoint(node4, node5)
world.connectNodeWithJoint(node2, node5, 1)
world.connectNodeWithJoint(node4, node6, 1)
world.connectNodeWithJoint(node6, node1)
world.connectNodeWithJoint(node4, node7)
world.connectNodeWithJoint(node7, node5)
world.connectNodeWithJoint(node7, node8)
world.connectNodeWithJoint(node2, node8)
world.connectNodeWithJoint(node9, node8, 1)
world.connectNodeWithJoint(node9, node1, 1)
world.connectNodeWithJoint(node9, node3, 1)

world.startNode = node4
world.createSimpleFoe(node1)
world.createTrackingFoe(node2)
world.createTrackingFoe(node9)
else:
node1 = world.createNode((100, 100))
node2 = world.createNode((200, 200))
world.connectNodeWithJoint(node1, node2)
world.connectNodeWithJoint(node1, node2, 1)
world.startNode = node1

return world

class Entity(object):
entityType = -1
def __init__(self, currentNode = None):
Expand Down Expand Up @@ -394,20 +349,26 @@ def __init__(self, currentNode = None, trackedEntity = None):
self.speed = 1
self._trackedEntity = trackedEntity
self._path = []
self._sleepTicks = 0

def track(self, trackedEntity):
self._trackedEntity = trackedEntity
self._path = []

def update(self):
if self.moving:
if self._sleepTicks > 0:
self._sleepTicks -= 1
elif self.moving:
Foe.update(self)
else:
# find path to player
if len(self._path) > 0:
nextEdge = self._path[0]
self._path = self._path[1:]
self.moveAlong(nextEdge)
elif random.randint(0, 5) == 0:
# sleep for a second
self._sleepTicks = 60
elif self._trackedEntity and ( self._trackedEntity.currentNode != self.currentNode or self._trackedEntity.currentNode != self.targetNode):
targetNode = self._trackedEntity.currentNode
if self._trackedEntity.moving:
Expand Down
File renamed without changes.

0 comments on commit 55fc0a7

Please sign in to comment.