added "info" and "wear" use functions, and fixed many bugs related to storing things by ID.
This commit is contained in:
parent
1b41c46105
commit
8355dccb33
4 changed files with 165 additions and 44 deletions
53
gamemap.py
53
gamemap.py
|
@ -409,6 +409,29 @@ class MapExit(Thing):
|
|||
return cls(parts['name'], parts['location'][0], parts['location'][1],
|
||||
parts['id'], parts['destination'], prefix, graphic)
|
||||
|
||||
class MapEntrance(Thing):
|
||||
yaml_flag = u'!MapEntrance'
|
||||
# no graphic - should not be drawn
|
||||
|
||||
def __init__(self, x: int, y: int, exitid: int):
|
||||
if prefix:
|
||||
description = "{0} {1}".format(prefix, name)
|
||||
super(MapEntrance, self).__init__('a', name, x, y, description, 1)
|
||||
self.exitid = exitid
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer, node):
|
||||
# save usual things
|
||||
ret = {'location': (node.x, node.y), 'id': node.exitid}
|
||||
return representer.represent_mapping(cls.yaml_flag, ret)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, constructor, node):
|
||||
parts = CommentedMap()
|
||||
constructor.construct_mapping(node, parts, True)
|
||||
# set default values for optional arguments
|
||||
return cls(parts['location'][0], parts['location'][1], parts['id'])
|
||||
|
||||
class MapError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
@ -575,7 +598,6 @@ Entering a map through stdin will be obsolete once testing is over."""
|
|||
|
||||
# generate matrix and graph first
|
||||
mapMatrix, mapGraph, dimensions = GameMap.parseMatrix(mat)
|
||||
playerStart = (-1, -1)
|
||||
level = GameMap(infile, mapGraph, mapMatrix, dimensions)
|
||||
|
||||
# Now, load other info
|
||||
|
@ -664,6 +686,7 @@ list of lists of tuples."""
|
|||
level.wallColors.append('#7F3F0F')
|
||||
|
||||
# get things
|
||||
hasKnownEntrance = False
|
||||
if 'loadOnce' in info and not preLoaded:
|
||||
for thing in info['loadOnce']:
|
||||
#print(type(thing))
|
||||
|
@ -672,8 +695,9 @@ list of lists of tuples."""
|
|||
for thing in info['loadAlways']:
|
||||
#print(type(thing))
|
||||
nextThing = level.addThing(thing, nextThing)
|
||||
if thing.thingType == 'x' and prevMap == thing.exitid:
|
||||
if ((thing.thingType == 'x' and not hasKnownEntrance) or thing.thingType == 'a') and prevMap == thing.exitid:
|
||||
level.playerStart = (thing.x, thing.y)
|
||||
hasKnownEntrance = True
|
||||
return nextThing
|
||||
|
||||
# stuff the gameshell itself might use
|
||||
|
@ -686,6 +710,14 @@ list of lists of tuples."""
|
|||
if thing.thingID == -1: # This is to ensure that we don't double up IDs.
|
||||
thing.thingID = nextThing
|
||||
nextThing += 1
|
||||
# Some things, like containers, have other things as custom values,
|
||||
# so they need IDs as well.
|
||||
if thing.thingType in 'iuc':
|
||||
nextThing = self.addThingRecursive(thing.customValues, nextThing)
|
||||
if thing.thingType == 'c':
|
||||
for i in thing.inventory:
|
||||
i.thingID = nextThing
|
||||
nextThing += 1
|
||||
pos = self.coordsToInt(thing.x, thing.y)
|
||||
if pos not in self.thingPos:
|
||||
self.thingPos[pos] = [thing.thingID]
|
||||
|
@ -700,6 +732,21 @@ list of lists of tuples."""
|
|||
self.persistent.append(thing.thingID)
|
||||
return nextThing
|
||||
|
||||
def addThingRecursive(self, container, nextThing = 0):
|
||||
if isinstance(container, Thing):
|
||||
container.thingID = nextThing
|
||||
return nextThing + 1
|
||||
elif isinstance(container, dict):
|
||||
for i in container:
|
||||
nextThing = self.addThingRecursive(container[i], nextThing)
|
||||
return nextThing
|
||||
elif isinstance(container, list):
|
||||
for i in container:
|
||||
nextThing = self.addThingRecursive(i, nextThing)
|
||||
return nextThing
|
||||
else:
|
||||
return nextThing
|
||||
|
||||
def getThing(self, **kwargs):
|
||||
if 'name' in kwargs:
|
||||
return self.getThingByName(kwargs['name'])
|
||||
|
@ -885,7 +932,7 @@ The closeEnough parameter will create a path that lands beside the source if nec
|
|||
if len(self.thingPos[oldPos]) == 0:
|
||||
del self.thingPos[oldPos]
|
||||
oldName = thing.name
|
||||
if oldName in self.thingPos:
|
||||
if oldName in self.thingNames:
|
||||
self.thingNames[oldName].remove(thing.thingID)
|
||||
if len(self.thingNames[oldName]) == 0:
|
||||
del self.thingNames[oldName]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue