[gnome-nibbles] Fix head to head collision



commit c946bbb17993a01879541b67dd7c69d5f34400ef
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Wed Jul 1 20:14:06 2015 +0300

    Fix head to head collision

 src/nibbles-game.vala |   17 ++++++++++-------
 src/worm.vala         |   13 +++++++------
 2 files changed, 17 insertions(+), 13 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index d37f371..7a61c7b 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -203,13 +203,16 @@ public class NibblesGame : Object
                 continue;
 
             foreach (var other_worm in worms)
-                if (worm != other_worm
-                    && worm.collides_with_head (other_worm.head ()))
-                {
-                    worm.die (walls);
-                    other_worm.die (walls);
-                    continue;
-                }
+            {
+                if (worm.will_collide_with_head (other_worm)
+                    && worm != other_worm)
+                    {
+                        stderr.printf("[Debug] Head collision\n");
+                        worm.die (walls);
+                        other_worm.die (walls);
+                        continue;
+                    }
+            }
 
             if (!worm.can_move_to (walls, numworms))
             {
diff --git a/src/worm.vala b/src/worm.vala
index 0363ffd..f88f786 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -140,7 +140,7 @@ public class Worm : Object
 
     public bool can_move_to (int[,] walls, int numworms)
     {
-        Position position = position_move ();
+        var position = position_move ();
 
         if (walls[position.x, position.y] > NibblesGame.EMPTYCHAR &&
             walls[position.x, position.y] < 'z' + numworms)
@@ -149,12 +149,13 @@ public class Worm : Object
         return true;
     }
 
-    public bool collides_with_head (Position other_head)
+    public bool will_collide_with_head (Worm other_worm)
     {
-        if (head ().x == other_head.x)
-            return head ().y - 1 == other_head.y || head ().y + 1 == other_head.y;
-        if (head ().y == other_head.y)
-            return head ().x - 1 == other_head.x || head ().x + 1 == other_head.x;
+        var worm_pos = position_move ();
+        var other_worm_pos = other_worm.position_move ();
+
+        if (worm_pos == other_worm_pos)
+            return true;
 
         return false;
     }


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