[gnome-robots] transform commands into directions and vise-versa



commit bf7be752354bb5b18b84a69dbc70be2d042b36f6
Author: Andrey Kutejko <andy128k gmail com>
Date:   Wed Sep 23 23:14:27 2020 +0200

    transform commands into directions and vise-versa

 src/game-area.vala |   5 +--
 src/game.vala      | 116 +++++++++++++++++++++++++++--------------------------
 src/robots.vala    |   8 ++--
 3 files changed, 65 insertions(+), 64 deletions(-)
---
diff --git a/src/game-area.vala b/src/game-area.vala
index 2448db8..aeaacc9 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -242,9 +242,8 @@ public class GameArea : DrawingArea {
         int dx, dy;
         get_dir (x, y, out dx, out dy);
 
-        // TODO: replace by player_command
-        if (game.player_move (dx, dy)) {
-            game.move_robots ();
+        var cmd = PlayerCommand.from_direction (dx, dy);
+        if (game.player_command (cmd)) {
             queue_draw ();
         }
     }
diff --git a/src/game.vala b/src/game.vala
index 066b294..eb42349 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -21,6 +21,61 @@ using Games;
 
 public Game game = null;
 
+public enum PlayerCommand {
+    NW = 0,
+    N,
+    NE,
+    W,
+    STAY,
+    E,
+    SW,
+    S,
+    SE,
+    SAFE_TELEPORT,
+    RANDOM_TELEPORT,
+    WAIT;
+
+    public static PlayerCommand from_direction (int dx, int dy) {
+        foreach (var direction in directions) {
+            if (direction.dx == dx && direction.dy == dy) {
+                return direction.cmd;
+            }
+        }
+        return PlayerCommand.STAY;
+    }
+
+    public bool to_direction (out int dx, out int dy) {
+        foreach (var direction in directions) {
+            if (direction.cmd == this) {
+                dx = direction.dx;
+                dy = direction.dy;
+                return true;
+            }
+        }
+        dx = 0;
+        dy = 0;
+        return false;
+    }
+}
+
+struct Direction {
+    PlayerCommand cmd;
+    int dx;
+    int dy;
+}
+
+const Direction[] directions = {
+    { PlayerCommand.NW,    -1, -1 },
+    { PlayerCommand.N,      0, -1 },
+    { PlayerCommand.NE,     1, -1 },
+    { PlayerCommand.W,     -1,  0 },
+    { PlayerCommand.STAY,   0,  0 },
+    { PlayerCommand.E,      1,  0 },
+    { PlayerCommand.SW,    -1,  1 },
+    { PlayerCommand.S,      0,  1 },
+    { PlayerCommand.SE,     1,  1 }
+};
+
 public class Game {
 
     /*
@@ -47,21 +102,6 @@ public class Game {
         WAITING_TYPE2,
     }
 
-    public enum PlayerCommand {
-        NW = 0,
-        N,
-        NE,
-        W,
-        STAY,
-        E,
-        SW,
-        S,
-        SE,
-        SAFE_TELEPORT,
-        RANDOM_TELEPORT,
-        WAIT,
-    }
-
     Rand rand;
     public State state { get; private set; }
     public Arena arena { get; private set; }
@@ -590,7 +630,7 @@ public class Game {
      * Returns:
      * TRUE if the player can move, FALSE otherwise
      **/
-    public bool player_move (int dx, int dy) {
+    private bool player_move (int dx, int dy) {
         var change = try_player_move (dx, dy);
 
         if (change == null) {
@@ -693,55 +733,17 @@ public class Game {
 
         switch (key) {
         case PlayerCommand.NW:
-            if (player_move (-1, -1)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.N:
-            if (player_move (0, -1)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.NE:
-            if (player_move (1, -1)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.W:
-            if (player_move (-1, 0)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.STAY:
-            if (player_move (0, 0)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.E:
-            if (player_move (1, 0)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.SW:
-            if (player_move (-1, 1)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.S:
-            if (player_move (0, 1)) {
-                move_robots ();
-                return true;
-            }
-            return false;
         case PlayerCommand.SE:
-            if (player_move (1, 1)) {
+            int dx, dy;
+            assert (key.to_direction (out dx, out dy));
+            if (player_move (dx, dy)) {
                 move_robots ();
                 return true;
             }
diff --git a/src/robots.vala b/src/robots.vala
index ed5bd57..850dcc0 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -151,19 +151,19 @@ public class RobotsWindow : ApplicationWindow {
     }
 
     private void random_teleport_cb () {
-        if (game.player_command (Game.PlayerCommand.RANDOM_TELEPORT)) {
+        if (game.player_command (PlayerCommand.RANDOM_TELEPORT)) {
             game_area.queue_draw ();
         }
     }
 
     private void safe_teleport_cb () {
-        if (game.player_command (Game.PlayerCommand.SAFE_TELEPORT)) {
+        if (game.player_command (PlayerCommand.SAFE_TELEPORT)) {
             game_area.queue_draw ();
         }
     }
 
     private void wait_cb () {
-        if (game.player_command (Game.PlayerCommand.WAIT)) {
+        if (game.player_command (PlayerCommand.WAIT)) {
             game_area.queue_draw ();
         }
     }
@@ -181,7 +181,7 @@ public class RobotsWindow : ApplicationWindow {
 
         for (var i = 0; i < control_keys.length; ++i) {
             if (pressed == ((char)control_keys[i]).toupper ()) {
-                if (game.player_command ((Game.PlayerCommand)i)) {
+                if (game.player_command ((PlayerCommand)i)) {
                     game_area.queue_draw ();
                 }
                 return true;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]