[gnome-nibbles/wip/vala: 57/64] Implement game over



commit 9bb73b11bc92ecdcb346b36c85ae433547ea8b0e
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Wed Jul 29 18:09:37 2015 +0300

    Implement game over

 src/gnome-nibbles.vala    |   36 +++++++++++++++++++++++++++++++++---
 src/nibbles-game.vala     |   44 +++++++++++++++++++++++++++++++++++---------
 src/nibbles.gresource.xml |    8 --------
 src/worm.vala             |   12 ++++++------
 4 files changed, 74 insertions(+), 26 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 817db85..909f48c 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -364,10 +364,40 @@ public class Nibbles : Gtk.Application
         });
     }
 
-    public void log_score_cb (Worm worm)
+    public void log_score_cb (int score)
     {
-        scores_context.add_score (worm.score, cat_slow);
-        scores_context.run_dialog ();
+        if (game.numhumans != 1)
+            return;
+
+        if (game.start_level != 1)
+            return;
+
+        if (score <= 0)
+        {
+            stderr.printf("[Debug] 0\n");
+            return;
+        }
+        stderr.printf("[Debug] Here\n");
+        try
+        {
+            scores_context.add_score (score, cat_slow);
+
+        }
+        catch (GLib.Error e)
+        {
+            // Translators: This warning is displayed when adding a score fails
+            // just before displaying the score dialog
+            warning ("Failed to add score: %s", e.message);
+        }
+
+        try
+        {
+            scores_context.run_dialog ();
+        }
+        catch (GLib.Error e)
+        {
+            warning ("Failed to run scores dialog: %s", e.message);
+        }
     }
 
     private void scores_cb ()
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 04851bd..eba4908 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -42,7 +42,9 @@ public class NibblesGame : Object
     public const int GAMEDELAY = 35;
     public const int BONUSDELAY = 100;
 
-    public const int NUMWORMS = 6;
+    public const int NUMHUMANS = 2;
+    public const int NUMAI = 0;
+    public const int NUMWORMS = NUMHUMANS + NUMAI;
 
     public const int WIDTH = 92;
     public const int HEIGHT = 66;
@@ -55,7 +57,9 @@ public class NibblesGame : Object
 
     public Gee.LinkedList<Worm> worms;
 
-    public int numworms = NUMWORMS;
+    public int numhumans = NUMHUMANS;
+    public int numai = NUMAI;
+    public int numworms = NUMHUMANS + NUMAI;
 
     public int game_speed = 2;
 
@@ -67,7 +71,7 @@ public class NibblesGame : Object
     public signal void worm_moved (Worm worm);
     public signal void bonus_applied (Worm worm);
     public signal void loop_started ();
-    public signal void log_score (Worm worm);
+    public signal void log_score (int score);
     public signal void animate_end_game ();
 
     public Gee.HashMap<Worm, WormProperties?> worm_props;
@@ -257,6 +261,7 @@ public class NibblesGame : Object
         boni.bonuses.remove_all (found);
         // END FIXME
 
+        var dead_worms = new Gee.LinkedList<Worm> ();
         foreach (var worm in worms)
         {
             if (worm.is_stopped)
@@ -268,15 +273,17 @@ public class NibblesGame : Object
                     && worm != other_worm
                     && !other_worm.is_stopped)
                     {
-                        worm.die (walls);
-                        other_worm.die (walls);
+                        if (!dead_worms.contains (worm))
+                            dead_worms.add (worm);
+                        if (!dead_worms.contains (worm))
+                            dead_worms.add (other_worm);
                         continue;
                     }
             }
 
             if (!worm.can_move_to (walls, numworms))
             {
-                worm.die (walls);
+                dead_worms.add (worm);
                 continue;
             }
 
@@ -288,6 +295,15 @@ public class NibblesGame : Object
             else
                 worm.move (walls, true);
         }
+
+        foreach (var worm in dead_worms)
+        {
+            if (numworms > 1)
+                worm.score = worm.score * 7 / 10;
+
+            if (worm.lives > 0)
+                worm.reset (walls);
+        }
     }
 
     public void apply_bonus (Bonus bonus, Worm worm)
@@ -357,7 +373,15 @@ public class NibblesGame : Object
         var status = get_game_status ();
         loop_started ();
 
-        if (status == GameStatus.VICTORY)
+        if (status == GameStatus.GAMEOVER)
+        {
+            end_game ();
+
+            log_score (worms.first ().score);
+
+            return Source.REMOVE;
+        }
+        else if (status == GameStatus.VICTORY)
         {
             end_game ();
             var winner = get_winner ();
@@ -365,7 +389,7 @@ public class NibblesGame : Object
             if (winner == null)
                 return Source.REMOVE;
 
-            log_score (winner);
+            log_score (winner.score);
 
             return Source.REMOVE;
         }
@@ -381,6 +405,8 @@ public class NibblesGame : Object
         {
             if (worm.lives > 0)
                 worms_left += 1;
+            else if (worm.is_human && worm.lives <= 0)
+                return GameStatus.GAMEOVER;
         }
 
         if (worms_left == 1 && numworms > 1)
@@ -456,7 +482,7 @@ public class NibblesGame : Object
     {
         foreach (var worm in worms)
         {
-            if (worm.human)
+            if (worm.is_human)
             {
                 if (worm.handle_keypress (keyval, worm_props))
                     return true;
diff --git a/src/worm.vala b/src/worm.vala
index d1c4c98..62ce5f6 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -22,14 +22,14 @@
 public class Worm : Object
 {
     public const int STARTING_LENGTH = 5;
-    private const int STARTING_LIVES = 6;
+    private const int STARTING_LIVES = 2;
     public const int GROW_FACTOR = 4;
 
     public Position starting_position { get; private set; }
 
     public int id { get; private set; }
 
-    public bool human;
+    public bool is_human;
     public bool keypress = false;
     public bool is_stopped = false;
 
@@ -77,7 +77,7 @@ public class Worm : Object
     public Worm (int id, WormDirection direction)
     {
         this.id = id;
-        human = true;
+        is_human = true;
         starting_direction = direction;
         lives = STARTING_LIVES;
         score = 0;
@@ -105,7 +105,7 @@ public class Worm : Object
 
     public void move (int[,] walls, bool remove)
     {
-        if (human)
+        if (is_human)
             keypress = false;
 
         var position = head ();
@@ -164,7 +164,7 @@ public class Worm : Object
         {
             if (length <= erase_size)
             {
-                die (walls);
+                reset (walls);
             }
 
             for (int i = 0; i < erase_size; i++)
@@ -212,7 +212,7 @@ public class Worm : Object
         lives--;
     }
 
-    public void die (int[,] walls)
+    public void reset (int[,] walls)
     {
         is_stopped = true;
         lose_life ();


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