[quadrapassel] game: Let the game control fast movement



commit 5b8cf4a89542e0b6344b52f536f2ceefd9a1b9c4
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sat Dec 2 13:40:14 2017 +0100

    game: Let the game control fast movement
    
    Fast movement was relegated to GTK+ sending events multiple times.
    
    This implements a similar behavior directly into the game so we can have
    it behaving consistently accross the different kinds of controllers.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791129

 src/game.vala         |   65 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/quadrapassel.vala |    8 +++++-
 2 files changed, 70 insertions(+), 3 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index 3e5d1f4..20c2e76 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -244,6 +244,12 @@ public class Game : Object
     /* The current level */
     public int level { get { return starting_level + n_lines_destroyed / 10; } }
 
+    /* The direction we are moving */
+    private int fast_move_direction = 0;
+
+    /* Timer to animate moving fast */
+    private uint fast_move_timeout = 0;
+
     /* true if we are in fast forward mode */
     private bool fast_forward = false;
 
@@ -344,6 +350,7 @@ public class Game : Object
         g.score = score;
         g.starting_level = starting_level;
         g.pick_difficult_blocks = pick_difficult_blocks;
+        g.fast_move_direction = fast_move_direction;
         g.fast_forward = fast_forward;
         g.has_started = has_started;
         g._paused = _paused;
@@ -363,12 +370,25 @@ public class Game : Object
 
     public bool move_left ()
     {
-        return move_shape (-1, 0, 0);
+        return move_direction (-1);
     }
 
     public bool move_right ()
     {
-        return move_shape (1, 0, 0);
+        return move_direction (1);
+    }
+
+    public bool stop_moving ()
+    {
+        if (game_over)
+            return false;
+
+        if (fast_move_timeout != 0)
+            Source.remove (fast_move_timeout);
+        fast_move_timeout = 0;
+        fast_move_direction = 0;
+
+        return true;
     }
 
     public bool rotate_left ()
@@ -407,6 +427,47 @@ public class Game : Object
             Source.remove (drop_timeout);
     }
 
+    private bool move_direction (int direction)
+    {
+        if (game_over)
+            return false;
+        if (fast_move_direction == direction)
+            return true;
+
+        if (fast_move_timeout != 0)
+            Source.remove (fast_move_timeout);
+        fast_move_timeout = 0;
+        fast_move_direction = direction;
+        if (!move ())
+            return false;
+
+        fast_move_timeout = Timeout.add (500, setup_fast_move_cb);
+
+        return true;
+    }
+
+    private bool setup_fast_move_cb ()
+    {
+        if (!move ())
+            return false;
+        fast_move_timeout = Timeout.add (40, move);
+
+        return false;
+    }
+
+    private bool move ()
+    {
+        if (!move_shape (fast_move_direction, 0, 0))
+        {
+            fast_move_timeout = 0;
+            fast_move_direction = 0;
+
+            return false;
+        }
+
+        return true;
+    }
+
     private void setup_drop_timer ()
     {
         var timestep = (int) Math.round (80 + 800.0 * Math.pow (0.75, level - 1));
diff --git a/src/quadrapassel.vala b/src/quadrapassel.vala
index 2d297b2..5ff79a6 100644
--- a/src/quadrapassel.vala
+++ b/src/quadrapassel.vala
@@ -611,7 +611,13 @@ public class Quadrapassel : Gtk.Application
         if (game == null)
             return false;
 
-        if (keyval == upper_key (settings.get_int ("key-down")))
+        if (keyval == upper_key (settings.get_int ("key-left")) ||
+            keyval == upper_key (settings.get_int ("key-right")))
+        {
+            game.stop_moving ();
+            return true;
+        }
+        else if (keyval == upper_key (settings.get_int ("key-down")))
         {
             game.set_fast_forward (false);
             return true;


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