[gnome-robots] decouple Game class from properties



commit e03dca8c68002511cc0ec63f9e90ad05c3f2d719
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Sep 27 13:53:39 2020 +0200

    decouple Game class from properties

 src/game-area.vala | 40 ++++++++++++++++++++++++++++++++++++--
 src/game.vala      | 56 ++++++++++++++++--------------------------------------
 2 files changed, 54 insertions(+), 42 deletions(-)
---
diff --git a/src/game-area.vala b/src/game-area.vala
index f3bbe96..eb7c61e 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -20,6 +20,7 @@
 using Gtk;
 using Gdk;
 using Cairo;
+using Games;
 
 public class GameArea : DrawingArea {
 
@@ -302,14 +303,19 @@ public class GameArea : DrawingArea {
     }
 
     public void player_command (PlayerCommand cmd) {
-        if (game.player_command (cmd)) {
+        var safety =
+            !properties_safe_moves () ? Game.MoveSafety.UNSAFE :
+            properties_super_safe_moves () ? Game.MoveSafety.SUPER_SAFE :
+            Game.MoveSafety.SAFE;
+
+        if (game.player_command (cmd, safety)) {
             queue_draw ();
         } else {
             play_sound (Sound.BAD);
         }
     }
 
-    private void on_game_event (Game.Event event) {
+    private void on_game_event (Game.Event event, int param) {
         switch (event) {
         case Game.Event.TELEPORTED:
             play_sound (Sound.TELEPORT);
@@ -323,6 +329,9 @@ public class GameArea : DrawingArea {
         case Game.Event.DEATH:
             play_sound (Sound.DIE);
             break;
+        case Game.Event.SCORED:
+            log_score (param);
+            break;
         case Game.Event.VICTORY:
             message_box (_("Congratulations, You Have Defeated the Robots!! \nBut Can You do it Again?"));
             play_sound (Sound.VICTORY);
@@ -337,6 +346,33 @@ public class GameArea : DrawingArea {
         }
     }
 
+    /**
+     * Enters a score in the high-score table
+     **/
+    private void log_score (int sc) {
+        if (sc <= 0) {
+            return;
+        }
+
+        string key;
+        if (properties_super_safe_moves ()) {
+            key = game.config.description + "-super-safe";
+        } else if (properties_safe_moves ()) {
+            key = game.config.description + "-safe";
+        } else {
+            key = game.config.description;
+        }
+
+        string name = category_name_from_key (key);
+        var category = new Scores.Category (key, name);
+        highscores.add_score.begin (sc, category, null, (ctx, res) => {
+            try {
+                highscores.add_score.end (res);
+            } catch (Error error) {
+                warning ("Failed to add score: %s", error.message);
+            }
+        });
+    }
 
     private void play_sound (Sound sound) {
         if (properties_sound ()) {
diff --git a/src/game.vala b/src/game.vala
index 5f36fee..d3af8ee 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -17,8 +17,6 @@
  * For more details see the file COPYING.
  */
 
-using Games;
-
 public Game game = null;
 
 public enum PlayerCommand {
@@ -149,12 +147,13 @@ public class Game {
         SPLAT,
         LEVEL_COMPLETE,
         DEATH,
+        SCORED,
         VICTORY,
         NO_TELEPORT_LOCATIONS,
         NO_SAFE_TELEPORT_LOCATIONS,
     }
 
-    public signal void game_event (Event event);
+    public signal void game_event (Event event, int param = 0);
 
     /**
      * Displays the high-score table
@@ -163,34 +162,6 @@ public class Game {
         highscores.run_dialog ();
     }
 
-    /**
-     * Enters a score in the high-score table
-     **/
-    private void log_score (int sc) {
-        if (sc <= 0) {
-            return;
-        }
-
-        string key;
-        if (properties_super_safe_moves ()) {
-            key = config.description + "-super-safe";
-        } else if (properties_safe_moves ()) {
-            key = config.description + "-safe";
-        } else {
-            key = config.description;
-        }
-
-        string name = category_name_from_key (key);
-        var category = new Scores.Category (key, name);
-        highscores.add_score.begin (sc, category, null, (ctx, res) => {
-            try {
-                highscores.add_score.end (res);
-            } catch (Error error) {
-                warning ("Failed to add score: %s", error.message);
-            }
-        });
-    }
-
     /**
      * Ends the current game.
      **/
@@ -371,7 +342,7 @@ public class Game {
             ++endlev_counter;
             if (endlev_counter >= DEAD_DELAY) {
                 if (score > 0) {
-                    log_score (score);
+                    game_event (Event.SCORED, score);
                 }
                 start_new_game ();
             }
@@ -387,9 +358,6 @@ public class Game {
         kills = 0;
         score_step = 0;
 
-        if (state == State.PLAYING)
-            log_score (score);
-
         safe_teleports = config.initial_safe_teleports;
 
         splat = null;
@@ -619,6 +587,12 @@ public class Game {
         };
     }
 
+    public enum MoveSafety {
+        UNSAFE,
+        SAFE,
+        SUPER_SAFE,
+    }
+
     /**
      * player_move
      * @dx: x direction
@@ -630,16 +604,16 @@ public class Game {
      * Returns:
      * TRUE if the player can move, FALSE otherwise
      **/
-    private bool player_move (int dx, int dy) {
+    private bool player_move (int dx, int dy, MoveSafety safety) {
         var change = try_player_move (dx, dy);
 
         if (change == null) {
             return false;
         }
 
-        if (properties_safe_moves ()) {
+        if (safety != MoveSafety.UNSAFE) {
             if (!check_safe (change)) {
-                if (properties_super_safe_moves () || safe_move_available ()) {
+                if (safety == MoveSafety.SUPER_SAFE || safe_move_available ()) {
                     return false;
                 }
             }
@@ -724,7 +698,9 @@ public class Game {
     /**
      * handles player's commands
      **/
-    public bool player_command (PlayerCommand cmd) {
+    public bool player_command (PlayerCommand cmd,
+                                MoveSafety safety = MoveSafety.UNSAFE
+    ) {
         if (state != State.PLAYING)
             return false;
 
@@ -740,7 +716,7 @@ public class Game {
         case PlayerCommand.SE:
             int dx, dy;
             assert (cmd.to_direction (out dx, out dy));
-            if (player_move (dx, dy)) {
+            if (player_move (dx, dy, safety)) {
                 move_robots ();
                 return true;
             } else {


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