[gnome-nibbles/arnaudb/rework-warps: 17/18] Manage warps in Game, not in Worm.



commit 3ddb11917674a0e143deeee4e90a59a07d971221
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Jun 29 17:48:28 2020 +0200

    Manage warps in Game, not in Worm.

 src/nibbles-game.vala | 28 ++++++++++++++--------------
 src/worm.vala         | 20 ++++++++++----------
 2 files changed, 24 insertions(+), 24 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index f5b25a3..84a1793 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -362,7 +362,6 @@ private class NibblesGame : Object
         {
             var worm = new Worm (i, width, height);
             worm.bonus_found.connect (bonus_found_cb);
-            worm.warp_found.connect (warp_found_cb);
             worm.is_human = (i < numhumans);
             worms.add (worm);
         }
@@ -428,7 +427,20 @@ private class NibblesGame : Object
              || worm.list.is_empty)
                 continue;
 
-            worm.move (board);
+            worm.move_part_1 ();
+            if (board[worm.head.x, worm.head.y] == NibblesGame.WARPCHAR)
+            {
+                int target_x;
+                int target_y;
+                if (!warp_manager.get_warp_target (worm.head.x, worm.head.y,
+                                  /* horizontal */ worm.direction == WormDirection.LEFT || worm.direction == 
WormDirection.RIGHT,
+                                                   out target_x, out target_y))
+                    assert_not_reached ();
+
+                worm.move_part_2 (board, Position () { x = target_x, y = target_y });
+            }
+            else
+                worm.move_part_2 (board, null);
 
             /* kill worms on heads collision */
             foreach (var other_worm in worms)
@@ -625,18 +637,6 @@ private class NibblesGame : Object
             add_bonus (true);
     }
 
-    private void warp_found_cb (Worm worm)
-    {
-        int target_x;
-        int target_y;
-        if (!warp_manager.get_warp_target (worm.head.x, worm.head.y,
-                          /* horizontal */ worm.direction == WormDirection.LEFT || worm.direction == 
WormDirection.RIGHT,
-                                           out target_x, out target_y))
-            return;
-
-        worm.warp (target_x, target_y);
-    }
-
     internal GameStatus? get_game_status ()
     {
         var worms_left = 0;
diff --git a/src/worm.vala b/src/worm.vala
index d453373..a86519d 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -136,7 +136,6 @@ private class Worm : Object
     internal signal void reversed ();
 
     internal signal void bonus_found ();
-    internal signal void warp_found ();
 
     public int width    { private get; protected construct; }
     public int height   { private get; protected construct; }
@@ -170,7 +169,7 @@ private class Worm : Object
         key_queue.clear ();
     }
 
-    internal void move (int[,] board)
+    internal void move_part_1 ()
     {
         if (is_human)
             keypress = false;
@@ -208,9 +207,12 @@ private class Worm : Object
 
         /* Add a new body piece */
         list.offer_head (position);
+    }
 
-        if (board[head.x, head.y] == NibblesGame.WARPCHAR)
-            warp_found ();
+    internal void move_part_2 (int[,] board, Position? head_position)
+    {
+        if (head_position != null)
+            head = Position () { x = head_position.x, y = head_position.y };
 
         if (change > 0)
         {
@@ -270,11 +272,6 @@ private class Worm : Object
             direction = (list[0].y > list[1].y) ? WormDirection.DOWN : WormDirection.UP;
     }
 
-    internal inline void warp (int x, int y)
-    {
-        head = Position () { x = x, y = y };
-    }
-
     internal bool can_move_to (int[,] board, int numworms)
     {
         var position = position_move ();
@@ -295,7 +292,10 @@ private class Worm : Object
     {
         change = STARTING_LENGTH - 1;
         for (int i = 0; i < STARTING_LENGTH; i++)
-            move (board);
+        {
+            move_part_1 ();
+            move_part_2 (board, /* no warp */ null);
+        }
     }
 
     private void materialize (int [,] board)


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