[gnome-nibbles/wip/vala] Add sound effects



commit 19c0d87711456711c4e5ae8570a9111f6bba6914
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Aug 17 15:05:23 2015 +0300

    Add sound effects

 data/org.gnome.nibbles.gschema.xml |    5 ++++
 src/Makefile.am                    |    1 +
 src/boni.vala                      |    4 ---
 src/config.vapi                    |    1 +
 src/gnome-nibbles.vala             |    1 +
 src/nibbles-view.vala              |   43 ++++++++++++++++++++++++++++++++++-
 6 files changed, 49 insertions(+), 6 deletions(-)
---
diff --git a/data/org.gnome.nibbles.gschema.xml b/data/org.gnome.nibbles.gschema.xml
index 3d3af8d..c897ad1 100644
--- a/data/org.gnome.nibbles.gschema.xml
+++ b/data/org.gnome.nibbles.gschema.xml
@@ -32,6 +32,11 @@
       <summary>Game level to start on</summary>
       <description>Game level to start on.</description>
     </key>
+    <key name="sound" type="b">
+      <default>true</default>
+      <summary>Enable sounds</summary>
+      <description>Enable sounds.</description>
+    </key>
   </schema>
   <schema id="org.gnome.nibbles.worm0" path="/org/gnome/nibbles/worm0/">
     <key name="color" type="s">
diff --git a/src/Makefile.am b/src/Makefile.am
index aad7043..0392dc8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -17,6 +17,7 @@ gnome_nibbles_CPPFLAGS = \
        -DVERSION=\"$(VERSION)\" \
        -DPKGDATADIR=\"$(datadir)/gnome-nibbles\" \
        -DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \
+       -DSOUND_DIRECTORY=\"$(datadir)/gnome-nibbles/sounds\" \
        $(GNOME_NIBBLES_CFLAGS)
 
 gnome_nibbles_VALAFLAGS = \
diff --git a/src/boni.vala b/src/boni.vala
index d83ff42..e0332a3 100644
--- a/src/boni.vala
+++ b/src/boni.vala
@@ -55,10 +55,6 @@ public class Boni : Object
         walls[x + 1, y + 1] = type + 'A';
         bonus_added ();
         numbonuses++;
-
-        //TODO
-        // if (type != BonusType.REGULAR)
-        //     play_sound ("appear");
     }
 
     public void remove_bonus (int[,] walls, Bonus bonus)
diff --git a/src/config.vapi b/src/config.vapi
index aa09de5..9aa9a8f 100644
--- a/src/config.vapi
+++ b/src/config.vapi
@@ -1,3 +1,4 @@
 public const string PKGDATADIR;
 public const string GETTEXT_PACKAGE;
 public const string VERSION;
+public const string SOUND_DIRECTORY;
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 08283c5..7df3f30 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -174,6 +174,7 @@ public class Nibbles : Gtk.Application
 
         view = new NibblesView (game);
         view.configure_event.connect (configure_event_cb);
+        view.is_muted = !settings.get_boolean ("sound");
         view.show ();
 
         frame = new Games.GridFrame (NibblesGame.WIDTH, NibblesGame.HEIGHT);
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 172cd12..f74363f 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -60,6 +60,8 @@ public class NibblesView : GtkClutter.Embed
       "purple"
     };
 
+    public bool is_muted;
+
     public NibblesView (NibblesGame game)
     {
         this.game = game;
@@ -574,6 +576,8 @@ public class NibblesView : GtkClutter.Embed
         group.set_pivot_point (0.5f, 0.5f);
         group.set_opacity (0);
         group.restore_easing_state ();
+
+        play_sound ("crash");
     }
 
     public void worm_tail_reduced_cb (Worm worm, int erase_size)
@@ -626,12 +630,10 @@ public class NibblesView : GtkClutter.Embed
             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");
         /* Last bonus added to the list is the one that needs a texture */
         var bonus = game.boni.bonuses.last ();
         var actor = new BonusTexture ();
@@ -653,6 +655,8 @@ public class NibblesView : GtkClutter.Embed
         actor.set_position (bonus.x * game.tile_size, bonus.y * game.tile_size);
 
         level.add_child (actor);
+        if (bonus.type != BonusType.REGULAR)
+            play_sound ("appear");
 
         bonus_actors.set (bonus, actor);
     }
@@ -676,6 +680,28 @@ public class NibblesView : GtkClutter.Embed
         actor.set_scale (1.45f, 1.45f);
         actor.set_pivot_point (0.5f, 0.5f);
         actor.restore_easing_state ();
+
+        var bonus = game.boni.bonuses.last ();
+        switch (bonus.type)
+        {
+            case BonusType.REGULAR:
+                play_sound ("gobble");
+                break;
+            case BonusType.DOUBLE:
+                play_sound ("bonus");
+                break;
+            case BonusType.HALF:
+                play_sound ("bonus");
+                break;
+            case BonusType.LIFE:
+                play_sound ("life");
+                break;
+            case BonusType.REVERSE:
+                play_sound ("reverse");
+                break;
+            default:
+                break;
+        }
     }
 
     public void boni_rescale (int tile_size)
@@ -706,6 +732,19 @@ public class NibblesView : GtkClutter.Embed
         }
     }
 
+    private void play_sound (string name)
+    {
+        if (is_muted)
+            return;
+
+        var filename = @"$(name).ogg";
+        var path = Path.build_filename (SOUND_DIRECTORY, filename, null);
+
+        CanberraGtk.play_for_widget (this, 0,
+                                     Canberra.PROP_MEDIA_NAME, name,
+                                     Canberra.PROP_MEDIA_FILENAME, path);
+    }
+
     public static int colorval_from_name (string name)
     {
         for (int i = 0; i < NUM_COLORS; i++)


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