The player can now have simple conversations with NPCs.
This commit is contained in:
parent
e0b88e7a45
commit
e81345e793
4 changed files with 115 additions and 10 deletions
42
gameshell.py
42
gameshell.py
|
@ -7,9 +7,12 @@ import sys as _sys
|
|||
import heapq
|
||||
#import gamemap
|
||||
import gameevents
|
||||
#from os import get_terminal_size
|
||||
import textwrap as _tw
|
||||
from shutil import get_terminal_size as _gts
|
||||
#import random
|
||||
|
||||
TERM_SIZE = _gts()[0]
|
||||
|
||||
class GameShell(Shell):
|
||||
|
||||
UP = 1
|
||||
|
@ -24,6 +27,7 @@ class GameShell(Shell):
|
|||
self.outstream = _sys.stdout
|
||||
self.gameBase = gameBase
|
||||
self.colorMode = 0
|
||||
self.ps2 = '?> '
|
||||
|
||||
# register functions
|
||||
|
||||
|
@ -33,6 +37,7 @@ class GameShell(Shell):
|
|||
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)
|
||||
|
@ -50,6 +55,7 @@ class GameShell(Shell):
|
|||
self.registerCommand('colorTest', self.colorTest)
|
||||
self.registerAlias('run', ['go', '-r'])
|
||||
self.gameBase.registerIO('container', self.container)
|
||||
self.gameBase.registerIO('dialog', self.dialog)
|
||||
|
||||
# Helper functions
|
||||
|
||||
|
@ -111,6 +117,7 @@ If -l is given, a map legend will be printed under the map."""
|
|||
rows = []
|
||||
index = 0
|
||||
exits = {}
|
||||
characters = {}
|
||||
doors = {}
|
||||
useables = {}
|
||||
items = {}
|
||||
|
@ -135,6 +142,9 @@ If -l is given, a map legend will be printed under the map."""
|
|||
if 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
|
||||
characters[len(characters)+1] = (thing.name, thing.graphic[1])
|
||||
rows[-1].append('C{0}'.format(len(characters)))
|
||||
elif thing.thingType == 'd': # door
|
||||
doors[len(doors)+1] = (thing.name, thing.graphic[1])
|
||||
rows[-1].append('D{0}'.format(len(doors)))
|
||||
|
@ -173,6 +183,9 @@ If -l is given, a map legend will be printed under the map."""
|
|||
"Xn - Exit to another area"]
|
||||
for i in exits:
|
||||
legend.append(' {0}X{1}{2} - {3}'.format(self.color(exits[i][1][1:]), i, self.clearColor(), exits[i][0]))
|
||||
legend.append("Cn - Character")
|
||||
for i in characters:
|
||||
legend.append(' {0}U{1}{2} - {3}'.format(self.color(characters[i][1][1:]), i, self.clearColor(), characters[i][0]))
|
||||
legend.append("Un - Useable object")
|
||||
for i in useables:
|
||||
legend.append(' {0}U{1}{2} - {3}'.format(self.color(useables[i][1][1:]), i, self.clearColor(), useables[i][0]))
|
||||
|
@ -262,6 +275,33 @@ If -l is given, a map legend will be printed under the map."""
|
|||
instr = input("Take, store, or exit: ")
|
||||
return inv, cont, timeSpent
|
||||
|
||||
def dialog(self, dialogObj):
|
||||
if 'opener' in dialogObj:
|
||||
print(_tw.fill(dialogObj['opener'], width = TERM_SIZE))
|
||||
while isinstance(dialogObj, dict):
|
||||
if 'action' in dialogObj:
|
||||
action = dialogObj['action']
|
||||
if action == 'answer':
|
||||
answer = 0
|
||||
if 'answers' in dialogObj and isinstance(dialogObj['answers'], list):
|
||||
for i in range(len(dialogObj['answers'])):
|
||||
print(_tw.fill('{}: {}'.format(i+1, dialogObj['answers'][i]), width = TERM_SIZE))
|
||||
answer = int(input(self.ps2)) - 1
|
||||
if 'replies' in dialogObj and isinstance(dialogObj['replies'], list):
|
||||
ret = self.dialog(dialogObj['replies'][answer])
|
||||
if ret > 1:
|
||||
return ret - 1
|
||||
elif ret == 0:
|
||||
return 0
|
||||
# if ret == 1, then do this dialog again
|
||||
|
||||
elif len(action) >= 4 and action[:4] == 'back':
|
||||
if len(action) == 4:
|
||||
return 1
|
||||
return int(action[4:])
|
||||
elif action == 'exit':
|
||||
return 0
|
||||
|
||||
def update(self):
|
||||
self.gameBase.gameEventLoop()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue