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