From: Christian Heller Date: Sun, 25 Oct 2020 00:48:08 +0000 (+0200) Subject: Introduce map geometry. X-Git-Url: https://plomlompom.com/repos/balance?a=commitdiff_plain;h=e1d03eb7d5f847813071e1711accbb4d9153fdcf;p=plomrogue2-experiments Introduce map geometry. --- diff --git a/new2/plomrogue/game.py b/new2/plomrogue/game.py index c8c2f3c..47e08fb 100755 --- a/new2/plomrogue/game.py +++ b/new2/plomrogue/game.py @@ -4,6 +4,7 @@ from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY from plomrogue.io import GameIO from plomrogue.misc import quote from plomrogue.things import Thing, ThingPlayer +from plomrogue.mapping import MapGeometrySquare @@ -36,14 +37,17 @@ class Game(GameBase): self.io = GameIO(self) self.tasks = {'WAIT': Task_WAIT, 'MOVE': Task_MOVE} - self.commands = {'QUERY': cmd_QUERY, 'ALL': cmd_ALL, 'LOGIN': cmd_LOGIN} + self.map_geometry = MapGeometrySquare() + self.commands = {'QUERY': cmd_QUERY, + 'ALL': cmd_ALL, + 'LOGIN': cmd_LOGIN} self.thing_type = Thing self.thing_types = {'player': ThingPlayer} self.sessions = {} def get_string_options(self, string_option_type): if string_option_type == 'direction': - return ['UP', 'DOWN', 'LEFT', 'RIGHT'] + return self.map_geometry.get_directions() return None def send_gamestate(self, connection_id=None): diff --git a/new2/plomrogue/mapping.py b/new2/plomrogue/mapping.py index 1847f69..aa5af2c 100644 --- a/new2/plomrogue/mapping.py +++ b/new2/plomrogue/mapping.py @@ -12,3 +12,38 @@ class YX(collections.namedtuple('YX', ('y', 'x'))): def __str__(self): return 'Y:%s,X:%s' % (self.y, self.x) + + + +class MapGeometry(): + + def get_directions(self): + directions = [] + for name in dir(self): + if name[:5] == 'move_': + directions += [name[5:]] + return directions + + def move(self, start_pos, direction): + mover = getattr(self, 'move_' + direction) + return mover(start_pos) + + + +class MapGeometryWithLeftRightMoves(MapGeometry): + + def move_LEFT(self, start_pos): + return YX(start_pos.y, start_pos.x - 1) + + def move_RIGHT(self, start_pos): + return YX(start_pos.y, start_pos.x + 1) + + + +class MapGeometrySquare(MapGeometryWithLeftRightMoves): + + def move_UP(self, start_pos): + return YX(start_pos.y - 1, start_pos.x) + + def move_DOWN(self, start_pos): + return YX(start_pos.y + 1, start_pos.x) diff --git a/new2/plomrogue/tasks.py b/new2/plomrogue/tasks.py index 402dfcb..a753526 100644 --- a/new2/plomrogue/tasks.py +++ b/new2/plomrogue/tasks.py @@ -1,5 +1,4 @@ from plomrogue.errors import GameError -#from plomrogue.misc import quote from plomrogue.mapping import YX @@ -29,13 +28,8 @@ class Task_MOVE(Task): argtypes = 'string:direction' def get_move_target(self): - moves = { - 'UP': YX(-1, 0), - 'DOWN': YX(1, 0), - 'LEFT': YX(0, -1), - 'RIGHT': YX(0, 1), - } - return self.thing.position + moves[self.args[0]] + return self.thing.game.map_geometry.move(self.thing.position, + self.args[0]) def check(self): test_pos = self.get_move_target()