[gnome-nibbles] Implement scores via libgames-support



commit d674083210727368be6fa7e4575689271bd2aad9
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Jul 27 16:11:39 2015 +0300

    Implement scores via libgames-support

 src/gnome-nibbles.vala |   47 ++++++++++++++++++++++++++++++++++++++++-
 src/nibbles-game.vala  |   54 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 2 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 4e93f13..c526f54 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -38,10 +38,16 @@ public class Nibbles : Gtk.Application
     private Gee.LinkedList<Gtk.ToggleButton> number_of_players_buttons;
     private Gtk.Revealer next_button_revealer;
 
+    private Games.Scores.Context scores_context;
+    private Games.Scores.Category cat_beginner;
+    private Games.Scores.Category cat_slow;
+    private Games.Scores.Category cat_medium;
+    private Games.Scores.Category cat_fast;
+
     private NibblesView? view;
     private NibblesGame? game = null;
 
-    private const int COUNTDOWN_TIME = 5;
+    private const int COUNTDOWN_TIME = 0;
 
     private const ActionEntry action_entries[] =
     {
@@ -85,6 +91,7 @@ public class Nibbles : Gtk.Application
 
     protected override void startup ()
     {
+        stderr.printf("[Debug] Startup\n");
         base.startup ();
 
         var css_provider = new Gtk.CssProvider ();
@@ -109,6 +116,7 @@ public class Nibbles : Gtk.Application
         window.size_allocate.connect (size_allocate_cb);
         window.window_state_event.connect (window_state_event_cb);
         window.set_default_size (settings.get_int ("window-width"), settings.get_int ("window-height"));
+
         if (settings.get_boolean ("window-is-maximized"))
             window.maximize ();
 
@@ -132,6 +140,7 @@ public class Nibbles : Gtk.Application
 
         /* Load game */
         game = new NibblesGame (settings);
+        game.log_score.connect (log_score_cb);
 
         view = new NibblesView (game);
         view.show ();
@@ -154,11 +163,16 @@ public class Nibbles : Gtk.Application
         else
             // show_new_game_screen_cb ();
             start_game_cb ();
+
+        window.show_all ();
+
+        create_scores ();
     }
 
     protected override void activate ()
     {
-        window.present ();
+        stderr.printf("[Debug] Activate\n");
+        base.activate ();
     }
 
     protected override void shutdown ()
@@ -326,6 +340,35 @@ public class Nibbles : Gtk.Application
         }
     }
 
+    public void create_scores ()
+    {
+        stderr.printf("[Debug] Created\n");
+        scores_context = new Games.Scores.Context ("gnome-nibbles", "", window, 
Games.Scores.Style.PLAIN_DESCENDING);
+        cat_beginner = new Games.Scores.Category ("beginner", "Beginner");
+        cat_slow = new Games.Scores.Category ("slow", "Slow");
+        cat_medium = new Games.Scores.Category ("medium", "Medium");
+        cat_fast = new Games.Scores.Category ("fast", "Fast");
+
+        scores_context.category_request.connect ((s, key) => {
+            if (key == "beginner")
+                return cat_beginner;
+            else if (key == "slow")
+                return cat_slow;
+            else if (key == "cat_medium")
+                return cat_medium;
+            else if (key == "cat_fast")
+                return cat_fast;
+            else
+                return null;
+        });
+    }
+
+    public void log_score_cb (Worm worm)
+    {
+        scores_context.add_score (worm.score, cat_slow);
+        scores_context.run_dialog ();
+    }
+
     public static int main (string[] args)
     {
         var context = new OptionContext ("");
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 57701f4..ecc6ad7 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -64,6 +64,7 @@ public class NibblesGame : Object
     public signal void worm_moved (Worm worm);
     public signal void bonus_applied (Worm worm);
     public signal void loop_ended ();
+    public signal void log_score (Worm worm);
 
     public Gee.HashMap<Worm, WormProperties?> worm_props;
 
@@ -334,11 +335,57 @@ public class NibblesGame : Object
 
     public bool main_loop_cb ()
     {
+        var status = get_game_status ();
+
+        if (status == GameStatus.VICTORY)
+        {
+            // end_game ();
+            var winner = get_winner ();
+
+            if (winner == null)
+                return Source.REMOVE;
+
+            stderr.printf("[Debug] Logging score\n");
+            log_score (winner);
+
+            return Source.REMOVE;
+        }
         move_worms ();
         loop_ended ();
+
         return Source.CONTINUE;
     }
 
+    public GameStatus? get_game_status ()
+    {
+        var worms_left = 0;
+        foreach (var worm in worms)
+        {
+            if (worm.lives > 0)
+                worms_left += 1;
+        }
+
+        if (worms_left == 1 && numworms > 1)
+            return GameStatus.VICTORY;
+        else if (worms_left == 0) {
+            /* There was only one worm and it died */
+            return GameStatus.GAMEOVER;
+        }
+
+        return null;
+    }
+
+    public Worm? get_winner ()
+    {
+        foreach (var worm in worms)
+        {
+            if (worm.lives > 0)
+                return worm;
+        }
+
+        return null;
+    }
+
     public void load_properties (Settings settings)
     {
         tile_size = settings.get_int ("tile-size");
@@ -394,3 +441,10 @@ public class NibblesGame : Object
         return false;
     }
 }
+
+public enum GameStatus
+{
+    GAMEOVER,
+    VICTORY,
+    NEXTROUND
+}


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