[gnome-robots] move bubbles to GameArea class



commit c270946fb5b7742533d9b2b58b4e428d4fccf57c
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Sep 13 14:44:17 2020 +0200

    move bubbles to GameArea class

 src/arena.vala     |   5 +++
 src/game-area.vala |  21 +++++++++-
 src/game.vala      | 119 +++++++++++++++++++++++++++++------------------------
 src/graphics.vala  | 110 -------------------------------------------------
 4 files changed, 91 insertions(+), 164 deletions(-)
---
diff --git a/src/arena.vala b/src/arena.vala
index 3f978c3..79301bf 100644
--- a/src/arena.vala
+++ b/src/arena.vala
@@ -29,6 +29,11 @@ public delegate ObjectType ArenaMapper (ObjectType obj);
 
 public class Arena {
 
+    public struct Coords {
+        public int x;
+        public int y;
+    }
+
     private int _width;
     private int _height;
     private ObjectType[] arena;
diff --git a/src/game-area.vala b/src/game-area.vala
index c0eb71a..2639847 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -131,7 +131,26 @@ public class GameArea : DrawingArea {
             }
         }
 
-        draw_bubble (cr);
+        if (game.splat != null) {
+            splat_bubble.draw (cr,
+                               game.splat.x * tile_width + 8,
+                               game.splat.y * tile_height + 8);
+        }
+
+        switch (game.get_state ()) {
+        case Game.State.DEAD:
+            aieee_bubble.draw (cr,
+                               game.player.x * tile_width + 8,
+                               game.player.y * tile_height + 4);
+            break;
+        case Game.State.COMPLETE:
+            yahoo_bubble.draw (cr,
+                               game.player.x * tile_width + 8,
+                               game.player.y * tile_height + 4);
+            break;
+        default:
+            break;
+        }
 
         return true;
     }
