[gnome-nibbles/wip/vala: 37/64] Add new regular bonus when one is consumed



commit ababf4befeab26844f601304ce6e55c211139840
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Wed Jul 8 12:34:05 2015 +0300

    Add new regular bonus when one is consumed

 src/boni.vala         |   24 +++++++++++++++-
 src/nibbles-game.vala |   71 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/nibbles-view.vala |   14 +++++++++
 src/worm.vala         |   10 ++++++-
 4 files changed, 114 insertions(+), 5 deletions(-)
---
diff --git a/src/boni.vala b/src/boni.vala
index 56329a7..c231812 100644
--- a/src/boni.vala
+++ b/src/boni.vala
@@ -22,6 +22,7 @@ public class Boni : Object
     public Gee.LinkedList<Bonus> bonuses;
     public int missed;
     public int left;
+    public int numboni;
     public int numbonuses;
 
     public const int MAX_BONUSES = 100;
@@ -34,8 +35,9 @@ public class Boni : Object
     {
         bonuses = new Gee.LinkedList<Bonus> ();
         missed = 0;
-        numbonuses = 8 + numworms;
-        left = numbonuses;
+        numboni = 8 + numworms;
+        numbonuses = 0;
+        left = numboni;
     }
 
     public void add_bonus (int[,] walls, int x, int y, BonusType type, bool fake, int countdown)
@@ -66,6 +68,24 @@ public class Boni : Object
 
         bonus_removed (bonus);
     }
+
+    public Bonus? get_bonus (int[,] walls, int x, int y)
+    {
+        foreach (var bonus in bonuses)
+        {
+            if ((x == bonus.x && y == bonus.y)
+                || (x == bonus.x + 1 && y == bonus.y)
+                || (x == bonus.x && y == bonus.y + 1)
+                || (x == bonus.x + 1 && y == bonus.y + 1))
+            {
+                return bonus;
+            }
+        }
+
+        // Should never be reached
+        stderr.printf("[Debug] Not found\n");
+        return null;
+    }
 }
 
 public class Bonus : Object
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index f11ebe9..7ac0347 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -42,7 +42,7 @@ public class NibblesGame : Object
     public const int GAMEDELAY = 35;
     public const int BONUSDELAY = 100;
 
-    public const int NUMWORMS = 2;
+    public const int NUMWORMS = 1;
 
     public const int WIDTH = 92;
     public const int HEIGHT = 66;
@@ -63,6 +63,8 @@ public class NibblesGame : Object
 
     public signal void worm_moved (Worm worm);
 
+    public signal void bonus_applied (Bonus bonus);
+
     public Gee.HashMap<Worm, WormProperties?> worm_props;
 
     public NibblesGame (Settings settings)
@@ -92,7 +94,10 @@ public class NibblesGame : Object
     {
         stderr.printf("[Debug] Loading worms\n");
         foreach (var worm in worms)
+        {
             worm.spawn (walls);
+            worm.bonus_found.connect (bonus_found_cb);
+        }
     }
 
     public void add_bonus (bool regular)
@@ -217,7 +222,7 @@ public class NibblesGame : Object
             }
         }
 
-        // FIXME: Use an iterator instead of a second list and remove
+        // FIXME 1/3: Use an iterator instead of a second list and remove
         // from the boni.bonuses list inside boni.remove_bonus ()
         var found = new Gee.LinkedList<Bonus> ();
         foreach (var bonus in boni.bonuses)
@@ -269,6 +274,68 @@ public class NibblesGame : Object
         }
     }
 
+    public void apply_bonus (Bonus bonus, Worm worm)
+    {
+        if (bonus.fake)
+        {
+            // handle reverse
+            return;
+        }
+
+        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;
+                break;
+            case BonusType.DOUBLE:
+                worm.score += (worm.list.size + worm.change) * current_level;
+                worm.change += worm.list.size + worm.change;
+                break;
+            case BonusType.HALF:
+                if (worm.list.size + 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;
+                }
+                break;
+            case BonusType.LIFE:
+                worm.lives++;
+                break;
+            case BonusType.REVERSE:
+                // TODO
+                break;
+        }
+    }
+
+    public void bonus_found_cb (Worm worm)
+    {
+        var bonus = boni.get_bonus (walls, worm.head ().x, worm.head ().y);
+        if (bonus == null)
+            return;
+        apply_bonus (bonus, worm);
+        bonus_applied (bonus);
+
+        if (walls[worm.head ().x, worm.head ().y] == BonusType.REGULAR + 'A'
+            && !bonus.fake)
+        {
+            // FIXME: 2/3
+            boni.remove_bonus (walls, bonus);
+            boni.bonuses.remove (bonus);
+
+            if (boni.left != 0)
+                add_bonus (true);
+        }
+        else
+        {
+            // FIXME: 3/3
+            boni.remove_bonus (walls, bonus);
+            boni.bonuses.remove (bonus);
+        }
+    }
+
     public bool main_loop_cb ()
     {
         move_worms ();
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 51527d1..44a9e36 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -31,6 +31,8 @@ public class NibblesView : GtkClutter.Embed
             _game = value;
             _game.boni.bonus_added.connect (bonus_added_cb);
             _game.boni.bonus_removed.connect (bonus_removed_cb);
+
+            _game.bonus_applied.connect (bonus_applied_cb);
         }
     }
 
@@ -550,6 +552,18 @@ public class NibblesView : GtkClutter.Embed
         stage.remove_child (bonus_actor);
     }
 
+    public void bonus_applied_cb (Bonus bonus)
+    {
+        var bonus_actor = bonus_actors.get (bonus);
+
+        bonus_actor.save_easing_state ();
+        bonus_actor.set_easing_mode (Clutter.AnimationMode.EASE_OUT_QUINT);
+        bonus_actor.set_easing_duration (NibblesGame.GAMEDELAY * 15);
+        bonus_actor.set_scale (1.45f, 1.45f);
+        bonus_actor.set_pivot_point (0.5f, 0.5f);
+        bonus_actor.restore_easing_state ();
+    }
+
     public void boni_rescale (int tile_size)
     {
         float x_pos, y_pos;
diff --git a/src/worm.vala b/src/worm.vala
index 8754c60..be4dd0c 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -23,6 +23,7 @@ public class Worm : Object
 {
     public const int STARTING_LENGTH = 5;
     private const int STARTING_LIVES = 6;
+    public const int GROW_FACTOR = 4;
 
     public Position starting_position { get; private set; }
 
@@ -32,7 +33,8 @@ public class Worm : Object
     public bool keypress = false;
     public bool is_stopped = false;
 
-    public int lives { get; private set; }
+    public int lives;
+    public int change;
     public int score;
 
     private WormDirection _direction;
@@ -63,6 +65,8 @@ public class Worm : Object
     public signal void rescaled (int tile_size);
     public signal void died ();
 
+    public signal void bonus_found ();
+
     public Worm (int id, WormDirection direction)
     {
         this.id = id;
@@ -135,6 +139,10 @@ public class Worm : Object
         else
             added ();
 
+        /* Check for bonus before changing tile */
+        if (walls[head ().x, head ().y] != NibblesGame.EMPTYCHAR)
+            bonus_found ();
+
         /* Mark the tile as occupied by the worm's body */
         walls[head ().x, head ().y] = NibblesGame.WORMCHAR + id;
 


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