[gnome-nibbles/arnaudb/modernize-code] Move a variable where it belongs.



commit 7d175c58977d8d193e4d8582aaa95a75c682f16c
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat May 30 00:35:14 2020 +0200

    Move a variable where it belongs.
    
    Not sure at all why it is
    saved in the settings but
    this can be worked later.

 src/nibbles-game.vala   |   7 +---
 src/nibbles-test.vala   |   2 +-
 src/nibbles-view.vala   | 105 +++++++++++++++++++++++++-----------------------
 src/nibbles-window.vala |  15 +++----
 4 files changed, 65 insertions(+), 64 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index f60fdc0..b110480 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -28,8 +28,6 @@ private enum GameStatus
 
 private class NibblesGame : Object
 {
-    internal const int MINIMUM_TILE_SIZE = 7;
-
     internal const int GAMEDELAY = 35;
 
     internal const int MAX_HUMANS = 4;
@@ -53,7 +51,6 @@ private class NibblesGame : Object
     public int speed            { internal get; internal construct set; }
 
     /* Board data */
-    public int tile_size        { internal get; internal construct set; }
     internal int[,] board = new int[WIDTH, HEIGHT];
 
     /* Worms data */
@@ -91,9 +88,9 @@ private class NibblesGame : Object
         boni.bonus_removed.connect ((bonus) => bonus_removed (bonus));
     }
 
-    internal NibblesGame (int tile_size, int start_level, int speed, bool fakes, bool no_random = false)
+    internal NibblesGame (int start_level, int speed, bool fakes, bool no_random = false)
     {
-        Object (tile_size: tile_size, start_level: start_level, current_level: start_level, speed: speed, 
fakes: fakes);
+        Object (start_level: start_level, current_level: start_level, speed: speed, fakes: fakes);
 
         Random.set_seed (no_random ? 42 : (uint32) time_t ());
     }
diff --git a/src/nibbles-test.vala b/src/nibbles-test.vala
index bbe0e72..c3b66f1 100644
--- a/src/nibbles-test.vala
+++ b/src/nibbles-test.vala
@@ -41,7 +41,7 @@ namespace NibblesTest
 
     private static void test_games ()
     {
-        NibblesGame game = new NibblesGame (/* TODO tile size */ 42, /* start level */ 1, /* speed */ 0, /* 
fakes */ false, /* no random */ true);
+        NibblesGame game = new NibblesGame (/* start level */ 1, /* speed */ 0, /* fakes */ false, /* no 
random */ true);
 
         game.numhumans = 0;
         game.numai = 4;
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 447fe78..4a75821 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -110,6 +110,9 @@ private class WarpTexture: GtkClutter.Texture
 
 private class NibblesView : GtkClutter.Embed
 {
+    private const int MINIMUM_TILE_SIZE = 7;
+    public int tile_size { internal get; protected construct set; }
+
     /* Pixmaps */
     private Gdk.Pixbuf wall_pixmaps[11];
     private Gdk.Pixbuf worm_pixmaps[6];
@@ -146,23 +149,23 @@ private class NibblesView : GtkClutter.Embed
         }
     }
 
