[gnome-2048] Make game responsive.



commit 0404c94f74707b3a94e60b35dfde7773cf3c6343
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sun Feb 3 07:14:50 2019 +0100

    Make game responsive.
    
    That was harder than I hoped.
    
    A player can now start a move
    while the new-tile animations
    are playing. It makes game so
    responsive that I increased a
    bit the animations durations.
    
    Closes #7.

 data/org.gnome.TwentyFortyEight.gschema.xml |  2 +-
 data/preferences.ui                         |  6 +--
 src/application.vala                        |  6 +--
 src/game.vala                               | 68 ++++++++++++++++++-----------
 4 files changed, 50 insertions(+), 32 deletions(-)
---
diff --git a/data/org.gnome.TwentyFortyEight.gschema.xml b/data/org.gnome.TwentyFortyEight.gschema.xml
index 5e79288..dca0c5b 100644
--- a/data/org.gnome.TwentyFortyEight.gschema.xml
+++ b/data/org.gnome.TwentyFortyEight.gschema.xml
@@ -53,7 +53,7 @@
       <description>Whether the user shall be congratulated on obtaining target tile.</description>
     </key>
     <key name="animations-speed" type="d">
-      <default>100.0</default>
+      <default>130.0</default>
       <!-- as in the preferences.ui file -->
       <range min="20.0" max="2000.0"/>
       <!-- Translators: summary of a settings key, see 'dconf-editor /org/gnome/2048/animations-speed' -->
diff --git a/data/preferences.ui b/data/preferences.ui
index 3548bee..7516ebf 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -108,19 +108,19 @@
       <!-- Translators: on preferences window, entry of the menu for changing animations speed (with a 
mnemonic that appears pressing Alt)-->
       <attribute name="label" translatable="yes">_Slow</attribute>
       <attribute name="action">app.animations-speed</attribute>
-      <attribute name="target">250.0</attribute>
+      <attribute name="target">260.0</attribute>
     </item>
     <item>
       <!-- Translators: on preferences window, entry of the menu for changing animations speed (with a 
mnemonic that appears pressing Alt) -->
       <attribute name="label" translatable="yes">_Normal</attribute>
       <attribute name="action">app.animations-speed</attribute>
-      <attribute name="target">100.0</attribute>
+      <attribute name="target">130.0</attribute>
     </item>
     <item>
       <!-- Translators: on preferences window, entry of the menu for changing animations speed (with a 
mnemonic that appears pressing Alt) -->
       <attribute name="label" translatable="yes">_Fast</attribute>
       <attribute name="action">app.animations-speed</attribute>
-      <attribute name="target">40.0</attribute>
+      <attribute name="target">65.0</attribute>
     </item>
   </menu>
 </interface>
diff --git a/src/application.vala b/src/application.vala
index 9442307..ffb1144 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -544,15 +544,15 @@ private class Application : Gtk.Application
     }
     private static inline void _get_animations_button_label (ref double speed, out string 
_animations_button_label)
     {
-        if (speed == 100.0)
+        if (speed == 130.0)
             /* Translators: in the preferences dialog; possible label of the MenuButton to choose animation 
speed */
             _animations_button_label = _("Normal");
 
-        else if (speed == 40.0)
+        else if (speed == 65.0)
             /* Translators: in the preferences dialog; possible label of the MenuButton to choose animation 
speed */
             _animations_button_label = _("Fast");
 
-        else if (speed == 250.0)
+        else if (speed == 260.0)
             /* Translators: in the preferences dialog; possible label of the MenuButton to choose animation 
speed */
             _animations_button_label = _("Slow");
 
diff --git a/src/game.vala b/src/game.vala
index ebe8f0d..984027e 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -24,7 +24,7 @@ private class Game : Object
         IDLE,
         MOVING,
         SHOWING_FIRST_TILE,
-        SHOWING_SECOND_TILE,
+        SHOWING_NEW_TILE,
         RESTORING_TILES
     }
 
@@ -177,7 +177,8 @@ private class Game : Object
 
     internal bool cannot_move ()
     {
-        return _state != GameState.IDLE;
+        return _state != GameState.IDLE
+            && _state != GameState.SHOWING_NEW_TILE;
     }
 
     internal void load_settings (ref GLib.Settings settings)
@@ -287,6 +288,13 @@ private class Game : Object
         Tile tile;
         _grid.new_tile (out tile);
 
