Fixed instant crash in last commit by adding valid testdata.yml, and fixed testdata.yml parsing.

This commit is contained in:
Patrick Marsee 2019-10-14 12:02:13 -04:00
parent 9cda61a895
commit aba0014e27
4 changed files with 39 additions and 14 deletions

View file

@ -555,23 +555,28 @@ Object can be the name of the object, or its coordinates."""
data = None data = None
with open(dataFile, 'r') as f: with open(dataFile, 'r') as f:
data = yaml.load(f) data = yaml.load(f)
# In this context, 'singleton' means a 'thing' that can be accessed by
# any map. This is useful for when you have major characters who will
# show up in many scenes, and their inventory must stay the same, for
# example.
if 'singletons' in data: if 'singletons' in data:
for datum in data['singletons']: for thing in data['singletons']:
thing = data['singletons'][datum] #thing = data['singletons'][datum]
if not isinstance(thing, _gt.Thing): if not isinstance(thing, _gt.Thing):
print("Non-thing in singletons, ignoring.\n", _sys.stderr)
continue continue
thing.thingID = self.nextThing thing.thingID = self.nextThing
self.nextThing += 1 self.nextThing += 1
if thing.thingType in 'iun': if thing.thingType in 'iun':
nextThing = self.addThingRecursive(thing.customValues, nextThing) self.nextThing = _gm.GameMap.addThingRecursive(thing.customValues, self.nextThing)
if thing.thingType == 'n': if thing.thingType == 'n':
for i in thing.tempInventory: for i in thing.tempInventory:
if i.thingID == -1: if i.thingID == -1:
i.thingID = nextThing i.thingID = self.nextThing
nextThing = self.addThingRecursive(i.customValues, nextThing + 1) self.nextThing = _gm.GameMap.addThingRecursive(i.customValues, self.nextThing + 1)
thing.addThing(i) thing.addThing(i)
del thing.tempInventory del thing.tempInventory
self.singletons = dict(data[singletons]) self.singletons = list(data['singletons'])
return data return data
def gameEventLoop(self): def gameEventLoop(self):

View file

@ -220,12 +220,12 @@ list of lists of tuples."""
# Some things, like containers, have other things as custom values, # Some things, like containers, have other things as custom values,
# so they need IDs as well. # so they need IDs as well.
if thing.thingType in 'iun': if thing.thingType in 'iun':
nextThing = self.addThingRecursive(thing.customValues, nextThing) nextThing = GameMap.addThingRecursive(thing.customValues, nextThing)
if thing.thingType == 'n': if thing.thingType == 'n':
for i in thing.tempInventory: for i in thing.tempInventory:
if i.thingID == -1: if i.thingID == -1:
i.thingID = nextThing i.thingID = nextThing
nextThing = self.addThingRecursive(i.customValues, nextThing + 1) nextThing = GameMap.addThingRecursive(i.customValues, nextThing + 1)
thing.addThing(i) thing.addThing(i)
del thing.tempInventory del thing.tempInventory
pos = self.coordsToInt(thing.x, thing.y) pos = self.coordsToInt(thing.x, thing.y)
@ -242,21 +242,22 @@ list of lists of tuples."""
self.persistent.append(thing.thingID) self.persistent.append(thing.thingID)
return nextThing return nextThing
def addThingRecursive(self, container, nextThing = 0): @staticmethod
def addThingRecursive(container, nextThing = 0):
if isinstance(container, _gt.Thing): if isinstance(container, _gt.Thing):
if container.thingID == -1: if container.thingID == -1:
container.thingID = nextThing container.thingID = nextThing
nextThing = self.addThingRecursive(container.customValues, nextThing) nextThing = GameMap.addThingRecursive(container.customValues, nextThing)
return nextThing + 1 return nextThing + 1
else: else:
return nextThing return nextThing
elif isinstance(container, dict): elif isinstance(container, dict):
for i in container: for i in container:
nextThing = self.addThingRecursive(container[i], nextThing) nextThing = GameMap.addThingRecursive(container[i], nextThing)
return nextThing return nextThing
elif isinstance(container, list): elif isinstance(container, list):
for i in container: for i in container:
nextThing = self.addThingRecursive(i, nextThing) nextThing = GameMap.addThingRecursive(i, nextThing)
return nextThing return nextThing
else: else:
return nextThing return nextThing

View file

@ -34,10 +34,10 @@ class GameShell(Shell):
self.gameTitle = data['title'] self.gameTitle = data['title']
self.startLevel = 'testing/test1.yml' # should be changed for actual games self.startLevel = 'testing/test1.yml' # should be changed for actual games
if 'startLevel' in data: if 'startLevel' in data:
self.gameTitle = data['startLevel'] self.startLevel = data['startLevel']
self.openingText = '{}\nIn Development' self.openingText = '{}\nIn Development'
if 'openingText' in data: if 'openingText' in data:
self.gameTitle = data['openingText'] self.openingText = data['openingText']
self.ps2 = '?> ' self.ps2 = '?> '
self.__inGame = False self.__inGame = False

19
testing/testdata.yml Normal file
View file

@ -0,0 +1,19 @@
%YAML 1.2
---
title: Sample Text
openingText: "{}\nsample text"
startLevel: testing/test1.yml
singletons:
- !NPC
name: follower
description: a follower
location: [0, 0]
behaviors:
go: [-1, follow]
arrive: [-1, follow]
customValues:
follow:
distance: 2
isFollowing: True
target: You