various tweaks and fixes
This commit is contained in:
parent
ed7fe60a6d
commit
ed7d265b48
5 changed files with 524 additions and 212 deletions
202
gameshell.py
202
gameshell.py
|
@ -27,36 +27,26 @@ class GameShell(Shell):
|
|||
self.outstream = _sys.stdout
|
||||
self.gameBase = gameBase
|
||||
self.colorMode = 0
|
||||
self.gameTitle = 'Game Shell' # should be changed for actual games
|
||||
self.startLevel = 'testing/test1.yml' # should be changed for actual games
|
||||
self.openingText = '{}\nIn Development'
|
||||
self.ps2 = '?> '
|
||||
self.__inGame = False
|
||||
|
||||
# register functions
|
||||
|
||||
self.registerCommand('map', self.showMap)
|
||||
self.registerCommand('ls', self.showMap)
|
||||
self.registerCommand('go', self.gameBase.go)
|
||||
self.registerCommand('move', self.gameBase.go)
|
||||
self.registerCommand('walk', self.gameBase.go)
|
||||
self.registerCommand('look', self.gameBase.look)
|
||||
self.registerCommand('talk', self.gameBase.talk)
|
||||
self.registerCommand('use', self.gameBase.use)
|
||||
self.registerCommand('loadMap', self.gameBase.loadMap)
|
||||
self.registerCommand('man', self.man)
|
||||
self.registerCommand('help', self.man)
|
||||
self.registerCommand('save', self.gameBase.saveGame)
|
||||
self.registerCommand('load', self.gameBase.loadGame)
|
||||
self.registerCommand('take', self.gameBase.take)
|
||||
self.registerCommand('get', self.gameBase.take)
|
||||
self.registerCommand('drop', self.gameBase.drop)
|
||||
self.registerCommand('inv', self.inv)
|
||||
self.registerCommand('bag', self.inv)
|
||||
self.registerCommand('items', self.inv)
|
||||
self.registerCommand('status', self.status)
|
||||
self.registerCommand('load', self.gameBase.loadGame) # should always be available
|
||||
self.registerCommand('flippetywick', self.devMode)
|
||||
self.registerCommand('options', self.options)
|
||||
self.registerCommand('colorTest', self.colorTest)
|
||||
self.registerAlias('run', ['go', '-r'])
|
||||
self.registerCommand('man', self.man)
|
||||
self.registerCommand('help', self.man)
|
||||
self.registerCommand('new', self.newGame)
|
||||
self.gameBase.registerIO('container', self.container)
|
||||
self.gameBase.registerIO('dialog', self.dialog)
|
||||
self.gameBase.registerIO('info', self.info)
|
||||
self.gameBase.registerIO('playercmd', self.playercmd)
|
||||
self.menuMode()
|
||||
|
||||
# Helper functions
|
||||
|
||||
|
@ -108,6 +98,39 @@ class GameShell(Shell):
|
|||
self.__colorTestRoutine(0, 128) # dark
|
||||
self.__colorTestRoutine(0, 256) # medium
|
||||
self.__colorTestRoutine(128, 256) # light
|
||||
|
||||
def __getAdjFloors(self, level, x, y):
|
||||
"""Get the floor colors of the 8 nearest tiles."""
|
||||
adjFloors = []
|
||||
for i in range(y-1, y+2):
|
||||
if i < 0 or i >= level.dimensions[1]:
|
||||
continue
|
||||
for j in range(x-1, x+2):
|
||||
if j < 0 or j >= level.dimensions[0]:
|
||||
continue
|
||||
if level.mapMatrix[i][j][0] == 'e':
|
||||
adjFloors.append(level.mapMatrix[i][j][1])
|
||||
return adjFloors
|
||||
|
||||
def __getUnderWallColor(self, level, x, y):
|
||||
adjFloors = self.__getAdjFloors(level, x, y)
|
||||
if len(adjFloors) > 0:
|
||||
counts = {}
|
||||
highestCount = 0
|
||||
ret = -1
|
||||
for i in adjFloors:
|
||||
if i in counts:
|
||||
counts[i] += 1
|
||||
else:
|
||||
counts[i] = 1
|
||||
if counts[i] > highestCount:
|
||||
ret = i
|
||||
highestCount = counts[i]
|
||||
elif counts[i] == highestCount and i < ret:
|
||||
ret = i
|
||||
return ret
|
||||
else:
|
||||
return -1
|
||||
|
||||
def showMap(self, args):
|
||||
"""map [-l]
|
||||
|
@ -170,8 +193,15 @@ If -l is given, a map legend will be printed under the map."""
|
|||
sides += GameShell.DOWN
|
||||
if x > 0 and level.mapMatrix[y][x-1][0] == 'w':
|
||||
sides += GameShell.LEFT
|
||||
underWallColor = self.__getUnderWallColor(level, x, y)
|
||||
if level.floorColors[underWallColor] != floorColor and underWallColor != -1:
|
||||
floorColor = level.floorColors[underWallColor]
|
||||
rows[-1].append(self.color(floorColor[1:], fg = False))
|
||||
rows[-1].append(GameShell.WALLS[sides])
|
||||
else:
|
||||
if level.floorColors[level.mapMatrix[y][x][1]] != floorColor:
|
||||
floorColor = level.floorColors[level.mapMatrix[y][x][1]]
|
||||
rows[-1].append(self.color(floorColor[1:], fg = False))
|
||||
rows[-1].append(' ')
|
||||
index += 1
|
||||
rows[-1] = ''.join(rows[-1])
|
||||
|
@ -235,26 +265,108 @@ If -l is given, a map legend will be printed under the map."""
|
|||
def inv(self, args):
|
||||
print('\n'.join([self.gameBase.playerInv[i].name for i in self.gameBase.playerInv]))
|
||||
|
||||
def newGame(self, args):
|
||||
if self.__inGame:
|
||||
response = input("Do you want to save before exiting (Y/n/x)? ")
|
||||
if len(response) > 0:
|
||||
if response[0] in 'Nn':
|
||||
super(GameShell, self).exitShell(args)
|
||||
elif response[0] in 'Xx': # cancel
|
||||
return
|
||||
else:
|
||||
sf = input('Save file: ')
|
||||
if len(sf) > 0:
|
||||
gameBase.saveGame([sf])
|
||||
else:
|
||||
print('No save file given, cancelling exit.')
|
||||
else:
|
||||
sf = input('Save file: ')
|
||||
if len(sf) > 0:
|
||||
gameBase.saveGame([sf])
|
||||
else:
|
||||
print('No save file given, cancelling exit.')
|
||||
try:
|
||||
self.gameBase.loadMap([self.startLevel])
|
||||
except RuntimeError as e:
|
||||
print(e)
|
||||
return
|
||||
self.gameMode()
|
||||
|
||||
def gameMode(self):
|
||||
"""Mode for in-game."""
|
||||
if not self.__inGame:
|
||||
self.registerCommand('map', self.showMap)
|
||||
self.registerCommand('ls', self.showMap)
|
||||
self.registerCommand('go', self.gameBase.go)
|
||||
self.registerCommand('move', self.gameBase.go)
|
||||
self.registerCommand('walk', self.gameBase.go)
|
||||
self.registerCommand('look', self.gameBase.look)
|
||||
self.registerCommand('talk', self.gameBase.talk)
|
||||
self.registerCommand('use', self.gameBase.use)
|
||||
self.registerCommand('save', self.gameBase.saveGame)
|
||||
self.registerCommand('take', self.gameBase.take)
|
||||
self.registerCommand('get', self.gameBase.take)
|
||||
self.registerCommand('drop', self.gameBase.drop)
|
||||
self.registerCommand('inv', self.inv)
|
||||
self.registerCommand('bag', self.inv)
|
||||
self.registerCommand('items', self.inv)
|
||||
self.registerAlias('run', ['go', '-r'])
|
||||
self.__inGame = True
|
||||
|
||||
def menuMode(self, text = None):
|
||||
"""Mode for main menus and the like."""
|
||||
if self.__inGame:
|
||||
self.registerCommand('map', self.showMap)
|
||||
self.unRegisterCommand('ls', self.showMap)
|
||||
self.unRegisterCommand('go', self.gameBase.go)
|
||||
self.unRegisterCommand('move', self.gameBase.go)
|
||||
self.unRegisterCommand('walk', self.gameBase.go)
|
||||
self.unRegisterCommand('look', self.gameBase.look)
|
||||
self.unRegisterCommand('talk', self.gameBase.talk)
|
||||
self.unRegisterCommand('use', self.gameBase.use)
|
||||
self.unRegisterCommand('save', self.gameBase.saveGame)
|
||||
self.unRegisterCommand('take', self.gameBase.take)
|
||||
self.unRegisterCommand('get', self.gameBase.take)
|
||||
self.unRegisterCommand('drop', self.gameBase.drop)
|
||||
self.unRegisterCommand('inv', self.inv)
|
||||
self.unRegisterCommand('bag', self.inv)
|
||||
self.unRegisterCommand('items', self.inv)
|
||||
self.unRegisterAlias('run', ['go', '-r'])
|
||||
self.__inGame = False
|
||||
if text == None:
|
||||
print(self.openingText.format(self.gameTitle))
|
||||
else:
|
||||
print(text.format(self.gameTitle))
|
||||
|
||||
def devMode(self, args):
|
||||
print('Dev mode activated. Please dev responsibly.')
|
||||
self.gameMode()
|
||||
self.registerCommand('dev', self.runScript)
|
||||
self.registerCommand('status', self.status)
|
||||
self.registerCommand('loadMap', self.gameBase.loadMap)
|
||||
|
||||
def runScript(self, args):
|
||||
"""simple wrapper for gameBase.runscript."""
|
||||
self.gameBase.parseScript(' '.join(args))
|
||||
|
||||
# IO calls
|
||||
|
||||
def container(self, inv, cont):
|
||||
"""container IO"""
|
||||
# Pretty print: get length of the longest inventory item's name
|
||||
longestLen = 0
|
||||
longestStr = ""
|
||||
for i in inv:
|
||||
if len(i) > longestLen:
|
||||
longestLen = len(i)
|
||||
longestStr = i
|
||||
if len(inv[i].name) > longestLen:
|
||||
longestLen = len(inv[i].name)
|
||||
if longestLen > 0:
|
||||
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(invKeys[i]))
|
||||
print('{{0:<{0}}}{1}'.format(longestLen+2, cont[i].name).format(inv[invKeys[i]].name))
|
||||
i += 1
|
||||
while i < len(invKeys):
|
||||
print(invKeys[i])
|
||||
print(inv[invKeys[i]].name)
|
||||
i += 1
|
||||
while i < len(cont):
|
||||
print(' '*(longestLen+2) + cont[i].name)
|
||||
|
@ -308,7 +420,7 @@ If -l is given, a map legend will be printed under the map."""
|
|||
charsRead += len(items[instr])
|
||||
else:
|
||||
print('{} not here.'.format(instr))
|
||||
input('<strike RETURN>')
|
||||
input('<ENTER>')
|
||||
for i in items:
|
||||
print(' ', i)
|
||||
instr = input("Choose an item to view, or exit: ")
|
||||
|
@ -336,7 +448,10 @@ If -l is given, a map legend will be printed under the map."""
|
|||
return ret
|
||||
# if ret == 1 or ret == 0, go back again.
|
||||
elif 'opener' in dialogObj:
|
||||
print(_tw.fill(dialogObj['opener'], width = TERM_SIZE))
|
||||
toPrint = dialogObj['opener'].split('\n') # split by lines so that fill doesn't erase newlines
|
||||
for line in toPrint:
|
||||
print(_tw.fill(line, width = TERM_SIZE))
|
||||
input('<ENTER>')
|
||||
while isinstance(dialogObj, dict):
|
||||
if 'action' in dialogObj:
|
||||
action = dialogObj['action']
|
||||
|
@ -350,8 +465,8 @@ If -l is given, a map legend will be printed under the map."""
|
|||
if ans[0] == '?':
|
||||
condEnd = ans.index(':')
|
||||
cond = ans[1:condEnd].strip().split()
|
||||
if self.gameBase.compareValues(cond[1], cond[0], cond[2]):
|
||||
print(_tw.fill('{}: {}'.format(j+1, ans[condEnd+1:]), width = TERM_SIZE))
|
||||
if self.gameBase.compareValues(cond):
|
||||
print(_tw.fill('{}: {}'.format(j+1, ans[condEnd+1:].strip()), width = TERM_SIZE))
|
||||
j += 1
|
||||
else:
|
||||
skips.append(i)
|
||||
|
@ -389,6 +504,31 @@ If -l is given, a map legend will be printed under the map."""
|
|||
|
||||
def update(self):
|
||||
self.gameBase.gameEventLoop()
|
||||
|
||||
def exitShell(self, args):
|
||||
if self.__inGame:
|
||||
response = input("Do you want to save before exiting (Y/n/x)? ")
|
||||
if len(response) > 0:
|
||||
if response[0] in 'Nn':
|
||||
super(GameShell, self).exitShell(args)
|
||||
elif response[0] in 'Xx': # cancel
|
||||
return
|
||||
else:
|
||||
sf = input('Save file: ')
|
||||
if len(sf) > 0:
|
||||
gameBase.saveGame([sf])
|
||||
super(GameShell, self).exitShell(args)
|
||||
else:
|
||||
print('No save file given, cancelling exit.')
|
||||
else:
|
||||
sf = input('Save file: ')
|
||||
if len(sf) > 0:
|
||||
gameBase.saveGame([sf])
|
||||
super(GameShell, self).exitShell(args)
|
||||
else:
|
||||
print('No save file given, cancelling exit.')
|
||||
else:
|
||||
super(GameShell, self).exitShell(args)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sh = GameShell(GameBase())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue