[gnome-nibbles] Add Boni and Bonus classes. Implement methods for bonus adding/drawing



commit 841b59195245ab0dc714a07e9aec710d889f46cb
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Wed Jul 1 16:27:44 2015 +0300

    Add Boni and Bonus classes. Implement methods for bonus adding/drawing

 src/Makefile.am       |    1 +
 src/boni.vala         |   88 ++++++++++++++++++++++++++++++++++
 src/nibbles-game.vala |  126 ++++++++++++++++++++++++++++++++++++++++++++++++-
 src/nibbles-view.vala |   59 +++++++++++++++++++++--
 4 files changed, 269 insertions(+), 5 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 6d3a192..aad7043 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -8,6 +8,7 @@ gnome_nibbles_SOURCES = \
        nibbles-view.vala \
        nibbles-game.vala \
        worm.vala \
+       boni.vala \
        $(BUILT_SOURCES)
 
 gnome_nibbles_CFLAGS = -w
diff --git a/src/boni.vala b/src/boni.vala
new file mode 100644
index 0000000..2f5462f
--- /dev/null
+++ b/src/boni.vala
@@ -0,0 +1,88 @@
+/* -*- 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
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+public class Boni : Object
+{
+    public Gee.ArrayList<Bonus> bonuses;
+    public int missed;
+    public int left;
+    public int numbonuses;
+
+    public const int MAX_BONUSES = 100;
+    public const int MAX_MISSED = 2;
+
+    public signal void bonus_added ();
+
+    public Boni (int numworms)
+    {
+        bonuses = new Gee.ArrayList<Bonus> ();
+        missed = 0;
+        numbonuses = 8 + numworms;
+        left = numbonuses;
+    }
+
+    public void add_bonus (int[,] walls, int x, int y, BonusType type, bool fake, int countdown)
+    {
+        if (numbonuses == MAX_BONUSES)
+            return;
+
+        var bonus = new Bonus (x, y, type, fake, countdown);
+        bonuses.add (bonus);
+        walls[x, y] = type + 'A';
+        walls[x + 1, y] = type + 'A';
+        walls[x, y + 1] = type + 'A';
+        walls[x + 1, y + 1] = type + 'A';
+        bonus_added ();
+        numbonuses++;
+
+        //TODO
+        // if (type != BonusType.REGULAR)
+        //     play_sound ("appear");
+    }
+}
+
+public class Bonus : Object
+{
+    public int x;
+    public int y;
+    public BonusType type;
+    public bool fake;
+    public int countdown;
+
+    public Bonus (int x, int y, BonusType type, bool fake, int countdown)
+    {
+        this.x = x;
+        this.y = y;
+        this.type = type;
+        this.fake = fake;
+        this.countdown = countdown;
+    }
+}
+
+public enum BonusType
+{
+    REGULAR,
+    HALF,
+    DOUBLE,
+    LIFE,
+    REVERSE,
+    CUT,
+    SWITCH,
+    WARP
+}
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 1232dca..d37f371 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -17,10 +17,23 @@
  */
 
 // This is a fairly literal translation of the LGPLv2+ original by
-// Callum McKenzie, itself based on GtkFrame and GtkAspectFrame.
+// Sean MacIsaac, Ian Peters, Guillaume Béland.
 
 public class NibblesGame : Object
 {
+    private Boni _boni;
+    public Boni boni
+    {
+        get { return _boni; }
+        set
+        {
+            if (_boni != null)
+                SignalHandler.disconnect_matched (_boni, SignalMatchType.DATA, 0, 0, null, null, this);
+
+            _boni = value;
+        }
+    }
+
     public int tile_size;
     public int start_level;
 
@@ -45,21 +58,27 @@ public class NibblesGame : Object
 
     public int game_speed = 4;
 
+    public bool fakes = false;
+
     public signal void worm_moved (Worm worm);
 
     public Gee.HashMap<Worm, WormProperties?> worm_props;
 
     public NibblesGame (Settings settings)
     {
+        boni = new Boni (numworms);
         walls = new int[WIDTH, HEIGHT];
         worms = new Gee.LinkedList<Worm> ();
         worm_props = new Gee.HashMap<Worm, WormProperties?> ();
+
+        Random.set_seed ((uint32) time_t ());
         load_properties (settings);
     }
 
     public void start ()
     {
         add_worms ();
+        add_bonus (true);
         var id = Timeout.add (game_speed * GAMEDELAY, main_loop_cb);
         Source.set_name_by_id (id, "[Nibbles] main_loop_cb");
     }
@@ -71,6 +90,111 @@ public class NibblesGame : Object
             worm.spawn (walls);
     }
 
+    public void add_bonus (bool regular)
+    {
+        bool good = false;
+        int x = 0, y = 0;
+
+        stderr.printf("[Debug] Adding bonus\n");
+        if (!regular)
+        {
+            if (Random.int_range (0, 50) != 0)
+                return;
+        }
+
+        stderr.printf("[Debug] Adding bonus2\n");
+        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)
+                good = false;
+            if (walls[x, y + 1] != EMPTYCHAR)
+                good = false;
+            if (walls[x + 1, y + 1] != EMPTYCHAR)
+                good = false;
+        } while (!good);
+
+        stderr.printf("[Debug] Adding bonus3\n");
+        if (regular)
+        {
+            if ((Random.int_range (0, 7) == 0) && fakes)
+                boni.add_bonus (walls, x, y, BonusType.REGULAR, true, 300);
+
+            good = false;
+            while (!good)
+            {
+                good = true;
+
+                x = Random.int_range (0, WIDTH - 1);
+                y = Random.int_range (0, HEIGHT - 1);
+                if (walls[x, y] != EMPTYCHAR)
+                    good = false;
+                if (walls[x + 1, y] != EMPTYCHAR)
+                    good = false;
+                if (walls[x, y + 1] != EMPTYCHAR)
+                    good = false;
+                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
+                good = true;
+
+            if (good && !fakes)
+                return;
+
+            switch (Random.int_range (0, 21))
+            {
+                case 0:
+                case 1:
+                case 2:
+                case 3:
+                case 4:
+                case 5:
+                case 6:
+                case 7:
+                case 8:
+                case 9:
+                    boni.add_bonus (walls, x, y, BonusType.HALF, good, 200);
+                    break;
+                case 10:
+                case 11:
+                case 12:
+                case 13:
+                case 14:
+                    boni.add_bonus (walls, x, y, BonusType.DOUBLE, good, 150);
+                    break;
+                case 15:
+                    boni.add_bonus (walls, x, y, BonusType.LIFE, good, 100);
+                    break;
+                case 16:
+                case 17:
+                case 18:
+                case 19:
+                case 20:
+                    if (numworms > 1)
+                        boni.add_bonus (walls, x, y, BonusType.REVERSE, good, 150);
+                    break;
+            }
+        }
+
+        stderr.printf("[Debug] Finished adding bonus\n");
+    }
+
     public void move_worms ()
     {
         foreach (var worm in worms)
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 685680b..bc06eea 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -29,6 +29,7 @@ public class NibblesView : GtkClutter.Embed
                 SignalHandler.disconnect_matched (_game, SignalMatchType.DATA, 0, 0, null, null, this);
 
             _game = value;
+            _game.boni.bonus_added.connect (bonus_added_cb);
         }
     }
 
@@ -41,6 +42,7 @@ public class NibblesView : GtkClutter.Embed
     private Gdk.Pixbuf boni_pixmaps[9];
 
     public Gee.HashMap<Worm, WormActor> worm_actors;
+    public Gee.HashMap<Bonus, BonusTexture> bonus_actors;
 
     public const int NUM_COLORS = 7;
     public static string[] color_lookup =
@@ -92,6 +94,7 @@ public class NibblesView : GtkClutter.Embed
         surface.set_opacity (100);
 
         worm_actors = new Gee.HashMap<Worm, WormActor> ();
+        bonus_actors = new Gee.HashMap<Bonus, BonusTexture> ();
 
         load_pixmap ();
 
@@ -422,12 +425,12 @@ public class NibblesView : GtkClutter.Embed
         }
         catch (Clutter.TextureError e)
         {
-            /* Fatal console error when the a worm's texture could not be set. */
+            /* 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 the a worm's texture could not be set. */
+            /* Fatal console error when a worm's texture could not be set. */
             error (_("Nibbles failed to set texture: %s"), e.message);
         }
 
@@ -481,12 +484,12 @@ public class NibblesView : GtkClutter.Embed
             }
             catch (Clutter.TextureError e)
             {
-                /* Fatal console error when the a worm's texture could not be set. */
+                /* 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 the a worm's texture could not be set. */
+                /* Fatal console error when a worm's texture could not be set. */
                 error (_("Nibbles failed to set texture: %s"), e.message);
             }
 
@@ -510,6 +513,35 @@ public class NibblesView : GtkClutter.Embed
         group.restore_easing_state ();
     }
 
+    public void bonus_added_cb ()
+    {
+        stderr.printf("[Debug] Bonus ADDED\n");
+        /* Last bonus added to the list is the one that needs a texture */
+        var bonus = game.boni.bonuses.last ();
+        var actor = new BonusTexture ();
+        try
+        {
+            actor.set_from_pixbuf (boni_pixmaps[bonus.type]);
+        }
+        catch (Clutter.TextureError e)
+        {
+            /* Fatal console error when a texture could not be set. */
+            error (_("Nibbles failed to set texture: %s"), e.message);
+        }
+        catch (Error e)
+        {
+            /* Fatal console error when a texture could not be set. */
+            error (_("Nibbles failed to set texture: %s"), e.message);
+        }
+
+        actor.set_position (bonus.x * game.tile_size, bonus.y * game.tile_size);
+        // actor.set_size (game.tile_size, game.tile_size);
+
+        stage.add_child (actor);
+
+        bonus_actors.set (bonus, actor);
+    }
+
     public static int colorval_from_name (string name)
     {
         for (int i = 0; i < NUM_COLORS; i++)
@@ -545,3 +577,22 @@ public class WormActor : Clutter.Actor
         restore_easing_state ();
     }
 }
+
+public class BonusTexture : GtkClutter.Texture
+{
+    public override void show ()
+    {
+        base.show ();
+
+        set_opacity (0);
+        set_scale (3.0, 3.0);
+
+        save_easing_state ();
+        set_easing_mode (Clutter.AnimationMode.EASE_OUT_BOUNCE);
+        set_easing_duration (NibblesGame.GAMEDELAY * 20);
+        set_scale (1.0, 1.0);
+        set_pivot_point (0.5f, 0.5f);
+        set_opacity (0xff);
+        restore_easing_state ();
+    }
+}
\ No newline at end of file


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