[gnome-nibbles/arnaudb/rework-warps: 28/33] Rework warps code.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/arnaudb/rework-warps: 28/33] Rework warps code.
- Date: Tue, 23 Jun 2020 11:19:29 +0000 (UTC)
commit 485a3d37f351927f27be88533779251e3380c0d4
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Sun Jun 21 23:19:20 2020 +0200
Rework warps code.
src/nibbles-game.vala | 22 +++++---
src/warp.vala | 146 +++++++++++++++++++++++++-------------------------
src/worm.vala | 4 +-
3 files changed, 91 insertions(+), 81 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index c00d902..4830c1d 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -81,7 +81,6 @@ private class NibblesGame : Object
construct
{
- warp_manager.warp_added.connect ((warp) => warp_added (warp.x, warp.y));
boni.bonus_removed.connect ((bonus) => bonus_removed (bonus));
}
@@ -98,7 +97,7 @@ private class NibblesGame : Object
return false;
boni.reset (numworms);
- warp_manager.warps.clear ();
+ warp_manager.clear_warps ();
string tmpboard;
int count = 0;
@@ -202,7 +201,14 @@ private class NibblesGame : Object
case 'Y':
case 'Z':
board[j, i] = (int) char_value;
- warp_manager.add_warp (board, j - 1, i - 1, -(board[j, i]), 0);
+ warp_manager.add_warp_source (board[j, i], j - 1, i - 1);
+
+ board[j - 1, i - 1] = NibblesGame.WARPCHAR;
+ board[j , i - 1] = NibblesGame.WARPCHAR;
+ board[j - 1, i ] = NibblesGame.WARPCHAR;
+ board[j , i ] = NibblesGame.WARPCHAR;
+
+ warp_added (j - 1, i - 1);
break;
case 'r':
@@ -214,7 +220,8 @@ private class NibblesGame : Object
case 'x':
case 'y':
case 'z':
- warp_manager.add_warp (board, -(((int) char_value) - 'a' + 'A'), 0, j, i);
+ // do not use the up() method: it depends on the locale, and that could have some
weird results ("i".up() is either I or İ, for example)
+ warp_manager.add_warp_target ((int) char_value - (int) 'a' + (int) 'A', j, i);
board[j, i] = (int) NibblesGame.EMPTYCHAR;
break;
@@ -602,11 +609,12 @@ private class NibblesGame : Object
private void warp_found_cb (Worm worm)
{
- var warp = warp_manager.get_warp (worm.head.x, worm.head.y);
- if (warp == null)
+ int target_x;
+ int target_y;
+ if (!warp_manager.get_warp_target (worm.head.x, worm.head.y, out target_x, out target_y))
return;
- worm.warp (warp);
+ worm.warp (target_x, target_y);
}
internal GameStatus? get_game_status ()
diff --git a/src/warp.vala b/src/warp.vala
index 57d8d9b..4ba6850 100644
--- a/src/warp.vala
+++ b/src/warp.vala
@@ -1,6 +1,9 @@
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* Gnome Nibbles: Gnome Worm Game
+ *
+ * Rewrite of the original by Sean MacIsaac, Ian Peters, Guillaume Béland
* Copyright (C) 2015 Iulian-Gabriel Radu <iulian radu67 gmail com>
+ * Copyright (C) 2020 Arnaud Bonatti <arnaud bonatti gmail com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -16,105 +19,104 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-// This is a fairly literal translation of the GPLv2+ original by
-// Sean MacIsaac, Ian Peters, Guillaume Béland.
-
-private class Warp : Object
+private class WarpManager: Object
{
- public int x { internal get; protected construct set; }
- public int y { internal get; protected construct set; }
+ private class Warp : Object
+ {
+ public int id { internal get; protected construct; }
- public int wx { internal get; protected construct set; }
- public int wy { internal get; protected construct set; }
+ public int source_x { internal get; protected construct set; }
+ public int source_y { internal get; protected construct set; }
- internal Warp (int x, int y, int wx, int wy)
- {
- Object (x: x, y: y, wx: wx, wy: wy);
- }
+ public int target_x { internal get; protected construct set; }
+ public int target_y { internal get; protected construct set; }
- internal void set_x_and_y (int x, int y)
- {
- this.x = x;
- this.y = y;
- }
+ internal Warp.from_source (int id, int source_x, int source_y)
+ {
+ Object (id : id,
+ source_x: source_x,
+ source_y: source_y);
+ }
- internal void set_wx_and_wy (int wx, int wy)
- {
- this.wx = wx;
- this.wy = wy;
+ internal Warp.from_target (int id, int target_x, int target_y)
+ {
+ Object (id : id,
+ target_x: target_x,
+ target_y: target_y);
+ }
+
+ internal void set_source (int x, int y)
+ {
+ source_x = x;
+ source_y = y;
+ }
+
+ internal void set_target (int x, int y)
+ {
+ target_x = x;
+ target_y = y;
+ }
}
-}
-private class WarpManager: Object
-{
private const int MAX_WARPS = 200;
- internal Gee.LinkedList<Warp> warps = new Gee.LinkedList<Warp> ();
-
- internal signal void warp_added (Warp warp);
+ private Gee.LinkedList<Warp> warps = new Gee.LinkedList<Warp> ();
- internal void add_warp (int[,] board, int x, int y, int wx, int wy)
+ internal void add_warp_source (int id, int x, int y)
{
- bool add = true;
-
- if (x < 0)
+ foreach (var warp in warps)
{
- foreach (var warp in warps)
+ if (warp.id == id)
{
- if (warp.wx == x)
- {
- warp.set_wx_and_wy (wx, wy);
- return;
- }
+ warp.set_source (x, y);
+ return;
}
+ }
- if (warps.size == MAX_WARPS)
- return;
+ if (warps.size == MAX_WARPS)
+ return;
- warps.add (new Warp (x, y, wx, wy));
- }
- else
+ warps.add (new Warp.from_source (id, x, y));
+ }
+
+ internal void add_warp_target (int id, int x, int y)
+ {
+ foreach (var warp in warps)
{
- foreach (var warp in warps)
+ if (warp.id == id)
{
- if (warp.x == wx)
- {
- warp.set_x_and_y (x, y);
- add = false;
-
- warp_added (warp);
- }
+ warp.set_target (x, y);
+ return;
}
+ }
- if (add)
- {
- if (warps.size == MAX_WARPS)
- return;
-
- var warp = new Warp (x, y, wx, wy);
- warps.add (warp);
-
- warp_added (warp);
- }
+ if (warps.size == MAX_WARPS)
+ return;
- board[x , y ] = NibblesGame.WARPCHAR;
- board[x + 1, y ] = NibblesGame.WARPCHAR;
- board[x , y + 1] = NibblesGame.WARPCHAR;
- board[x + 1, y + 1] = NibblesGame.WARPCHAR;
- }
+ warps.add (new Warp.from_target (id, x, y));
}
- internal Warp? get_warp (int x, int y)
+ internal bool get_warp_target (int x, int y, out int target_x, out int target_y)
{
foreach (var warp in warps)
{
- if ((x == warp.x && y == warp.y )
- || (x == warp.x + 1 && y == warp.y )
- || (x == warp.x && y == warp.y + 1)
- || (x == warp.x + 1 && y == warp.y + 1))
- return warp;
+ if ((x == warp.source_x && y == warp.source_y )
+ || (x == warp.source_x + 1 && y == warp.source_y )
+ || (x == warp.source_x && y == warp.source_y + 1)
+ || (x == warp.source_x + 1 && y == warp.source_y + 1))
+ {
+ target_x = warp.target_x;
+ target_y = warp.target_y;
+ return true;
+ }
}
+ target_x = 0; // garbage
+ target_y = 0; // garbage
+ return false;
+ }
- return null;
+ internal void clear_warps ()
+ {
+ warps.clear ();
}
}
diff --git a/src/worm.vala b/src/worm.vala
index d49d145..c0091a9 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -220,9 +220,9 @@ private class Worm : Object
direction = (list[0].y > list[1].y) ? WormDirection.DOWN : WormDirection.UP;
}
- internal inline void warp (Warp warp)
+ internal inline void warp (int x, int y)
{
- head = Position () { x = warp.wx, y = warp.wy };
+ head = Position () { x = x, y = y };
}
internal bool can_move_to (int[,] board, int numworms)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]