[gnome-robots] encapsulate game area size
- From: Andrey Kutejko <akutejko src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-robots] encapsulate game area size
- Date: Tue, 6 Oct 2020 19:31:41 +0000 (UTC)
commit f32caabdbd97ce4519d83994d8365163fd6bd92a
Author: Andrey Kutejko <andy128k gmail com>
Date: Mon Sep 14 19:57:33 2020 +0200
encapsulate game area size
src/game-area.vala | 45 +++++++++++++++++++++++++++++++++++++++++++--
src/game.vala | 50 ++++++++++++--------------------------------------
src/graphics.vala | 9 ---------
src/robots.vala | 2 +-
4 files changed, 56 insertions(+), 50 deletions(-)
---
diff --git a/src/game-area.vala b/src/game-area.vala
index 8fd1ef0..dc79478 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -26,6 +26,9 @@ public class GameArea : DrawingArea {
const int MINIMUM_TILE_WIDTH = 8;
const int MINIMUM_TILE_HEIGHT = 8;
+ private int tile_width = 0;
+ private int tile_height = 0;
+
private GestureMultiPress click_controller;
private EventControllerMotion motion_controller;
private Game game;
@@ -220,7 +223,7 @@ public class GameArea : DrawingArea {
}
int dx, dy;
- game.get_dir ((int)x, (int)y, out dx, out dy);
+ get_dir (x, y, out dx, out dy);
if (game.player_move (dx, dy)) {
game.move_robots ();
@@ -233,9 +236,47 @@ public class GameArea : DrawingArea {
set_cursor_default (window);
} else {
int dx, dy;
- game.get_dir ((int)x, (int)y, out dx, out dy);
+ get_dir (x, y, out dx, out dy);
set_cursor_by_direction (window, dx, dy);
}
}
+
+ private void get_dir (double ix, double iy, out int odx, out int ody) {
+ const int[,] MOVE_TABLE = {
+ {-1, 0}, {-1, -1}, {0, -1}, {1, -1},
+ {1, 0}, {1, 1}, {0, 1}, {-1, 1}
+ };
+ int x = ((int) (ix / tile_width)).clamp (0, game.arena.width);
+ int y = ((int) (iy / tile_height)).clamp (0, game.arena.height);
+
+ /* If we click on our man then we assume we hold. */
+ if ((x == game.player.x) && (y == game.player.y)) {
+ odx = 0;
+ ody = 0;
+ return;
+ }
+
+ /* If the square clicked on is a valid move, go there. */
+ int idx = x - game.player.x;
+ int idy = y - game.player.y;
+ if (idx.abs () < 2 && idy.abs () < 2) {
+ odx = idx;
+ ody = idy;
+ return;
+ }
+
+ /* Otherwise go in the general direction of the mouse click. */
+ double dx = ix - (game.player.x + 0.5) * tile_width;
+ double dy = iy - (game.player.y + 0.5) * tile_height;
+
+ double angle = Math.atan2 (dy, dx);
+
+ /* Note the adjustment we have to make (+9, not +8) because atan2's idea
+ * of octants and the ones we want are shifted by PI/8. */
+ int octant = (((int) Math.floor (8.0 * angle / Math.PI) + 9) / 2) % 8;
+
+ odx = MOVE_TABLE[octant, 0];
+ ody = MOVE_TABLE[octant, 1];
+ }
}
diff --git a/src/game.vala b/src/game.vala
index b5a5105..3a3c498 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -28,6 +28,12 @@ public Game game = null;
public class Game {
+ /*
+ * Size of the game playing area
+ */
+ public const int GAME_WIDTH = 45;
+ public const int GAME_HEIGHT = 30;
+
public enum State {
PLAYING = 1,
WAITING,
@@ -57,6 +63,12 @@ public class Game {
public State state = State.PLAYING;
public Arena arena;
public GameConfig config { get; set; }
+ public int width {
+ get { return arena.width; }
+ }
+ public int height {
+ get { return arena.height; }
+ }
public Arena.Coords player { get; private set; }
public Arena.Coords? splat { get; private set; }
@@ -1069,44 +1081,6 @@ public class Game {
break;
}
}
-
- public void get_dir (int ix, int iy, out int odx, out int ody) {
- const int[,] MOVE_TABLE = {
- {-1, 0}, {-1, -1}, {0, -1}, {1, -1},
- {1, 0}, {1, 1}, {0, 1}, {-1, 1}
- };
- int x = (ix / tile_width).clamp (0, arena.width);
- int y = (iy / tile_height).clamp (0, arena.height);
-
- /* If we click on our man then we assume we hold. */
- 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.x;
- int idy = y - player.y;
- if (idx.abs () < 2 && idy.abs () < 2) {
- odx = idx;
- ody = idy;
- return;
- }
-
- /* Otherwise go in the general direction of the mouse click. */
- double dx = ix - (player.x + 0.5) * tile_width;
- double dy = iy - (player.y + 0.5) * tile_height;
-
- double angle = Math.atan2 (dy, dx);
-
- /* Note the adjustment we have to make (+9, not +8) because atan2's idea
- * of octants and the ones we want are shifted by PI/8. */
- int octant = (((int) Math.floor (8.0 * angle / Math.PI) + 9) / 2) % 8;
-
- odx = MOVE_TABLE[octant, 0];
- ody = MOVE_TABLE[octant, 1];
- }
}
/**
diff --git a/src/graphics.vala b/src/graphics.vala
index 8637d85..83a7ec8 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -21,15 +21,6 @@ using Gtk;
using Gdk;
using Cairo;
-/*
- * Size of the game playing area
- */
-public const int GAME_WIDTH = 45;
-public const int GAME_HEIGHT = 30;
-
-public int tile_width = 0;
-public int tile_height = 0;
-
public RGBA calculate_light_color (RGBA color) {
/* While the two colours are labelled "light" and "dark" which one is
* which actually depends on how light or dark the base colour is. */
diff --git a/src/robots.vala b/src/robots.vala
index 46a1d70..240279a 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -324,7 +324,7 @@ void activate (Gtk.Application app) {
splat_bubble);
game_area.destroy.connect (() => game_area = null);
- var gridframe = new Games.GridFrame (GAME_WIDTH, GAME_HEIGHT);
+ var gridframe = new Games.GridFrame (game.width, game.height);
gridframe.add (game_area);
var hbox = new Box (Orientation.HORIZONTAL, 0);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]