[gnome-nibbles] Implement bonus type HALF



commit 1a17d6343c703683388147bbadafb530e1363956
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Sat Jul 11 13:39:39 2015 +0300

    Implement bonus type HALF

 src/boni.vala          |   12 ++++++------
 src/gnome-nibbles.vala |    1 +
 src/nibbles-game.vala  |   28 ++++++++++------------------
 src/nibbles-view.vala  |   41 +++++++++++++++++++++++++++++++++++++++++
 src/worm.vala          |   25 +++++++++++++++++++++++++
 5 files changed, 83 insertions(+), 24 deletions(-)
---
diff --git a/src/boni.vala b/src/boni.vala
index c231812..c387f4a 100644
--- a/src/boni.vala
+++ b/src/boni.vala
@@ -1,7 +1,6 @@
 /* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  * Gnome Nibbles: Gnome Worm Game
- * Copyright (C) 2015 Iulian-Gabriel Radu, Sean MacIsaac, Ian Peters,
- *                    Guillaume Béland
+ * Copyright (C) 2015 Iulian-Gabriel Radu <iulian radu67 gmail com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,11 +16,14 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+// This is a fairly literal translation of the LGPLv2+ original by
+// Sean MacIsaac, Ian Peters, Guillaume Béland.
+
 public class Boni : Object
 {
     public Gee.LinkedList<Bonus> bonuses;
     public int missed;
-    public int left;
+    public int numleft;
     public int numboni;
     public int numbonuses;
 
@@ -37,7 +39,7 @@ public class Boni : Object
         missed = 0;
         numboni = 8 + numworms;
         numbonuses = 0;
-        left = numboni;
+        numleft = numboni;
     }
 
     public void add_bonus (int[,] walls, int x, int y, BonusType type, bool fake, int countdown)
@@ -82,8 +84,6 @@ public class Boni : Object
             }
         }
 
-        // Should never be reached
-        stderr.printf("[Debug] Not found\n");
         return null;
     }
 }
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index ac9fe6e..ca7ec9b 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -97,6 +97,7 @@ public class Nibbles : Gtk.Application
         window.set_titlebar (headerbar);
 
         add_window (window);
+        start_game_cb ();
     }
 
     protected override void activate ()
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 59817af..d9aeeee 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -111,14 +111,12 @@ public class NibblesGame : Object
                 return;
         }
 
-        stderr.printf("[Debug] Adding bonus2, regular %d\n", (int) regular);
         do
         {
             good = true;
             x = Random.int_range (0, WIDTH - 1);
             y = Random.int_range (0, HEIGHT - 1);
 
-            stderr.printf("[Debug] %d %d\n", x, y);
             if (walls[x, y] != EMPTYCHAR)
                 good = false;
             if (walls[x + 1, y] != EMPTYCHAR)
@@ -129,7 +127,6 @@ public class NibblesGame : Object
                 good = false;
         } while (!good);
 
-        stderr.printf("[Debug] Adding bonus3\n");
         if (regular)
         {
             if ((Random.int_range (0, 7) == 0) && fakes)
@@ -151,13 +148,10 @@ public class NibblesGame : Object
                 if (walls[x + 1, y + 1] != EMPTYCHAR)
                     good = false;
             }
-            stderr.printf("[Debug] Called add_bonus\n");
             boni.add_bonus (walls, x, y, BonusType.REGULAR, false, 300);
-            stderr.printf("[Debug] Done add_bonus\n");
         }
         else if (boni.missed <= Boni.MAX_MISSED)
         {
-            stderr.printf("[Debug] Else if\n");
             if (Random.int_range (0, 7) != 0)
                 good = false;
             else
@@ -200,8 +194,6 @@ public class NibblesGame : Object
                     break;
             }
         }
-
-        stderr.printf("[Debug] Finished adding bonus\n");
     }
 
     public bool add_bonus_cb ()
@@ -291,20 +283,20 @@ public class NibblesGame : Object
         switch (walls[worm.head ().x, worm.head ().y] - 'A')
         {
             case BonusType.REGULAR:
-                boni.left--;
-                worm.change += (boni.numboni - boni.left) * Worm.GROW_FACTOR;
-                worm.score += (boni.numboni - boni.left) * current_level;
+                boni.numleft--;
+                worm.change += (boni.numboni - boni.numleft) * Worm.GROW_FACTOR;
+                worm.score += (boni.numboni - boni.numleft) * current_level;
                 break;
             case BonusType.DOUBLE:
-                worm.score += (worm.list.size + worm.change) * current_level;
-                worm.change += worm.list.size + worm.change;
+                worm.score += (worm.length + worm.change) * current_level;
+                worm.change += worm.length + worm.change;
                 break;
             case BonusType.HALF:
-                if (worm.list.size + worm.change > 2)
+                if (worm.length + worm.change > 2)
                 {
-                    worm.score += ((worm.list.size + worm.change / 2) * current_level);
-                    // worm.reduce_tail ((worm.list.size + worm.change) / 2);
-                    worm.change -= (worm.list.size + worm.change) /2;
+                    worm.score += ((worm.length + worm.change / 2) * current_level);
+                    worm.reduce_tail (walls, (worm.length + worm.change) / 2);
+                    worm.change -= (worm.length + worm.change) /2;
                 }
                 break;
             case BonusType.LIFE:
@@ -331,7 +323,7 @@ public class NibblesGame : Object
             boni.remove_bonus (walls, bonus);
             boni.bonuses.remove (bonus);
 
-            if (boni.left != 0)
+            if (boni.numleft != 0)
                 add_bonus (true);
         }
         else
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 6eb3afc..b9ef044 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -417,6 +417,7 @@ public class NibblesView : GtkClutter.Embed
         worm.moved.connect (worm_moved_cb);
         worm.rescaled.connect (worm_rescaled_cb);
         worm.died.connect (worm_died_cb);
+        worm.tail_reduced.connect (worm_tail_reduced_cb);
     }
 
     public void worm_added_cb (Worm worm)
@@ -513,6 +514,46 @@ public class NibblesView : GtkClutter.Embed
         group.restore_easing_state ();
     }
 
+    public void worm_tail_reduced_cb (Worm worm, int erase_size)
+    {
+        float x, y;
+        var group = new Clutter.Actor ();
+        var worm_actors = worm_actors.get (worm);
+        var color = game.worm_props.get (worm).color;
+        for (int i = 0; i < erase_size; i++)
+        {
+            var texture = new GtkClutter.Texture ();
+            try
+            {
+                texture.set_from_pixbuf (worm_pixmaps[color]);
+            }
+            catch (Clutter.TextureError e)
+            {
+                /* Fatal console error when a worm's texture could not be set. */
+                error (_("Nibbles failed to set texture: %s"), e.message);
+            }
+            catch (Error e)
+            {
+                /* Fatal console error when a worm's texture could not be set. */
+                error (_("Nibbles failed to set texture: %s"), e.message);
+            }
+
+            worm_actors.first_child.get_position (out x, out y);
+            worm_actors.remove_child (worm_actors.first_child);
+
+            texture.set_position (x, y);
+            texture.set_size (game.tile_size, game.tile_size);
+            group.add_child (texture);
+        }
+        stage.add_child (group);
+
+        group.save_easing_state ();
+        group.set_easing_mode (Clutter.AnimationMode.EASE_OUT_EXPO);
+        group.set_easing_duration (NibblesGame.GAMEDELAY * 25);
+        group.set_opacity (0);
+        group.restore_easing_state ();
+    }
+
     public void bonus_added_cb ()
     {
         stderr.printf("[Debug] Bonus ADDED\n");
diff --git a/src/worm.vala b/src/worm.vala
index c1aa6dd..d1c4c98 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -37,6 +37,12 @@ public class Worm : Object
     public int change;
     public int score;
 
+    public int length
+    {
+        get { return list.size; }
+        set {}
+    }
+
     private WormDirection _direction;
     public WormDirection direction
     {
@@ -64,6 +70,7 @@ public class Worm : Object
     public signal void moved ();
     public signal void rescaled (int tile_size);
     public signal void died ();
+    public signal void tail_reduced (int erase_size);
 
     public signal void bonus_found ();
 
@@ -151,6 +158,24 @@ public class Worm : Object
             dequeue_keypress ();
     }
 
+    public void reduce_tail (int[,] walls, int erase_size)
+    {
+        if (erase_size > 0)
+        {
+            if (length <= erase_size)
+            {
+                die (walls);
+            }
+
+            for (int i = 0; i < erase_size; i++)
+            {
+                walls[list.last ().x, list.last ().y] = NibblesGame.EMPTYCHAR;
+                list.poll_tail ();
+            }
+            tail_reduced (erase_size);
+        }
+    }
+
     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]