+        if (_state == GameState.SHOWING_FIRST_TILE)
+            _update_handled = true;
+        else
+        {
+            _update_handled = false;
+            _state = GameState.SHOWING_NEW_TILE;
+        }
         _create_show_hide_transition (true);
 
         _create_tile (tile);
@@ -456,6 +464,11 @@ private class Game : Object
 
     internal void move (MoveRequest request)
     {
+        if (_state == GameState.SHOWING_NEW_TILE)
+            _apply_move ();
+        else if (_state != GameState.IDLE)
+            assert_not_reached ();
+
         debug (MoveRequest.debug_string (request));
 
         Grid clone = _grid.clone ();
@@ -487,12 +500,11 @@ private class Game : Object
         }
     }
 
-    private void _on_move_trans_stopped (bool is_finished)
+    private void _on_move_trans_stopped (Clutter.Timeline trans, bool is_finished)
     {
-        debug (@"move animation stopped; finished $is_finished");
-        debug (@"$_grid");
+        debug (@"move animation stopped\n$_grid");
 
-        _move_trans.remove_all ();
+        ((Clutter.TransitionGroup) trans).remove_all ();
 
         foreach (TileMovement? e in _to_hide)
         {
@@ -514,8 +526,6 @@ private class Game : Object
         _store_score_update (delta_score);
 
         _create_random_tile ();
-
-        _show_hide_trans.start ();
     }
 
     /*\
@@ -526,28 +536,39 @@ private class Game : Object
     internal signal void target_value_reached (uint val);
 
     private uint _finish_move_id = 0;
+    private bool _update_handled = false;
 
     private void _create_show_hide_transition (bool animate)
     {
         _show_hide_trans = new Clutter.TransitionGroup ();
         _show_hide_trans.stopped.connect (_on_show_hide_trans_stopped);
-        _show_hide_trans.set_duration (animate ? _animations_duration / 2 : 10);
+        /* _show_hide_trans should be finished two times (forward and backward) before
+           one _move_trans is done, so at least animation time should be strictly half */
+        _show_hide_trans.set_duration (animate ? _animations_duration / 3 : 10);
     }
 
-    private void _on_show_hide_trans_stopped (bool is_finished)
+    private void _on_show_hide_trans_stopped (Clutter.Timeline trans, bool is_finished)
     {
-        debug (@"show/hide animation stopped; finished $is_finished");
+        debug ("show/hide animation stopped");
 
-        if (_show_hide_trans.direction == Clutter.TimelineDirection.FORWARD)
+        if (trans.direction == Clutter.TimelineDirection.FORWARD)
         {
-            _show_hide_trans.direction = Clutter.TimelineDirection.BACKWARD;
-            _show_hide_trans.start ();
+            trans.direction = Clutter.TimelineDirection.BACKWARD;
+            trans.start ();
             return;
         }
 
+        ((Clutter.TransitionGroup) trans).remove_all ();
+        _apply_move ();
+    }
+
+    private void _apply_move ()
+    {
         debug (@"$_grid");
 
-        _show_hide_trans.remove_all ();
+        if (_update_handled && _state != GameState.SHOWING_FIRST_TILE)
+            return;
+        _update_handled = true;
 
         foreach (TileMovement? e in _to_hide)
         {
@@ -566,15 +587,10 @@ private class Game : Object
 
         if (_state == GameState.SHOWING_FIRST_TILE)
         {
-            _state = GameState.SHOWING_SECOND_TILE;
+            _state = GameState.SHOWING_NEW_TILE;
             debug ("state show second tile");
             _create_random_tile ();
         }
-        else if (_state == GameState.SHOWING_SECOND_TILE)
-        {
-            _state = GameState.IDLE;
-            debug ("state idle");
-        }
         else if (_state != GameState.IDLE)
         {
             _state = GameState.IDLE;
@@ -639,17 +655,19 @@ private class Game : Object
     private Gee.LinkedList<uint> _undo_score_stack = new Gee.LinkedList<uint> ();
 
     internal void undo ()
+        requires (_allow_undo == true)
     {
-        Grid grid = _undo_stack.poll_head ();
-        uint delta_score = _undo_score_stack.poll_head ();
+        if (_state != GameState.IDLE)
+            return;
 
         _clear_foreground ();
-        _grid = grid;
+        _grid = _undo_stack.poll_head ();
         _restore_foreground (false);
-        score -= delta_score;
+        score -= _undo_score_stack.poll_head ();
 
         if (_undo_stack.size == 0)
             undo_disabled ();
+        _update_handled = false;
     }
 
     private void _load_undo_settings (ref GLib.Settings settings)


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