diff --git a/src/game.vala b/src/game.vala
index 709f70d..b5a5105 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -57,6 +57,8 @@ public class Game {
     public State state = State.PLAYING;
     public Arena arena;
     public GameConfig config { get; set; }
+    public Arena.Coords player { get; private set; }
+    public Arena.Coords? splat { get; private set; }
 
     int num_robots1 = 0;
     int num_robots2 = 0;
@@ -66,8 +68,6 @@ public class Game {
     int kills = 0;
     int score_step = 0;
     int safe_teleports = 0;
-    int player_xpos = 0;
-    int player_ypos = 0;
     int push_xpos = -1;
     int push_ypos = -1;
     uint game_timer_id = -1;
@@ -121,10 +121,10 @@ public class Game {
     void kill_player () {
         state = State.DEAD;
         play_sound (Sound.DIE);
-        arena[player_xpos, player_ypos] = ObjectType.PLAYER;
+        arena[player.x, player.y] = ObjectType.PLAYER;
         endlev_counter = 0;
-        add_aieee_bubble (player_xpos, player_ypos);
         set_move_action_sensitivity (false);
+        game_area.queue_draw ();
     }
 
     /**
@@ -227,9 +227,11 @@ public class Game {
     void generate_level () {
         clear_arena ();
 
-        player_xpos = arena.width / 2;
-        player_ypos = arena.height / 2;
-        arena.@set(player_xpos, player_ypos, ObjectType.PLAYER);
+        player = Arena.Coords () {
+            x = arena.width / 2,
+            y = arena.height / 2
+        };
+        arena.@set(player.x, player.y, ObjectType.PLAYER);
 
         num_robots1 = config.initial_type1 + config.increment_type1 * current_level;
 
@@ -307,13 +309,13 @@ public class Game {
                     push_xpos == i && push_ypos == j
                 ) {
                     if (arena[i, j] == ObjectType.ROBOT1) {
-                        add_splat_bubble (i, j);
+                        splat = Arena.Coords () { x = i, y = j };
                         play_sound (Sound.SPLAT);
                         push_xpos = push_ypos = -1;
                         score += config.score_type1_splatted;
                     }
                     if (arena[i, j] == ObjectType.ROBOT2) {
-                        add_splat_bubble (i, j);
+                        splat = Arena.Coords () { x = i, y = j };
                         play_sound (Sound.SPLAT);
                         push_xpos = push_ypos = -1;
                         score += config.score_type2_splatted;
@@ -329,7 +331,7 @@ public class Game {
             }
         }
 
-        if (arena[player_xpos, player_ypos] != ObjectType.PLAYER) {
+        if (arena[player.x, player.y] != ObjectType.PLAYER) {
             kill_player ();
         } else {
             /* This is in the else statement to catch the case where the last
@@ -339,7 +341,6 @@ public class Game {
                 state = State.COMPLETE;
                 play_sound (Sound.YAHOO);
                 endlev_counter = 0;
-                add_yahoo_bubble (player_xpos, player_ypos);
                 set_move_action_sensitivity (false);
             }
         }
@@ -369,18 +370,20 @@ public class Game {
                 state = State.WAITING;
             }
         } else if (state == State.WAITING) {
-            remove_splat_bubble ();
+            splat = null;
+            game_area.queue_draw ();
             move_robots ();
         } else if (state == State.COMPLETE) {
             ++endlev_counter;
             if (endlev_counter >= CHANGE_DELAY) {
                 ++current_level;
-                remove_bubble ();
                 clear_game_area ();
                 generate_level ();
                 state = State.PLAYING;
                 set_move_action_sensitivity (true);
                 update_game_status (score, current_level + 1, safe_teleports);
+                splat = null;
+                game_area.queue_draw ();
             }
         } else if (state == State.DEAD) {
             ++endlev_counter;
@@ -446,7 +449,8 @@ public class Game {
 
         safe_teleports = config.initial_safe_teleports;
 
-        remove_bubble ();
+        splat = null;
+        game_area.queue_draw ();
         generate_level ();
         clear_game_area ();
 
@@ -478,13 +482,13 @@ public class Game {
           if ((arena.@get (i, j) == ObjectType.ROBOT1) || (arena.@get (i, j) == ObjectType.ROBOT2)) {
             nx = i;
             ny = j;
-            if (player_xpos < nx)
+            if (player.x < nx)
               nx -= 1;
-            if (player_xpos > nx)
+            if (player.x > nx)
               nx += 1;
-            if (player_ypos < ny)
+            if (player.y < ny)
               ny -= 1;
-            if (player_ypos > ny)
+            if (player.y > ny)
               ny += 1;
 
             if (temp_arena.@get (nx, ny) == ObjectType.HEAP) {
@@ -526,13 +530,13 @@ public class Game {
           if (arena.@get (i, j) == ObjectType.ROBOT2) {
                 nx = i;
                 ny = j;
-                if (player_xpos < nx)
+                if (player.x < nx)
                   nx -= 1;
-                if (player_xpos > nx)
+                if (player.x > nx)
                   nx += 1;
-                if (player_ypos < ny)
+                if (player.y < ny)
                   ny -= 1;
-                if (player_ypos > ny)
+                if (player.y > ny)
                     ny += 1;
 
             if (temp_arena.@get (nx, ny) == ObjectType.HEAP) {
@@ -719,8 +723,8 @@ public class Game {
      * TRUE if the player can move, FALSE otherwise
      **/
     bool try_player_move (int dx, int dy) {
-        int nx = player_xpos + dx;
-        int ny = player_ypos + dy;
+        int nx = player.x + dx;
+        int ny = player.y + dy;
 
         if ((nx < 0) || (nx >= arena.width) || (ny < 0) || (ny >= arena.height)) {
             return false;
@@ -753,47 +757,47 @@ public class Game {
      **/
     bool safe_move_available () {
         if (try_player_move (-1, -1)) {
-            if (check_safe (player_xpos - 1, player_ypos - 1)) {
+            if (check_safe (player.x - 1, player.y - 1)) {
                 return true;
             }
         }
         if (try_player_move (0, -1)) {
-            if (check_safe (player_xpos, player_ypos - 1)) {
+            if (check_safe (player.x, player.y - 1)) {
                 return true;
             }
         }
         if (try_player_move (1, -1)) {
-            if (check_safe (player_xpos + 1, player_ypos - 1)) {
+            if (check_safe (player.x + 1, player.y - 1)) {
                 return true;
             }
         }
         if (try_player_move (-1, 0)) {
-            if (check_safe (player_xpos - 1, player_ypos)) {
+            if (check_safe (player.x - 1, player.y)) {
                 return true;
             }
         }
         if (try_player_move (0, 0)) {
-            if (check_safe (player_xpos, player_ypos)) {
+            if (check_safe (player.x, player.y)) {
                 return true;
             }
         }
         if (try_player_move (1, 0)) {
-            if (check_safe (player_xpos + 1, player_ypos)) {
+            if (check_safe (player.x + 1, player.y)) {
                 return true;
             }
         }
         if (try_player_move (-1, 1)) {
-            if (check_safe (player_xpos - 1, player_ypos + 1)) {
+            if (check_safe (player.x - 1, player.y + 1)) {
                 return true;
             }
         }
         if (try_player_move (0, 1)) {
-            if (check_safe (player_xpos, player_ypos + 1)) {
+            if (check_safe (player.x, player.y + 1)) {
                 return true;
             }
         }
         if (try_player_move (1, 1)) {
-            if (check_safe (player_xpos + 1, player_ypos + 1)) {
+            if (check_safe (player.x + 1, player.y + 1)) {
                 return true;
             }
         }
@@ -836,8 +840,8 @@ public class Game {
      **/
     public bool player_move (int dx, int dy) {
 
-        int nx = player_xpos + dx;
-        int ny = player_ypos + dy;
+        int nx = player.x + dx;
+        int ny = player.y + dy;
 
         if (properties_safe_moves ()) {
             if (!try_player_move (dx, dy)) {
@@ -858,14 +862,17 @@ public class Game {
             }
         }
 
-        player_xpos = nx;
-        player_ypos = ny;
+        player = Arena.Coords () {
+            x = nx,
+            y = ny
+        };
 
-        if (temp_arena.@get (player_xpos, player_ypos) == ObjectType.NONE) {
-            temp_arena.@set (player_xpos, player_ypos, ObjectType.PLAYER);
+        if (temp_arena.@get (player.x, player.y) == ObjectType.NONE) {
+            temp_arena.@set (player.x, player.y, ObjectType.PLAYER);
         }
 
-        remove_splat_bubble ();
+        splat = null;
+        game_area.queue_draw ();
 
         update_arena ();
 
@@ -894,12 +901,15 @@ public class Game {
         int xp;
         int yp;
         if (random_position ((x, y) => temp_arena.@get(x, y) == ObjectType.NONE, out xp, out yp)) {
-            player_xpos = xp;
-            player_ypos = yp;
-            temp_arena.@set (player_xpos, player_ypos, ObjectType.PLAYER);
+            player = Arena.Coords () {
+                x = xp,
+                y = yp
+            };
+            temp_arena.@set (player.x, player.y, ObjectType.PLAYER);
 
             update_arena ();
-            remove_splat_bubble ();
+            splat = null;
+            game_area.queue_draw ();
             play_sound (Sound.TELEPORT);
 
             return true;
@@ -941,15 +951,18 @@ public class Game {
         int xp;
         int yp;
         if (random_position ((x, y) => temp_arena.@get(x, y) == ObjectType.NONE && check_safe (x, y), out 
xp, out yp)) {
-            player_xpos = xp;
-            player_ypos = yp;
-            temp_arena.@set (player_xpos, player_ypos, ObjectType.PLAYER);
+            player = Arena.Coords () {
+                x = xp,
+                y = yp
+            };
+            temp_arena.@set (player.x, player.y, ObjectType.PLAYER);
 
             safe_teleports -= 1;
             update_game_status (score, current_level, safe_teleports);
 
             update_arena ();
-            remove_splat_bubble ();
+            splat = null;
+            game_area.queue_draw ();
             play_sound (Sound.TELEPORT);
 
             return true;
@@ -1066,15 +1079,15 @@ public class Game {
         int y = (iy / tile_height).clamp (0, arena.height);
 
         /* If we click on our man then we assume we hold. */
-        if ((x == player_xpos) && (y == player_ypos)) {
+        if ((x == player.x) && (y == player.y)) {
             odx = 0;
             ody = 0;
             return;
         }
 
         /* If the square clicked on is a valid move, go there. */
-        int idx = x - player_xpos;
-        int idy = y - player_ypos;
+        int idx = x - player.x;
+        int idy = y - player.y;
         if (idx.abs () < 2 && idy.abs () < 2) {
             odx = idx;
             ody = idy;
@@ -1082,8 +1095,8 @@ public class Game {
         }
 
         /* Otherwise go in the general direction of the mouse click. */
-        double dx = ix - (player_xpos + 0.5) * tile_width;
-        double dy = iy - (player_ypos + 0.5) * tile_height;
+        double dx = ix - (player.x + 0.5) * tile_width;
+        double dy = iy - (player.y + 0.5) * tile_height;
 
         double angle = Math.atan2 (dy, dx);
 
diff --git a/src/graphics.vala b/src/graphics.vala
index 072a15e..4a171a2 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -27,18 +27,6 @@ using Cairo;
 public const int GAME_WIDTH = 45;
 public const int GAME_HEIGHT = 30;
 
-public enum BubbleType {
-    NONE = 0,
-    YAHOO,
-    AIEEE,
-    SPLAT,
-}
-
-public const int BUBBLE_WIDTH = 86;
-public const int BUBBLE_HEIGHT = 34;
-public const int BUBBLE_XOFFSET = 8;
-public const int BUBBLE_YOFFSET = 4;
-
 public int tile_width = 0;
 public int tile_height = 0;
 
@@ -46,10 +34,6 @@ Bubble aieee_bubble = null;
 Bubble yahoo_bubble = null;
 Bubble splat_bubble = null;
 
-int bubble_xpos = 0;
-int bubble_ypos = 0;
-BubbleType bubble_type = BubbleType.NONE;
-
 public void load_game_graphics () throws Error {
     yahoo_bubble = new Bubble.from_data_file ("yahoo.png");
     aieee_bubble = new Bubble.from_data_file ("aieee.png");
@@ -92,97 +76,3 @@ public void clear_game_area () {
     game_area.queue_draw ();
 }
 
-/**
- * Draws a bubble if there is one
- **/
-public void draw_bubble (Context cr) {
-    if (bubble_type == BubbleType.NONE)
-        return;
-
-    Bubble bubble;
-    if (bubble_type == BubbleType.YAHOO) {
-        bubble = yahoo_bubble;
-    } else if (bubble_type == BubbleType.AIEEE) {
-        bubble = aieee_bubble;
-    } else {
-        bubble = splat_bubble;
-    }
-
-    bubble.draw (cr, bubble_xpos, bubble_ypos);
-}
-
-/**
- * remove_bubble
- *
- * Description:
- * removes all types of bubble
- **/
-public void remove_bubble () {
-    if (bubble_type == BubbleType.NONE)
-        return;
-
-    bubble_type = BubbleType.NONE;
-    game_area.queue_draw ();
-}
-
-/**
- * removes a splat bubble if there is one
- **/
-public void remove_splat_bubble () {
-    if (bubble_type != BubbleType.SPLAT)
-        return;
-
-    bubble_type = BubbleType.NONE;
-    game_area.queue_draw ();
-}
-
-
-/**
- * add_yahoo_bubble
- * @x: x position
- * @y: y position
- *
- * Description:
- * adds and "Yahoo" bubble at @x,@y
- **/
-public void add_yahoo_bubble (int x, int y) {
-    bubble_type = BubbleType.YAHOO;
-    bubble_xpos = x * tile_width + BUBBLE_XOFFSET;
-    bubble_ypos = y * tile_height + BUBBLE_YOFFSET;
-
-    game_area.queue_draw ();
-}
-
-
-/**
- * add_aieee_bubble
- * @x: x position
- * @y: y position
- *
- * Description:
- * adds and "Aieee" bubble at @x,@y
- **/
-public void add_aieee_bubble (int x, int y) {
-    bubble_type = BubbleType.AIEEE;
-    bubble_xpos = x * tile_width + BUBBLE_XOFFSET;
-    bubble_ypos = y * tile_height + BUBBLE_YOFFSET;
-
-    game_area.queue_draw ();
-}
-
-/**
- * add_splat_bubble
- * @x: x position
- * @y: y position
- *
- * Description:
- * adds a "Splat" speech bubble at @x,@y
- **/
-public void add_splat_bubble (int x, int y) {
-    bubble_type = BubbleType.SPLAT;
-    bubble_xpos = x * tile_width + BUBBLE_XOFFSET;
-    bubble_ypos = y * tile_height + 2 * BUBBLE_YOFFSET;
-
-    game_area.queue_draw ();
-}
-


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