[gnome-2048] Unduplicate some code.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-2048] Unduplicate some code.
- Date: Thu, 31 Jan 2019 22:00:41 +0000 (UTC)
commit ce400c3c282a1f70955e46e85198a4f9c4caf9d7
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Thu Jan 31 21:05:31 2019 +0100
Unduplicate some code.
src/application.vala | 28 +++++++-----
src/game.vala | 121 ++++-----------------------------------------------
src/grid.vala | 38 +++++++++++-----
3 files changed, 51 insertions(+), 136 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index b8777cf..2651d20 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -706,25 +706,31 @@ private class Application : Gtk.Application
* * move requests
\*/
- private enum MoveRequest {
- UP,
- RIGHT,
- DOWN,
- LEFT;
- }
-
private void _request_move (MoveRequest request)
{
if (_game_should_init)
return;
_game_restored = false;
+ _game.move (request);
+ }
+}
+
+private enum MoveRequest {
+ UP,
+ RIGHT,
+ DOWN,
+ LEFT;
+
+ internal static string debug_string (MoveRequest request)
+ {
switch (request)
{
- case MoveRequest.UP : _game.move_up (); break;
- case MoveRequest.RIGHT : _game.move_right (); break;
- case MoveRequest.DOWN : _game.move_down (); break;
- case MoveRequest.LEFT : _game.move_left (); break;
+ case UP: return "move up";
+ case RIGHT: return "move right";
+ case DOWN: return "move down";
+ case LEFT: return "move left";
+ default: assert_not_reached ();
}
}
}
diff --git a/src/game.vala b/src/game.vala
index 20a3dcb..7ea6bbc 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -22,10 +22,7 @@ private class Game : Object
enum GameState {
STOPPED,
IDLE,
- MOVING_DOWN,
- MOVING_UP,
- MOVING_RIGHT,
- MOVING_LEFT,
+ MOVING,
SHOWING_FIRST_TILE,
SHOWING_SECOND_TILE,
RESTORING_TILES
@@ -368,9 +365,9 @@ private class Game : Object
tile.val);
}
- internal void move_down ()
+ internal void move (MoveRequest request)
{
- debug ("move down");
+ debug (MoveRequest.debug_string (request));
Grid clone = _grid.clone ();
@@ -378,7 +375,7 @@ private class Game : Object
_move_trans.stopped.connect (_on_move_trans_stopped);
_move_trans.set_duration (_animations_duration);
- _grid.move_down (_to_move, _to_hide, _to_show);
+ _grid.move (request, _to_move, _to_hide, _to_show);
foreach (TileMovement? e in _to_move)
{
@@ -395,106 +392,7 @@ private class Game : Object
if ((_to_move.size > 0) || (_to_hide.size > 0) || (_to_show.size > 0))
{
- _state = GameState.MOVING_DOWN;
- _move_trans.start ();
- _store_movement (clone);
- }
- }
-
- internal void move_up ()
- {
- debug ("move up");
-
- Grid clone = _grid.clone ();
-
- _move_trans = new Clutter.TransitionGroup ();
- _move_trans.stopped.connect (_on_move_trans_stopped);
- _move_trans.set_duration (_animations_duration);
-
- _grid.move_up (_to_move, _to_hide, _to_show);
-
- foreach (TileMovement? e in _to_move)
- {
- if (e == null)
- assert_not_reached ();
- _move_tile (((!) e).from, ((!) e).to);
- }
- foreach (TileMovement? e in _to_hide)
- {
- if (e == null)
- assert_not_reached ();
- _prepare_move_tile (((!) e).from, ((!) e).to);
- }
-
- if ((_to_move.size > 0) || (_to_hide.size > 0) || (_to_show.size > 0))
- {
- _state = GameState.MOVING_UP;
- _move_trans.start ();
- _store_movement (clone);
- }
- }
-
- internal void move_left ()
- {
- debug ("move left");
-
- Grid clone = _grid.clone ();
-
- _move_trans = new Clutter.TransitionGroup ();
- _move_trans.stopped.connect (_on_move_trans_stopped);
- _move_trans.set_duration (_animations_duration);
-
- _grid.move_left (_to_move, _to_hide, _to_show);
-
- foreach (TileMovement? e in _to_move)
- {
- if (e == null)
- assert_not_reached ();
- _move_tile (((!) e).from, ((!) e).to);
- }
- foreach (TileMovement? e in _to_hide)
- {
- if (e == null)
- assert_not_reached ();
- _prepare_move_tile (((!) e).from, ((!) e).to);
- }
-
- if ((_to_move.size > 0) || (_to_hide.size > 0) || (_to_show.size > 0))
- {
- _state = GameState.MOVING_LEFT;
- _move_trans.start ();
- _store_movement (clone);
- }
- }
-
- internal void move_right ()
- {
- debug ("move right");
-
- Grid clone = _grid.clone ();
-
- _move_trans = new Clutter.TransitionGroup ();
- _move_trans.stopped.connect (_on_move_trans_stopped);
- _move_trans.set_duration (_animations_duration);
-
- _grid.move_right (_to_move, _to_hide, _to_show);
-
- foreach (TileMovement? e in _to_move)
- {
- if (e == null)
- assert_not_reached ();
- _move_tile (((!) e).from, ((!) e).to);
- }
- foreach (TileMovement? e in _to_hide)
- {
- if (e == null)
- assert_not_reached ();
- _prepare_move_tile (((!) e).from, ((!) e).to);
- }
-
- if ((_to_move.size > 0) || (_to_hide.size > 0) || (_to_show.size > 0))
- {
- _state = GameState.MOVING_RIGHT;
+ _state = GameState.MOVING;
_move_trans.start ();
_store_movement (clone);
}
@@ -614,9 +512,6 @@ private class Game : Object
private void _restore_foreground (bool animate)
{
- uint val;
- GridPosition pos;
- Tile tile;
int rows = _grid.rows;
int cols = _grid.cols;
@@ -626,11 +521,11 @@ private class Game : Object
{
for (int j = 0; j < cols; j++)
{
- val = _grid [i, j];
+ uint val = _grid [i, j];
if (val != 0)
{
- pos = { i, j };
- tile = { pos, val };
+ GridPosition pos = { i, j };
+ Tile tile = { pos, val };
_create_tile (tile);
_to_show.add (tile);
_show_tile (pos);
diff --git a/src/grid.vala b/src/grid.vala
index f0f8c3f..a5ceada 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -80,9 +80,23 @@ private class Grid : Object
}
}
- internal void move_down (Gee.LinkedList<TileMovement?> to_move,
- Gee.LinkedList<TileMovement?> to_hide,
- Gee.LinkedList<Tile?> to_show)
+ internal inline void move (MoveRequest request,
+ Gee.LinkedList<TileMovement?> to_move,
+ Gee.LinkedList<TileMovement?> to_hide,
+ Gee.LinkedList<Tile?> to_show)
+ {
+ switch (request)
+ {
+ case MoveRequest.DOWN: move_down (to_move, to_hide, to_show); return;
+ case MoveRequest.UP: move_up (to_move, to_hide, to_show); return;
+ case MoveRequest.LEFT: move_left (to_move, to_hide, to_show); return;
+ case MoveRequest.RIGHT: move_right (to_move, to_hide, to_show); return;
+ }
+ }
+
+ private void move_down (Gee.LinkedList<TileMovement?> to_move,
+ Gee.LinkedList<TileMovement?> to_hide,
+ Gee.LinkedList<Tile?> to_show)
{
to_move.clear ();
to_hide.clear ();
@@ -161,9 +175,9 @@ private class Grid : Object
}
}
- internal void move_up (Gee.LinkedList<TileMovement?> to_move,
- Gee.LinkedList<TileMovement?> to_hide,
- Gee.LinkedList<Tile?> to_show)
+ private void move_up (Gee.LinkedList<TileMovement?> to_move,
+ Gee.LinkedList<TileMovement?> to_hide,
+ Gee.LinkedList<Tile?> to_show)
{
to_move.clear ();
to_hide.clear ();
@@ -242,9 +256,9 @@ private class Grid : Object
}
}
- internal void move_left (Gee.LinkedList<TileMovement?> to_move,
- Gee.LinkedList<TileMovement?> to_hide,
- Gee.LinkedList<Tile?> to_show)
+ private void move_left (Gee.LinkedList<TileMovement?> to_move,
+ Gee.LinkedList<TileMovement?> to_hide,
+ Gee.LinkedList<Tile?> to_show)
{
to_move.clear ();
to_hide.clear ();
@@ -323,9 +337,9 @@ private class Grid : Object
}
}
- internal void move_right (Gee.LinkedList<TileMovement?> to_move,
- Gee.LinkedList<TileMovement?> to_hide,
- Gee.LinkedList<Tile?> to_show)
+ private void move_right (Gee.LinkedList<TileMovement?> to_move,
+ Gee.LinkedList<TileMovement?> to_hide,
+ Gee.LinkedList<Tile?> to_show)
{
to_move.clear ();
to_hide.clear ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]