* pyoo/interpret.py: Interpreter: Added a place cache for looking up places by name. remove try around handle_enter and handle_move. * pyoo/placeloader.py: New class. Loads a simple textual fomat for a set of potentially connetcted places. * pyoo/things.py: Added go <direction> verb to Place. Separated go mechanic to do_go. * roomtest.txt: A small set of connected places to test PlaceLoader. * testrooms.py: A small adventure that loads roomtest.txt, demonstrating PlaceLoader and subclassing Place.main
parent
8bb60b4203
commit
5fdb1512a4
@ -0,0 +1,46 @@ |
||||
|
||||
from .things import Place |
||||
|
||||
(ST_START, ST_DESC, ST_EXITS) = range(3) |
||||
|
||||
class PlaceLoader(object): |
||||
def __init__(self, infile, baseplace=Place): |
||||
"""Create the factory by reading the contents of infile (an iteratable object [file-like or otherwise]). Specify the base class for place with baseplace.""" |
||||
self.base = baseplace |
||||
self.places = dict() #indexed by name |
||||
|
||||
if infile: |
||||
self.process_file(infile) |
||||
|
||||
def process_file(self, inf): |
||||
state = ST_START |
||||
rm = None |
||||
desc = list() |
||||
for line in inf: |
||||
line = line.strip() |
||||
if state == ST_START: |
||||
rm = self.base(line) |
||||
rm.temp_exits = list() |
||||
state = ST_DESC |
||||
desc = list() |
||||
elif state == ST_DESC: |
||||
if line == '.': |
||||
state = ST_EXITS |
||||
rm.description = desc |
||||
else: |
||||
desc.append(line) |
||||
elif state == ST_EXITS: |
||||
if line == '.': |
||||
state = ST_START |
||||
self.places[rm.name] = rm |
||||
rm = None |
||||
else: |
||||
rm.temp_exits.append(line) |
||||
|
||||
# assemble the exits |
||||
for place in self.places.values(): |
||||
for ext in place.temp_exits: |
||||
names, destination = ext.split(' ',1) |
||||
for nm in names.split(','): |
||||
place.ways[nm] = self.places[destination] |
||||
place.update_go() |
@ -0,0 +1,40 @@ |
||||
Porch |
||||
This is a small, fairly nondescript porch covered on two sides with a neck-high fence. It is made of old boards nailed down instead of screwed. |
||||
|
||||
To the north is the front door, slightly ajar. |
||||
. |
||||
n,north Play Room |
||||
. |
||||
Play Room |
||||
This is a medium-sized room with light wood parcade floor. There are toys of every description strewn about willy-nilly, two large baskets of stuffed animals and various tables adorned with books and drawing supplies. Large south-facing windows stream bright winter sunlight, warming the room. |
||||
|
||||
To the south is the front door. To the east the room empties into the living room, to the north it continues into the hallway. |
||||
. |
||||
s,south Porch |
||||
n,north Hallway |
||||
e,east Living Room |
||||
. |
||||
Hallway |
||||
This is a narrow wood-floored hallway, echoing with every sound. |
||||
|
||||
Several doors connect to the hallway, all closed. It also contiues to the south into the play room, and east into the kitchen. |
||||
. |
||||
s,south Play Room |
||||
e,east Kitchen |
||||
. |
||||
Kitchen |
||||
This is a small linolium-floored kitchen, with red counters, a double stainless steel sink filled with dishes and a noisy refridgerator. It is illuminated from above by recessed flourescent lights, although only one bulb seems to be working. |
||||
|
||||
The room contiues to the south into the living room, and to the west into the hallway. |
||||
. |
||||
s,south Living Room |
||||
w,west Hallway |
||||
. |
||||
Living Room |
||||
This small intimate livingroom with light wood parkade flooring is dominated by a large wampa rug on the floor and a TV opposite a three seat red couch. A window to the south has the shades draw for enhanced viewing. |
||||
|
||||
The room continues to the west past the couch into the play room, and to the north into the kitchen. |
||||
. |
||||
w,west Play Room |
||||
n,north Kitchen |
||||
. |
@ -0,0 +1,42 @@ |
||||
import pyoo |
||||
from pyoo import PlaceLoader, Thing, verb, Interpreter, Container, Place, Player, PyooVerbNotFound |
||||
|
||||
class DescriptivePlace(Place): |
||||
def handle_enter(self, player): |
||||
self.do_look() |
||||
|
||||
@verb('look,l', 'none','none','none') |
||||
def look(self, verbname, dobjstr, prepstr, iobjstr, dobj, iobj, argstr): |
||||
self.do_look() |
||||
|
||||
def do_look(self): |
||||
print self.name |
||||
if isinstance(self.description, basestring): |
||||
print self.description |
||||
else: |
||||
for line in self.description: |
||||
print line |
||||
|
||||
loader = PlaceLoader(file('roomtest.txt','r'), DescriptivePlace) |
||||
game = Interpreter([], Player(), loader.places.values(), None) |
||||
porch = game.get_placematches('Porch')[0][1] |
||||
run = True |
||||
|
||||
# REPL |
||||
if __name__ == '__main__': |
||||
game.handle_move(porch) |
||||
while (run): |
||||
cmd = '' |
||||
try: |
||||
cmd = raw_input('>') |
||||
except EOFError: |
||||
run = False |
||||
if cmd.startswith('quit'): |
||||
run = False |
||||
else: |
||||
try: |
||||
game.interpret(cmd) |
||||
except PyooVerbNotFound: |
||||
print "I don't understand that." |
||||
|
||||
print "Bye!" |
Loading…
Reference in new issue