[gnome-nibbles/wip/vala] game: implement "reverse worms" bonus



commit 7fd26c9a7b828b6a87179d5e79bf91c4bbc24c00
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Sun Aug 16 23:10:17 2015 +0300

    game: implement "reverse worms" bonus

 src/nibbles-game.vala |   10 ++++++++--
 src/nibbles-view.vala |   14 ++++++++++++++
 src/worm.vala         |   17 +++++++++++++++++
 3 files changed, 39 insertions(+), 2 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 00886d4..02a0884 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -295,6 +295,12 @@ public class NibblesGame : Object
         }
     }
 
+    private void reverse_worms ()
+    {
+        foreach (var worm in worms)
+            worm.reverse (walls);
+    }
+
     /*\
     * * Handling bonuses
     \*/
@@ -399,7 +405,7 @@ public class NibblesGame : Object
     {
         if (bonus.fake)
         {
-            // handle reverse
+            reverse_worms ();
             return;
         }
 
@@ -426,7 +432,7 @@ public class NibblesGame : Object
                 worm.lives++;
                 break;
             case BonusType.REVERSE:
-                // TODO
+                reverse_worms ();
                 break;
         }
     }
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index f4e17f8..172cd12 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -403,6 +403,7 @@ public class NibblesView : GtkClutter.Embed
             worm.rescaled.connect (worm_rescaled_cb);
             worm.died.connect (worm_died_cb);
             worm.tail_reduced.connect (worm_tail_reduced_cb);
+            worm.reversed.connect (worm_reversed_cb);
         }
     }
 
@@ -615,6 +616,19 @@ public class NibblesView : GtkClutter.Embed
         group.restore_easing_state ();
     }
 
+    public void worm_reversed_cb (Worm worm)
+    {
+        var actors = worm_actors.get (worm);
+
+        var count = 0;
+        foreach (var actor in actors.get_children ())
+        {
+            actor.set_position (worm.list[count].x * game.tile_size, worm.list[count].y * game.tile_size);
+            count++;
+        }
+
+    }
+
     public void bonus_added_cb ()
     {
         stderr.printf("[Debug] Bonus ADDED\n");
diff --git a/src/worm.vala b/src/worm.vala
index 5f9b375..285ad3b 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -71,6 +71,7 @@ public class Worm : Object
     public signal void rescaled (int tile_size);
     public signal void died ();
     public signal void tail_reduced (int erase_size);
+    public signal void reversed ();
 
     public signal void bonus_found ();
 
@@ -183,6 +184,22 @@ public class Worm : Object
         }
     }
 
+    public void reverse (int[,] walls)
+    {
+        var reversed_list = new Gee.LinkedList<Position?> ();
+        foreach (var pos in list)
+            reversed_list.offer_head (pos);
+
+        reversed ();
+        list = reversed_list;
+
+        /* Set new direction as the opposite direction of the last two tail pieces */
+        if (list[0].y == list[1].y)
+            direction = (list[0].x > list[1].x) ? WormDirection.RIGHT : WormDirection.LEFT;
+        else
+            direction = (list[0].y > list[1].y) ? WormDirection.DOWN : WormDirection.UP;
+    }
+
     public bool can_move_to (int[,] walls, int numworms)
     {
         var position = position_move ();


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