[gnome-nibbles] Make worms transparent on respawn



commit 3cb94eac4a7595470919c3b020ef69c8e4e1d300
Author: Razvan Chitu <razvan ch95 gmail com>
Date:   Mon Nov 30 15:38:01 2015 +0200

    Make worms transparent on respawn
    
    https://bugzilla.gnome.org/show_bug.cgi?id=757874

 src/gnome-nibbles.vala |    2 +-
 src/nibbles-view.vala  |   13 +++++++++++
 src/worm.vala          |   56 ++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 64 insertions(+), 7 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 521b230..aecfeec 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -177,7 +177,7 @@ public class Nibbles : Gtk.Application
         game = new NibblesGame (settings);
         game.log_score.connect (log_score_cb);
         game.level_completed.connect (level_completed_cb);
-        game.notify["is_paused"].connect (() => {
+        game.notify["is-paused"].connect (() => {
             if (game.is_paused)
                 statusbar_stack.set_visible_child_name ("paused");
             else
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index ec12c8e..e7f780a 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -503,6 +503,17 @@ public class NibblesView : GtkClutter.Embed
             worm.died.connect (worm_died_cb);
             worm.tail_reduced.connect (worm_tail_reduced_cb);
             worm.reversed.connect (worm_reversed_cb);
+            worm.notify["is-materialized"].connect (() => {
+                uint8 opacity;
+                opacity = worm.is_materialized ? 0xff : 0x50;
+
+                var actors = worm_actors.get (worm);
+
+                actors.save_easing_state ();
+                actors.set_easing_duration (NibblesGame.GAMEDELAY * 10);
+                actors.set_opacity (opacity);
+                actors.restore_easing_state ();
+            });
         }
     }
 
@@ -634,6 +645,8 @@ public class NibblesView : GtkClutter.Embed
         actors.set_opacity (0xff);
         actors.restore_easing_state ();
 
+        worm.dematerialize (game.board, 3);
+
         Timeout.add (NibblesGame.GAMEDELAY * 27, () => {
             worm.is_stopped = false;
             return Source.REMOVE;
diff --git a/src/worm.vala b/src/worm.vala
index 6b5a0fc..c728401 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -58,6 +58,8 @@ public class Worm : Object
     public bool is_human;
     public bool keypress = false;
     public bool is_stopped = false;
+    public bool is_materialized { get; private set; default = true; }
+    private int rounds_dematerialized;
 
     public int lives { get; set; }
     public int change;
@@ -182,11 +184,17 @@ public class Worm : Object
         if (board[head.x, head.y] != NibblesGame.EMPTYCHAR)
             bonus_found ();
 
-        /* Mark the tile as occupied by the worm's body */
-        board[head.x, head.y] = NibblesGame.WORMCHAR + id;
+        /* Mark the tile as occupied by the worm's body, if it is materialized */
+        if (is_materialized)
+            board[head.x, head.y] = NibblesGame.WORMCHAR + id;
+        else
+            rounds_dematerialized -= 1;
 
         if (!key_queue.is_empty)
             dequeue_keypress ();
+
+        if (rounds_dematerialized == 1)
+            materialize (board);
     }
 
     public void reduce_tail (int[,] board, int erase_size)
@@ -231,18 +239,24 @@ public class Worm : Object
     public bool can_move_to (int[,] board, int numworms)
     {
         var position = position_move ();
+        int next_position = board[position.x, position.y];
 
-        if (board[position.x, position.y] > NibblesGame.EMPTYCHAR
-            && board[position.x, position.y] < 'z' + numworms)
-        {
+        if (next_position > NibblesGame.EMPTYCHAR
+            && next_position < NibblesGame.WORMCHAR)
             return false;
-        }
+
+        if (next_position >= NibblesGame.WORMCHAR
+            && next_position < NibblesGame.WORMCHAR + numworms)
+            return !is_materialized;
 
         return true;
     }
 
     public bool will_collide_with_head (Worm other_worm)
     {
+        if (!is_materialized || !other_worm.is_materialized)
+            return false;
+
         var worm_pos = position_move ();
         var other_worm_pos = other_worm.position_move ();
 
@@ -259,6 +273,33 @@ public class Worm : Object
             move (board);
     }
 
+    private void materialize (int [,] board)
+    {
+        foreach (var pos in list)
+        {
+            if (board[pos.x, pos.y] != NibblesGame.EMPTYCHAR)
+            {
+                rounds_dematerialized += 1;
+                return;
+            }
+        }
+        foreach (var pos in list)
+            board[pos.x, pos.y] = NibblesGame.WORMCHAR + id;
+        is_materialized = true;
+        rounds_dematerialized = 0;
+    }
+
+    public void dematerialize (int [,] board, int rounds)
+    {
+        rounds_dematerialized = rounds;
+        is_materialized = false;
+        foreach (var pos in list)
+        {
+            if (board[pos.x, pos.y] == NibblesGame.WORMCHAR + id)
+                board[pos.x, pos.y] = NibblesGame.EMPTYCHAR;
+        }
+    }
+
     public void add_life ()
     {
         if (lives > MAX_LIVES)
@@ -275,6 +316,9 @@ public class Worm : Object
     public void reset (int[,] board)
     {
         is_stopped = true;
+        is_materialized = false;
+        rounds_dematerialized = 0;
+
         key_queue.clear ();
 
         lose_life ();


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