home · contact · privacy
Add map writing.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 25 Oct 2020 01:48:53 +0000 (02:48 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 25 Oct 2020 01:48:53 +0000 (02:48 +0100)
new2/plomrogue/game.py
new2/plomrogue/mapping.py
new2/plomrogue/tasks.py
new2/rogue_chat.html

index e8c85da35adef872a0667bf97a75c09189049995..81bd530d8b3cde8c12db964042dabc505468256e 100755 (executable)
@@ -1,4 +1,4 @@
-from plomrogue.tasks import Task_WAIT, Task_MOVE
+from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_WRITE
 from plomrogue.errors import GameError
 from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY
 from plomrogue.io import GameIO
@@ -36,7 +36,8 @@ class Game(GameBase):
         self.changed = True
         self.io = GameIO(self)
         self.tasks = {'WAIT': Task_WAIT,
-                      'MOVE': Task_MOVE}
+                      'MOVE': Task_MOVE,
+                      'WRITE': Task_WRITE}
         self.map_geometry = MapGeometrySquare(YX(24, 40))
         self.commands = {'QUERY': cmd_QUERY,
                          'ALL': cmd_ALL,
@@ -47,8 +48,12 @@ class Game(GameBase):
         self.map = Map(self.map_geometry.size)
 
     def get_string_options(self, string_option_type):
+        import string
         if string_option_type == 'direction':
             return self.map_geometry.get_directions()
+        if string_option_type == 'char':
+            return [c for c in
+                    string.digits + string.ascii_letters + string.punctuation]
         return None
 
     def send_gamestate(self, connection_id=None):
index 0a96058c29c2f9b7b990dc98083a84b47037f504..cd0c23def676edd8eb146fca15da7c781201fb28 100644 (file)
@@ -63,6 +63,19 @@ class Map():
         self.size = map_size
         self.terrain = '.' * self.size_i
 
+    def __getitem__(self, yx):
+        return self.terrain[self.get_position_index(yx)]
+
+    def __setitem__(self, yx, c):
+        pos_i = self.get_position_index(yx)
+        if type(c) == str:
+            self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
+        else:
+            self.terrain[pos_i] = c
+
     @property
     def size_i(self):
         return self.size.y * self.size.x
+
+    def get_position_index(self, yx):
+        return yx.y * self.size.x + yx.x
index 01084166635e6fba149f2763ed3b7a1ac182f6a3..fffb36a117421bdf04a9082d5e4d3d3649995ef4 100644 (file)
@@ -25,6 +25,7 @@ class Task_WAIT(Task):
 
 
 class Task_MOVE(Task):
+    todo = 1
     argtypes = 'string:direction'
 
     def get_move_target(self):
@@ -38,3 +39,15 @@ class Task_MOVE(Task):
 
     def do(self):
         self.thing.position = self.get_move_target()
+
+
+
+class Task_WRITE(Task):
+    todo = 1
+    argtypes = 'string:char'
+
+    def check(self):
+        pass
+
+    def do(self):
+        self.thing.game.map[self.thing.position] = self.args[0]
index f1146cb7c9cd70646656c85c80ef67a4882a4973..5af97daa722531a568665a6fe1cf7600926e67d3 100644 (file)
@@ -143,7 +143,9 @@ let game = {
 
 let chat = {
   input_line:"",
-  history: ["contain whitespace, escape them with \\.",
+  history: ["visible ASCII char in the input prompt.",
+           "To write on the map, enter on a single",
+           "contain whitespace, escape them with \\.",
            "Use double quotes for strings that",
            "Use arrow keys to move your avatar.",
            "  QUERY USER TEXT - send TEXT to USER",
@@ -169,8 +171,12 @@ document.addEventListener('keydown', (event) => {
     chat.input_line = chat.input_line.slice(0, -1);
     tui.draw_input_line();
   } else if (event.key === 'Enter') {
-    websocket.send(chat.input_line);
-    chat.input_line = ''
+    if (chat.input_line.length === 1) {
+      websocket.send("TASK:WRITE " + chat.input_line);
+    } else {
+      websocket.send(chat.input_line);
+    }
+    chat.input_line = '';
     tui.draw_input_line();
   } else if (event.key === 'ArrowLeft') {
     websocket.send('TASK:MOVE LEFT');
@@ -181,7 +187,6 @@ document.addEventListener('keydown', (event) => {
   } else if (event.key === 'ArrowDown') {
     websocket.send('TASK:MOVE DOWN');
   };
-  console.log(event.key);
 }, false);
 
 let websocket = new WebSocket(websocket_location);