Rejiggered players to have a thing in the map, and made the inventory system better.

This commit is contained in:
Patrick Marsee 2019-07-04 18:48:47 -04:00
parent ed7d265b48
commit 5010042430
4 changed files with 347 additions and 287 deletions

View file

@ -46,7 +46,6 @@ class GameShell(Shell):
self.gameBase.registerIO('dialog', self.dialog)
self.gameBase.registerIO('info', self.info)
self.gameBase.registerIO('playercmd', self.playercmd)
self.menuMode()
# Helper functions
@ -148,25 +147,33 @@ If -l is given, a map legend will be printed under the map."""
level = self.gameBase.level
textColor = level.wallColors[0]
floorColor = level.floorColors[0]
priorities = {'p': 1, 'n': 2, 'i': 3, 'u': 4, 'd': 5, 'x': 6, 'a': 7}
for y in range(level.dimensions[1]):
rows.append(['{0}{1:2} {2}{3}'.format(self.clearColor(), y, self.color(textColor[1:]), self.color(floorColor[1:], fg = False))])
rows[-1].append(self.color(textColor[1:]))
for x in range(level.dimensions[0]):
pos = level.mapMatrix[y][x]
thing = level.getThingAtPos(index)
if x == self.gameBase.playerx and y == self.gameBase.playery:
if '#0000FF' != textColor:
textColor = '#0000FF'
rows[-1].append(self.color(textColor[1:]))
rows[-1].append('()')
elif thing:
things = level.getThingsAtPos(index)
#if x == self.gameBase.playerx and y == self.gameBase.playery:
# if self.gameBase.player != textColor:
# textColor = '#0000FF'
# rows[-1].append(self.color(textColor[1:]))
# rows[-1].append('()')
if len(things) > 0:
# Prioritize types: p, n, i, u, d, x, a
thing = things[0]
for i in things[1:]:
if priorities[i.thingType] < priorities[thing.thingType]:
thing = i
if thing.graphic[1] != textColor:
textColor = thing.graphic[1]
rows[-1].append(self.color(textColor[1:]))
if thing.thingType == 'x': # exit
if thing.thingType == 'p': # player
rows[-1].append('()')
elif thing.thingType == 'x': # exit
rows[-1].append('X{0}'.format(thing.exitid))
exits[thing.exitid] = (thing.name, thing.graphic[1])
elif thing.thingType == 'c': # useable
elif thing.thingType == 'n': # NPC
characters[len(characters)+1] = (thing.name, thing.graphic[1])
rows[-1].append('C{0}'.format(len(characters)))
elif thing.thingType == 'd': # door
@ -178,7 +185,7 @@ If -l is given, a map legend will be printed under the map."""
elif thing.thingType == 'i': # item
items[len(items)+1] = (thing.name, thing.graphic[1])
rows[-1].append('I{0}'.format(len(items)))
elif thing.thingType == 'a': # entrance
else: # entrance
rows[-1].append(' ')
elif pos[0] == 'w':
if level.wallColors[level.mapMatrix[y][x][1]] != textColor:
@ -245,12 +252,12 @@ If -l is given, a map legend will be printed under the map."""
ret.append("custom values:")
for i in self.gameBase.customValues:
ret.append("{0:<22}: {1}".format(i, self.gameBase.customValues[i]))
ret.append("Player name:{0:.>68}".format(self.gameBase.playerName))
ret.append("Player position:{0:.>64}".format("{0}{1}".format(self.gameBase.numberToLetter(self.gameBase.playerx), self.gameBase.playery)))
ret.append("Prev. position:{0:.>65}".format("{0}{1}".format(self.gameBase.numberToLetter(self.gameBase.prevx), self.gameBase.prevy)))
ret.append("Player name:{0:.>68}".format(self.gameBase.player.name))
ret.append("Player position:{0:.>64}".format("{0}{1}".format(self.gameBase.numberToLetter(self.gameBase.player.x), self.gameBase.player.y)))
ret.append("Prev. position:{0:.>65}".format("{0}{1}".format(self.gameBase.numberToLetter(self.gameBase.player.prevx), self.gameBase.player.prevy)))
ret.append("Inventory:")
for i in self.gameBase.playerInv:
ret.append("{0:<8}: {1}".format(i, self.gameBase.playerInv[i].name))
for i in self.gameBase.player.inventory:
ret.append("{0:<8}: {1}".format(i.thingID, i.name))
ret.append("Things:\nID Name X Y")
for i in self.gameBase.level.things:
j = self.gameBase.level.things[i]
@ -263,7 +270,7 @@ If -l is given, a map legend will be printed under the map."""
return
def inv(self, args):
print('\n'.join([self.gameBase.playerInv[i].name for i in self.gameBase.playerInv]))
print('\n'.join([i for i in self.gameBase.player.thingNames]))
def newGame(self, args):
if self.__inGame:
@ -351,22 +358,23 @@ If -l is given, a map legend will be printed under the map."""
# IO calls
def container(self, inv, cont):
"""container IO"""
def container(self, player, cont):
"""container IO
Player is modified through side-effect."""
# Pretty print: get length of the longest inventory item's name
longestLen = 0
for i in inv:
if len(inv[i].name) > longestLen:
longestLen = len(inv[i].name)
for i in player.thingNames:
if len(i) > longestLen:
longestLen = len(i)
if longestLen > 0:
inv = player.inventory # do this assignment because player.inventory is O(n)
print('{{0:<{0}}}{1}'.format(max(6, longestLen+2), "Container:").format("Inv:"))
i = 0
invKeys = tuple(inv.keys())
while i < len(invKeys) and i < len(cont):
print('{{0:<{0}}}{1}'.format(longestLen+2, cont[i].name).format(inv[invKeys[i]].name))
while i < len(inv) and i < len(cont):
print('{{0:<{0}}}{1}'.format(longestLen+2, cont[i].name).format(inv[i].name))
i += 1
while i < len(invKeys):
print(inv[invKeys[i]].name)
while i < len(inv):
print(inv[i].name)
i += 1
while i < len(cont):
print(' '*(longestLen+2) + cont[i].name)
@ -387,26 +395,27 @@ If -l is given, a map legend will be printed under the map."""
thing = ' '.join(instr[1:])
for i in range(len(cont)):
if thing == cont[i].name:
inv[cont[i].thingID] = cont[i]
player.addThing(cont[i])
del cont[i]
timeSpent += 0.5
print("{0} taken.".format(thing))
break
else:
# If it got here, it didn't find it.
print("No {0} in container.".format(thing))
elif instr[0] == "store":
# store something in the container
if instr[1] == "the":
del instr[1]
thingName = ' '.join(instr[1:])
for i in inv:
thing = inv[i].name
if thing == thingName:
cont.append(inv[i])
del inv[i]
print("{0} stored.".format(thing))
timeSpent += 0.5
break # so that all things with the same name don't get stored
if thingName in player.thingNames:
cont.append(player.removeThingByName(thingName))
print("{0} stored.".format(thingName))
timeSpent += 0.5
else:
print("No {0} in inventory.".format(thingName))
instr = input("Take, store, or exit: ")
return inv, cont, timeSpent
return cont, timeSpent
def info(self, items):
"""IO for collections of information"""
@ -532,4 +541,5 @@ If -l is given, a map legend will be printed under the map."""
if __name__ == '__main__':
sh = GameShell(GameBase())
sh.menuMode()
sh.run()