[gnome-nibbles/wip/vala: 49/64] Update scoreboard after each loop step



commit fd94a8eb2231aac14472fe8f9f48389d7b9d54db
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Tue Jul 21 18:29:03 2015 +0300

    Update scoreboard after each loop step

 src/gnome-nibbles.vala |   40 ++++++++++++++++++++++++++++++++++++++--
 src/nibbles-game.vala  |    2 ++
 2 files changed, 40 insertions(+), 2 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 27fbdb8..7ed84fa 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -237,6 +237,7 @@ public class Nibbles : Gtk.Application
          */
         game.load_properties (settings);
         game.current_level = game.start_level;
+        game.loop_ended.connect (scoreboard.update);
         view.new_level (game.current_level);
         view.configure_event.connect (configure_event_cb);
 
@@ -367,11 +368,11 @@ public class Nibbles : Gtk.Application
 [GtkTemplate (ui = "/org/gnome/nibbles/ui/scoreboard.ui")]
 public class Scoreboard : Gtk.Box
 {
-    private Gee.LinkedList<PlayerScoreBox> boxes;
+    private Gee.HashMap<PlayerScoreBox, Worm> boxes;
 
     public Scoreboard ()
     {
-        boxes = new Gee.LinkedList<PlayerScoreBox> ();
+        boxes = new Gee.HashMap<PlayerScoreBox, Worm> ();
     }
 
     public void register (Worm worm, string color_name, Gdk.Pixbuf life_pixbuf)
@@ -380,8 +381,17 @@ public class Scoreboard : Gtk.Box
         color.parse (color_name);
 
         var box = new PlayerScoreBox ("Worm %d".printf (worm.id + 1), color, worm.score, worm.lives, 
life_pixbuf);
+        boxes.set (box, worm);
         add (box);
     }
+
+    public void update ()
+    {
+        foreach (var entry in boxes.entries)
+        {
+            entry.key.update (entry.value.score, entry.value.lives);
+        }
+    }
 }
 
 [GtkTemplate (ui = "/org/gnome/nibbles/ui/player-score-box.ui")]
@@ -412,4 +422,30 @@ public class PlayerScoreBox : Gtk.Box
             lives_grid.attach (life, i % 6, i/6);
         }
     }
+
+    public void update (int score, int lives_left)
+    {
+        update_score (score);
+        update_lives (lives_left);
+    }
+
+    public void update_score (int score)
+    {
+        if (score_label.get_label () == score.to_string ())
+            return;
+
+        score_label.set_label (score.to_string ());
+    }
+
+    public void update_lives (int lives_left)
+    {
+        if (life_images.size == lives_left)
+            return;
+
+        for (int i = 0; i < life_images.size - lives_left; i++)
+        {
+            var life = life_images.poll ();
+            life.hide ();
+        }
+    }
 }
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 85aec96..57701f4 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -63,6 +63,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 Gee.HashMap<Worm, WormProperties?> worm_props;
 
@@ -334,6 +335,7 @@ public class NibblesGame : Object
     public bool main_loop_cb ()
     {
         move_worms ();
+        loop_ended ();
         return Source.CONTINUE;
     }
 


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