Added ability to make batch functions in the shell, and did some reworking of the event system in gamebase to allow it

This commit is contained in:
Patrick Marsee 2019-02-06 18:29:43 -05:00
parent 9055d8b257
commit bc70bad1c7
3 changed files with 130 additions and 54 deletions

View file

@ -25,6 +25,7 @@ class GameBase(object):
self.ps2 = '? '
self.eventQueue = []
self.gameTime = 0.0
self.skipLoop = True
# player info
self.playerx = -1
@ -187,18 +188,18 @@ The letter is not case-sensitive."""
# Now we have a heading! Let's see if we can get there...
if (x, y) == (self.playerx, self.playery):
_hq.heappush(self.eventQueue, (self.gameTime, _ge.ArriveEvent(self.playerName, x, y, 0.0)))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.ArriveEvent(self.playerName, x, y, 0.0)))
return
dist, path = self.level.path(x, y, self.playerx, self.playery)
if dist == -1:
print('{0} cannot reach {1}{2}.'.format(self.playerName, self.numberToLetter(x), y), file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
else:
pos = self.level.coordsToInt(self.playerx, self.playery)
space = path[pos]
if space == -1:
_hq.heappush(self.eventQueue, (self.gameTime, _ge.ArriveEvent(self.playerName, self.playerx, self.playery, 0.0)))
self.setEvent(0.0, _ge.ArriveEvent(self.playerName, self.playerx, self.playery, 0.0))
return
#target = self.level.coordsToInt(x, y)
t = 1
@ -207,12 +208,11 @@ The letter is not case-sensitive."""
newx, newy = self.level.intToCoords(space)
space = path[space]
if space != -1:
_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.GoEvent(self.playerName, newx, newy)))
self.setEvent(t * speed, _ge.GoEvent(self.playerName, newx, newy))
else:
_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.ArriveEvent(self.playerName, newx, newy, t * speed)))
self.setEvent(t * speed, _ge.ArriveEvent(self.playerName, newx, newy, t * speed))
break
t += 1
#newx, newy = self.level.intToCoords(space)
#_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.ArriveEvent(self.playerName, newx, newy, t * speed)))
return
@ -236,7 +236,7 @@ Object can be the name of the object, or its coordinates."""
print(self.justifyText(str(thing)), file = self.outstream)
else:
print("There is nothing to see here.\n", file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
def use(self, args):
"""use [-r] [the] object [on [the] object2]
@ -261,21 +261,21 @@ the name of an item in the player's inventory."""
thing, x, y = self.parseCoords(args)
if thing == None:
print("There is nothing to use.", file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
if thing.thingType != 'u' and thing.name not in self.playerInv:
print("The {0} cannot be used.".format(thing.name), file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
# Similar to go, but not quite the same.
if (x, y) == (self.playerx, self.playery) or thing.name in self.playerInv:
_hq.heappush(self.eventQueue, (self.gameTime + 0.125, _ge.UseEvent(thing)))
self.setEvent(0.125, _ge.UseEvent(thing))
return
dist, path = self.level.path(x, y, self.playerx, self.playery)
if dist == -1:
print('{0} cannot reach the {1}.'.format(self.playerName, thing.name), file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
else:
pos = self.level.coordsToInt(self.playerx, self.playery)
@ -286,11 +286,11 @@ the name of an item in the player's inventory."""
while space != -1:
newx, newy = self.level.intToCoords(space)
space = path[space]
_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.GoEvent(self.playerName, newx, newy)))
self.setEvent(t * speed, _ge.GoEvent(self.playerName, newx, newy))
t += 1
#newx, newy = self.level.intToCoords(space)
#_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.GoEvent(self.playerName, newx, newy)))
_hq.heappush(self.eventQueue, (self.gameTime + t * speed + 0.125, _ge.UseEvent(thing)))
self.setEvent(t * speed + 0.125, _ge.UseEvent(thing))
return
def useOn(self, args, speed):
@ -302,16 +302,16 @@ the name of an item in the player's inventory."""
thing, x, y = self.parseCoords(args[onIndex+1:])
if item == None or item.name not in self.playerInv:
print("There is no {0} in the inventory.".format(item.name), file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
if thing == None and x < 0 and y < 0:
print("Argument contains 'to' but with no real predicate.", file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
# Similar to go, but not quite the same.
if (x, y) == (self.playerx, self.playery):
_hq.heappush(self.eventQueue, (self.gameTime + 0.125, _ge.UseOnEvent(item, thing)))
self.setEvent(0.125, _ge.UseOnEvent(item, thing))
return
dist, path = self.level.path(x, y, self.playerx, self.playery)
if dist == -1:
@ -319,7 +319,7 @@ the name of an item in the player's inventory."""
print('{0} cannot reach the {1}.'.format(self.playerName, thing.name), file = self.outstream)
else:
print('{0} cannot reach {1}{2}.'.format(self.playerName, self.numberToLetter(x), y), file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
else:
pos = self.level.coordsToInt(self.playerx, self.playery)
@ -330,11 +330,11 @@ the name of an item in the player's inventory."""
while space != -1:
newx, newy = self.level.intToCoords(space)
space = path[space]
_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.GoEvent(self.playerName, newx, newy)))
self.setEvent(t * speed, _ge.GoEvent(self.playerName, newx, newy))
t += 1
#newx, newy = self.level.intToCoords(space)
#_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.GoEvent(self.playerName, newx, newy)))
_hq.heappush(self.eventQueue, (self.gameTime + t * speed + 0.125, _ge.UseOnEvent(item, thing)))
self.setEvent(t * speed + 0.125, _ge.UseOnEvent(item, thing))
return
def take(self, args):
@ -357,17 +357,17 @@ Object can be the name of the object, or its coordinates."""
return
if thing.thingType != 'i':
print("The {0} cannot be taken.".format(thing.name), file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
# Similar to go, but not quite the same.
if (x, y) == (self.playerx, self.playery):
_hq.heappush(self.eventQueue, (self.gameTime + 0.125, _ge.TakeEvent(thing)))
self.setEvent(0.125, _ge.TakeEvent(thing))
return
dist, path = self.level.path(x, y, self.playerx, self.playery)
if dist == -1:
print('{0} cannot reach the {1}.'.format(self.playerName, thing.name), file = self.outstream)
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
else:
pos = self.level.coordsToInt(self.playerx, self.playery)
@ -378,11 +378,11 @@ Object can be the name of the object, or its coordinates."""
while space != -1:
newx, newy = self.level.intToCoords(space)
space = path[space]
_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.GoEvent(self.playerName, newx, newy)))
self.setEvent(t * speed, _ge.GoEvent(self.playerName, newx, newy))
t += 1
#newx, newy = self.level.intToCoords(space)
#_hq.heappush(self.eventQueue, (self.gameTime + t * speed, _ge.GoEvent(self.playerName, newx, newy)))
_hq.heappush(self.eventQueue, (self.gameTime + t * speed + 0.125, _ge.TakeEvent(thing)))
self.setEvent(t * speed + 0.125, _ge.TakeEvent(thing))
return
def drop(self, args):
@ -390,7 +390,7 @@ Object can be the name of the object, or its coordinates."""
if args[0] == 'the':
args.pop(0)
if args[0] in self.playerInv:
_hq.heappush(self.eventQueue, (self.gameTime, _ge.DropEvent(self.playerInv[args[0]])))
self.setEvent(0.0, _ge.DropEvent(self.playerInv[args[0]]))
else:
print('{0} do not have a {1}.'.format(self.playerName, args[0]), file = self.outstream)
@ -464,7 +464,7 @@ Object can be the name of the object, or its coordinates."""
del self.persist[self.level.name][i]
# push a no-op event so that saving doesn't cost player characters time
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
def loadGame(self, args):
@ -483,19 +483,29 @@ Object can be the name of the object, or its coordinates."""
self.playerName, x, y, self.playerInv, levelname, self.persist, self.eventQueue, self.gameTime = _pi.load(f)
#print(levelname, x, y, file = self.outstream)
self.loadMap((levelname, x, y))
_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
#_hq.heappush(self.eventQueue, (self.gameTime, _ge.NoOpEvent()))
return
def gameEventLoop(self):
#print(self.skipLoop)
if self.skipLoop:
return
#print(self.skipLoop)
while len(self.eventQueue) > 0:
ev = _hq.heappop(self.eventQueue)
self.gameTime = ev[0]
e = ev[1]
if self.__gameEvents[e.eventType](e):
#print('break loop')
break
if len(self.eventQueue) == 0:
self.gameTime = 0.0
_ge.resetEventNum()
self.skipLoop = True
def setEvent(self, t, e, skip = False):
_hq.heappush(self.eventQueue, (self.gameTime + t, e))
self.skipLoop = skip
# default event handlers
@ -540,14 +550,14 @@ Object can be the name of the object, or its coordinates."""
if e.thing.useFunc == '':
print('The {0} cannot be used by itself.'.format(e.thing.name), file = self.outstream)
return True
_hq.heappush(self.eventQueue, (self.gameTime + self.__useFuncs[e.thing.useFunc](e.thing), _ge.NoOpEvent()))
self.setEvent(self.__useFuncs[e.thing.useFunc](e.thing), _ge.NoOpEvent())
return False
def handleUseOn(self, e):
if e.item.useOnFunc == '':
print('The {0} cannot be used on other objects.'.format(e.item.name), file = self.outstream)
return True
_hq.heappush(self.eventQueue, (self.gameTime + self.__useFuncs[e.item.useOnFunc](e.item, e.thing), _ge.NoOpEvent()))
self.setEvent(self.__useFuncs[e.item.useOnFunc](e.item, e.thing), _ge.NoOpEvent())
return False
def handleTake(self, e):