home · contact · privacy
Introduce map geometry.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 25 Oct 2020 00:48:08 +0000 (02:48 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 25 Oct 2020 00:48:08 +0000 (02:48 +0200)
new2/plomrogue/game.py
new2/plomrogue/mapping.py
new2/plomrogue/tasks.py

index c8c2f3cbf9fae868e15eaa0bfdba13a7233dbe8a..47e08fb93c6257b0148cbc4768b837c4d85a37cb 100755 (executable)
@@ -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):
index 1847f691a79f7cef829a3a6bc1b4323397e38488..aa5af2c6778c33444e51de1f74236e61a8442838 100644 (file)
@@ -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)
index 402dfcb47c191ea86d9cc133c55813683b3f117e..a75352625fba3ee7042091de2fa88874caf57296 100644 (file)
@@ -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()