-    internal NibblesView (NibblesGame game, bool is_muted)
+    internal NibblesView (NibblesGame game, int tile_size, bool is_muted)
     {
-        Object (game: game, is_muted: is_muted);
+        Object (game: game, tile_size: tile_size, is_muted: is_muted);
 
         stage = (Clutter.Stage) get_stage ();
         Clutter.Color stage_color = { 0x00, 0x00, 0x00, 0xff };
         stage.set_background_color (stage_color);
 
-        set_size_request (NibblesGame.MINIMUM_TILE_SIZE * NibblesGame.WIDTH,
-                          NibblesGame.MINIMUM_TILE_SIZE * NibblesGame.HEIGHT);
+        set_size_request (MINIMUM_TILE_SIZE * NibblesGame.WIDTH,
+                          MINIMUM_TILE_SIZE * NibblesGame.HEIGHT);
 
         load_pixmap ();
     }
 
     protected override bool configure_event (Gdk.EventConfigure event)
     {
-        int tile_size, ts_x, ts_y;
+        int new_tile_size, ts_x, ts_y;
 
         /* Compute the new tile size based on the size of the
          * drawing area, rounded down.
@@ -173,22 +176,22 @@ private class NibblesView : GtkClutter.Embed
             ts_x--;
         if (ts_y * NibblesGame.HEIGHT > event.height)
             ts_y--;
-        tile_size = int.min (ts_x, ts_y);
+        new_tile_size = int.min (ts_x, ts_y);
 
-        if (tile_size == 0 || game.tile_size == 0)
+        if (new_tile_size == 0 || tile_size == 0)
             return true;
 
-        if (game.tile_size != tile_size)
+        if (tile_size != new_tile_size)
         {
-            get_stage ().set_size (tile_size * NibblesGame.WIDTH, tile_size * NibblesGame.HEIGHT);
+            get_stage ().set_size (new_tile_size * NibblesGame.WIDTH, new_tile_size * NibblesGame.HEIGHT);
 
-            board_rescale (tile_size);
-            boni_rescale  (tile_size);
-            warps_rescale (tile_size);
+            board_rescale (new_tile_size);
+            boni_rescale  (new_tile_size);
+            warps_rescale (new_tile_size);
             foreach (var worm in game.worms)
-                worm.rescaled (tile_size);
+                worm.rescaled (new_tile_size);
 
-            game.tile_size = tile_size;
+            tile_size = new_tile_size;
         }
 
         return false;
@@ -245,7 +248,7 @@ private class NibblesView : GtkClutter.Embed
         GtkClutter.Texture? tmp;
         for (int i = 0; i < NibblesGame.HEIGHT; i++)
         {
-            y_pos = i * game.tile_size;
+            y_pos = i * tile_size;
             for (int j = 0; j < NibblesGame.WIDTH; j++)
             {
                 tmp = null;
@@ -334,9 +337,9 @@ private class NibblesView : GtkClutter.Embed
 
                 if (tmp != null)
                 {
-                    x_pos = j * game.tile_size;
+                    x_pos = j * tile_size;
 
-                    ((!) tmp).set_size (game.tile_size, game.tile_size);
+                    ((!) tmp).set_size (tile_size, tile_size);
                     ((!) tmp).set_position (x_pos, y_pos);
                     level.add_child ((!) tmp);
                 }
@@ -419,19 +422,19 @@ private class NibblesView : GtkClutter.Embed
         for (int i = 0; i < bonus_files.length; i++)
         {
             boni_pixmaps[i] = load_pixmap_file (bonus_files[i],
-                                                2 * game.tile_size, 2 * game.tile_size);
+                                                2 * tile_size, 2 * tile_size);
         }
 
         for (int i = 0; i < small_files.length; i++)
         {
             wall_pixmaps[i] = load_pixmap_file (small_files[i],
-                                                2 * game.tile_size, 2 * game.tile_size);
+                                                2 * tile_size, 2 * tile_size);
         }
 
         for (int i = 0; i < worm_files.length; i++)
         {
             worm_pixmaps[i] = load_pixmap_file (worm_files[i],
-                                                game.tile_size, game.tile_size);
+                                                tile_size, tile_size);
         }
     }
 
@@ -460,7 +463,7 @@ private class NibblesView : GtkClutter.Embed
         }
     }
 
-    private void board_rescale (int tile_size)
+    private void board_rescale (int new_tile_size)
     {
         int board_width, board_height;
         float x_pos, y_pos;
@@ -468,15 +471,15 @@ private class NibblesView : GtkClutter.Embed
         if (level == null)
             return;
 
-        board_width = NibblesGame.WIDTH * tile_size;
-        board_height = NibblesGame.HEIGHT * tile_size;
+        board_width = NibblesGame.WIDTH * new_tile_size;
+        board_height = NibblesGame.HEIGHT * new_tile_size;
 
         foreach (var actor in level.get_children ())
         {
             actor.get_position (out x_pos, out y_pos);
-            actor.set_position ((x_pos / game.tile_size) * tile_size,
-                                (y_pos / game.tile_size) * tile_size);
-            actor.set_size (tile_size, tile_size);
+            actor.set_position ((x_pos / tile_size) * new_tile_size,
+                                (y_pos / tile_size) * new_tile_size);
+            actor.set_size (new_tile_size, new_tile_size);
         }
 
         if (!name_labels.visible)
@@ -489,13 +492,13 @@ private class NibblesView : GtkClutter.Embed
             var middle = worm.length / 2;
             if (worm.direction == WormDirection.UP || worm.direction == WormDirection.DOWN)
             {
-                actor.set_x (worm.list[middle].x * tile_size - actor.width / 2 + tile_size / 2);
-                actor.set_y (worm.list[middle].y * tile_size - 5 * tile_size);
+                actor.set_x (worm.list[middle].x * new_tile_size - actor.width / 2 + new_tile_size / 2);
+                actor.set_y (worm.list[middle].y * new_tile_size - 5 * new_tile_size);
             }
             else if (worm.direction == WormDirection.LEFT || worm.direction == WormDirection.RIGHT)
             {
-                actor.set_x (worm.list[middle].x * tile_size - actor.width / 2 + tile_size / 2);
-                actor.set_y (worm.head.y * tile_size - 3 * tile_size);
+                actor.set_x (worm.list[middle].x * new_tile_size - actor.width / 2 + new_tile_size / 2);
+                actor.set_y (worm.head.y * new_tile_size - 3 * new_tile_size);
             }
         }
     }
@@ -533,13 +536,13 @@ private class NibblesView : GtkClutter.Embed
             var middle = worm.length / 2;
             if (worm.direction == WormDirection.UP || worm.direction == WormDirection.DOWN)
             {
-                label.set_x (worm.list[middle].x * game.tile_size - label.width / 2 + game.tile_size / 2);
-                label.set_y (worm.list[middle].y * game.tile_size - 5 * game.tile_size);
+                label.set_x (worm.list[middle].x * tile_size - label.width / 2 + tile_size / 2);
+                label.set_y (worm.list[middle].y * tile_size - 5 * tile_size);
             }
             else if (worm.direction == WormDirection.LEFT || worm.direction == WormDirection.RIGHT)
             {
-                label.set_x (worm.list[middle].x * game.tile_size - label.width / 2 + game.tile_size / 2);
-                label.set_y (worm.head.y * game.tile_size - 3 * game.tile_size);
+                label.set_x (worm.list[middle].x * tile_size - label.width / 2 + tile_size / 2);
+                label.set_y (worm.head.y * tile_size - 3 * tile_size);
             }
             name_labels.add (label);
         }
@@ -566,8 +569,8 @@ private class NibblesView : GtkClutter.Embed
         {
             error ("Nibbles failed to set texture: %s", e.message);
         }
-        actor.set_size (game.tile_size, game.tile_size);
-        actor.set_position (worm.list.first ().x * game.tile_size, worm.list.first ().y * game.tile_size);
+        actor.set_size (tile_size, tile_size);
+        actor.set_position (worm.list.first ().x * tile_size, worm.list.first ().y * tile_size);
 
         var actors = worm_actors.@get (worm);
         actors.add_child (actor);
@@ -605,7 +608,7 @@ private class NibblesView : GtkClutter.Embed
         worm_added_cb (worm);
     }
 
-    private void worm_rescaled_cb (Worm worm, int tile_size)
+    private void worm_rescaled_cb (Worm worm, int new_tile_size)
     {
         float x_pos, y_pos;
         var actors = worm_actors.@get (worm);
@@ -615,9 +618,9 @@ private class NibblesView : GtkClutter.Embed
         foreach (var actor in actors.get_children ())
         {
             actor.get_position (out x_pos, out y_pos);
-            actor.set_position ((x_pos / game.tile_size) * tile_size,
-                                (y_pos / game.tile_size) * tile_size);
-            actor.set_size (tile_size, tile_size);
+            actor.set_position ((x_pos / tile_size) * new_tile_size,
+                                (y_pos / tile_size) * new_tile_size);
+            actor.set_size (new_tile_size, new_tile_size);
         }
     }
 
@@ -646,7 +649,7 @@ private class NibblesView : GtkClutter.Embed
             actor.get_position (out x, out y);
 
             texture.set_position (x, y);
-            texture.set_size (game.tile_size, game.tile_size);
+            texture.set_size (tile_size, tile_size);
             group.add_child (texture);
         }
 
@@ -691,7 +694,7 @@ private class NibblesView : GtkClutter.Embed
             worm_actors.remove_child (worm_actors.first_child);
 
             texture.set_position (x, y);
-            texture.set_size (game.tile_size, game.tile_size);
+            texture.set_size (tile_size, tile_size);
             group.add_child (texture);
         }
         level.add_child (group);
@@ -710,7 +713,7 @@ private class NibblesView : GtkClutter.Embed
         var count = 0;
         foreach (var actor in actors.get_children ())
         {
-            actor.set_position (worm.list[count].x * game.tile_size, worm.list[count].y * game.tile_size);
+            actor.set_position (worm.list[count].x * tile_size, worm.list[count].y * tile_size);
             count++;
         }
     }
@@ -735,8 +738,8 @@ private class NibblesView : GtkClutter.Embed
             error ("Nibbles failed to set texture: %s", e.message);
         }
 
-        actor.set_size (game.tile_size, game.tile_size);
-        actor.set_position (bonus.x * game.tile_size, bonus.y * game.tile_size);
+        actor.set_size (tile_size, tile_size);
+        actor.set_position (bonus.x * tile_size, bonus.y * tile_size);
 
         level.add_child (actor);
         if (bonus.bonus_type != BonusType.REGULAR)
@@ -787,12 +790,12 @@ private class NibblesView : GtkClutter.Embed
         }
     }
 
-    private void boni_rescale (int tile_size)
+    private void boni_rescale (int new_tile_size)
     {
         foreach (var bonus in bonus_actors.keys)
         {
             var actor = bonus_actors.@get (bonus);
-            actor.set_size (tile_size, tile_size);
+            actor.set_size (new_tile_size, new_tile_size);
         }
     }
 
@@ -816,18 +819,18 @@ private class NibblesView : GtkClutter.Embed
             error ("Nibbles failed to set texture: %s", e.message);
         }
 
-        actor.set_size (game.tile_size, game.tile_size);
-        actor.set_position (x * game.tile_size, y * game.tile_size);
+        actor.set_size (tile_size, tile_size);
+        actor.set_position (x * tile_size, y * tile_size);
 
         level.add_child (actor);
 
         warp_actors.add (actor);
     }
 
-    private void warps_rescale (int tile_size)
+    private void warps_rescale (int new_tile_size)
     {
         foreach (var actor in warp_actors)
-            actor.set_size (tile_size, tile_size);
+            actor.set_size (new_tile_size, new_tile_size);
     }
 
     /*\
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index 337c6ee..75a60bc 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -157,8 +157,7 @@ private class NibblesWindow : ApplicationWindow
         number_of_ai_buttons.add (ai5);
 
         /* Create game */
-        game = new NibblesGame (settings.get_int ("tile-size"),
-                                settings.get_int ("start-level"),
+        game = new NibblesGame (settings.get_int ("start-level"),
                                 settings.get_int ("speed"),
                                 settings.get_boolean ("fakes"));
         game.log_score.connect (log_score_cb);
@@ -171,14 +170,16 @@ private class NibblesWindow : ApplicationWindow
         });
 
         /* Create view */
