mirror of
https://github.com/OpenLiberty/liberty-bikes.git
synced 2025-01-24 10:34:03 +08:00
Control player size in pixels on frontend
This commit is contained in:
parent
e5177c652f
commit
393861b297
@ -34,11 +34,6 @@ export class GameWebsocket {
|
|||||||
this.websocket.send(json);
|
this.websocket.send(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendBinary(bytes: any) {
|
|
||||||
console.log(`sending binary: ${bytes.toString()}`);
|
|
||||||
this.websocket.send(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
onMessage(evt: MessageEvent) {
|
onMessage(evt: MessageEvent) {
|
||||||
console.log(`received: ${evt.data}`);
|
console.log(`received: ${evt.data}`);
|
||||||
if (typeof evt.data === 'string') {
|
if (typeof evt.data === 'string') {
|
||||||
|
@ -2,6 +2,7 @@ import * as $ from 'jquery';
|
|||||||
import { GameWebsocket } from './websocket';
|
import { GameWebsocket } from './websocket';
|
||||||
|
|
||||||
export class Whiteboard {
|
export class Whiteboard {
|
||||||
|
static readonly PLAYER_SIZE = 5;
|
||||||
canvas: any;
|
canvas: any;
|
||||||
context: any;
|
context: any;
|
||||||
gamesocket: GameWebsocket;
|
gamesocket: GameWebsocket;
|
||||||
@ -37,7 +38,8 @@ export class Whiteboard {
|
|||||||
drawSquare(data) {
|
drawSquare(data) {
|
||||||
const json = JSON.parse(data);
|
const json = JSON.parse(data);
|
||||||
this.context.fillStyle = json.color;
|
this.context.fillStyle = json.color;
|
||||||
this.context.fillRect(json.coords.x, json.coords.y, 5, 5);
|
this.context.fillRect(Whiteboard.PLAYER_SIZE * json.coords.x, Whiteboard.PLAYER_SIZE * json.coords.y,
|
||||||
|
Whiteboard.PLAYER_SIZE, Whiteboard.PLAYER_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePlayerList(json) {
|
updatePlayerList(json) {
|
||||||
|
@ -21,8 +21,8 @@ public class GameRound implements Runnable {
|
|||||||
OPEN, FULL, RUNNING, FINISHED
|
OPEN, FULL, RUNNING, FINISHED
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final int GAME_TICK_SPEED = 50;
|
public static final int GAME_TICK_SPEED = 50; // ms
|
||||||
public static final int GAME_SIZE = 600;
|
public static final int BOARD_SIZE = 121;
|
||||||
|
|
||||||
private static final Random r = new Random();
|
private static final Random r = new Random();
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ public class GameRound implements Runnable {
|
|||||||
public Set<Player> players = new HashSet<Player>();
|
public Set<Player> players = new HashSet<Player>();
|
||||||
public State state = State.OPEN;
|
public State state = State.OPEN;
|
||||||
|
|
||||||
private boolean[][] board = new boolean[121][121];
|
private boolean[][] board = new boolean[BOARD_SIZE][BOARD_SIZE];
|
||||||
private AtomicBoolean gameRunning = new AtomicBoolean(false);
|
private AtomicBoolean gameRunning = new AtomicBoolean(false);
|
||||||
private AtomicBoolean paused = new AtomicBoolean(false);
|
private AtomicBoolean paused = new AtomicBoolean(false);
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ public class GameRound implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
for (int i = 0; i < GAME_SIZE / Player.PLAYER_SIZE + 1; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
Arrays.fill(board[i], true);
|
Arrays.fill(board[i], true);
|
||||||
gameRunning.set(true);
|
gameRunning.set(true);
|
||||||
System.out.println("Starting round: " + id);
|
System.out.println("Starting round: " + id);
|
||||||
|
@ -27,8 +27,6 @@ public class Player {
|
|||||||
Disconnected
|
Disconnected
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final int PLAYER_SIZE = 5;
|
|
||||||
|
|
||||||
private Session client;
|
private Session client;
|
||||||
public final String color;
|
public final String color;
|
||||||
public DIRECTION direction = DIRECTION.RIGHT;
|
public DIRECTION direction = DIRECTION.RIGHT;
|
||||||
@ -90,29 +88,29 @@ public class Player {
|
|||||||
*/
|
*/
|
||||||
public boolean movePlayer(boolean[][] board) {
|
public boolean movePlayer(boolean[][] board) {
|
||||||
// Consume the space the player was in before the move
|
// Consume the space the player was in before the move
|
||||||
board[x / PLAYER_SIZE][y / PLAYER_SIZE] = false;
|
board[x][y] = false;
|
||||||
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case UP:
|
case UP:
|
||||||
if (y - PLAYER_SIZE >= 0)
|
if (y - 1 >= 0)
|
||||||
y -= PLAYER_SIZE;
|
y--;
|
||||||
break;
|
break;
|
||||||
case DOWN:
|
case DOWN:
|
||||||
if (y + PLAYER_SIZE < GameRound.GAME_SIZE)
|
if (y + 1 < GameRound.BOARD_SIZE)
|
||||||
y += PLAYER_SIZE;
|
y++;
|
||||||
break;
|
break;
|
||||||
case RIGHT:
|
case RIGHT:
|
||||||
if (x + PLAYER_SIZE < GameRound.GAME_SIZE)
|
if (x + 1 < GameRound.BOARD_SIZE)
|
||||||
x += PLAYER_SIZE;
|
x++;
|
||||||
break;
|
break;
|
||||||
case LEFT:
|
case LEFT:
|
||||||
if (x - PLAYER_SIZE >= 0)
|
if (x - 1 >= 0)
|
||||||
x -= PLAYER_SIZE;
|
x--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the player is now dead after moving
|
// Check if the player is now dead after moving
|
||||||
boolean spaceAvailable = board[x / PLAYER_SIZE][y / PLAYER_SIZE];
|
boolean spaceAvailable = board[x][y];
|
||||||
if (!spaceAvailable) {
|
if (!spaceAvailable) {
|
||||||
setStatus(STATUS.Dead);
|
setStatus(STATUS.Dead);
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,10 @@ public class PlayerFactory {
|
|||||||
public static int MAX_PLAYERS = PlayerData.values().length;
|
public static int MAX_PLAYERS = PlayerData.values().length;
|
||||||
|
|
||||||
private static enum PlayerData {
|
private static enum PlayerData {
|
||||||
START_1("#DF740C", 50, 50, DIRECTION.RIGHT),
|
START_1("#DF740C", 10, 10, DIRECTION.RIGHT),
|
||||||
START_2("#FF0000", 50, GameRound.GAME_SIZE - 50, DIRECTION.UP),
|
START_2("#FF0000", 10, GameRound.BOARD_SIZE - 10, DIRECTION.UP),
|
||||||
START_3("#6FC3DF", GameRound.GAME_SIZE - 50, 50, DIRECTION.DOWN),
|
START_3("#6FC3DF", GameRound.BOARD_SIZE - 10, 10, DIRECTION.DOWN),
|
||||||
START_4("#FFE64D", GameRound.GAME_SIZE - 50, GameRound.GAME_SIZE - 50, DIRECTION.LEFT);
|
START_4("#FFE64D", GameRound.BOARD_SIZE - 10, GameRound.BOARD_SIZE - 10, DIRECTION.LEFT);
|
||||||
|
|
||||||
public final String color;
|
public final String color;
|
||||||
public final int x;
|
public final int x;
|
||||||
|
Loading…
Reference in New Issue
Block a user