-        view = new NibblesView (game, !settings.get_boolean ("sound"));
+        view = new NibblesView (game,
+                                settings.get_int ("tile-size"),
+                                !settings.get_boolean ("sound"));
         view.show ();
 
         frame = new Games.GridFrame (NibblesGame.WIDTH, NibblesGame.HEIGHT);
         game_box.pack_start (frame);
 
         /* Create scoreboard */
-        scoreboard_life = view.load_pixmap_file ("scoreboard-life.svg", 2 * game.tile_size, 2 * 
game.tile_size);
+        scoreboard_life = view.load_pixmap_file ("scoreboard-life.svg", 2 * view.tile_size, 2 * 
view.tile_size);
 
         frame.add (view);
         frame.show ();
@@ -188,8 +189,8 @@ private class NibblesWindow : ApplicationWindow
         game.numai = settings.get_int ("ai");
 
         /* Controls screen */
-        arrow_pixbuf = view.load_pixmap_file ("arrow.svg", 5 * game.tile_size, 5 * game.tile_size);
-        arrow_key_pixbuf = view.load_pixmap_file ("arrow-key.svg", 5 * game.tile_size, 5 * game.tile_size);
+        arrow_pixbuf = view.load_pixmap_file ("arrow.svg", 5 * view.tile_size, 5 * view.tile_size);
+        arrow_key_pixbuf = view.load_pixmap_file ("arrow-key.svg", 5 * view.tile_size, 5 * view.tile_size);
 
         /* Check whether to display the first run screen */
         var first_run = settings.get_boolean ("first-run");
@@ -211,7 +212,7 @@ private class NibblesWindow : ApplicationWindow
         settings.set_boolean ("window-is-maximized", window_is_maximized);
 
         // game properties
-        settings.set_int ("tile-size", game.tile_size);
+        settings.set_int ("tile-size", view.tile_size);     // TODO why?!
         settings.set_int ("start-level", game.start_level);
         settings.set_int ("speed", game.speed);
         settings.set_boolean ("fakes", game.fakes);


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