diff --git a/front/volume/src/components/NavBar.svelte b/front/volume/src/components/NavBar.svelte
index 8de5531..b2866ff 100644
--- a/front/volume/src/components/NavBar.svelte
+++ b/front/volume/src/components/NavBar.svelte
@@ -27,28 +27,28 @@
{#if link.text === "Leaderboard"}
+
-
+
+
+
+
diff --git a/front/volume/src/components/Pong/MapCustomization.svelte b/front/volume/src/components/Pong/MapCustomization.svelte
index 2e4b441..4a94656 100644
--- a/front/volume/src/components/Pong/MapCustomization.svelte
+++ b/front/volume/src/components/Pong/MapCustomization.svelte
@@ -3,10 +3,11 @@
import { Point, Rect } from "./utils";
import type { Map } from "./Map";
import { DEFAULT_BALL_SIZE, DEFAULT_MAP_SIZE } from "./constants";
+ import { Ball } from "./Ball";
export let map: Map;
- let canvas: HTMLCanvasElement;
+ let gameCanvas: HTMLCanvasElement;
let context: CanvasRenderingContext2D;
let wallWidth = 20;
let wallHeight = 80;
@@ -14,65 +15,95 @@
const MAX_WALLS = 5;
onMount(() => {
- if (canvas) {
- canvas.width = map.size.x;
- canvas.height = map.size.y;
- context = canvas.getContext("2d");
+ if (gameCanvas) {
+ gameCanvas.width = map.size.x;
+ gameCanvas.height = map.size.y;
+ context = gameCanvas.getContext("2d");
drawMap();
}
});
- $: {
- if (canvas) {
- canvas.width = map.size.x;
- canvas.height = map.size.y;
- }
- drawMap();
- }
-
function drawMap() {
- if (canvas && context) {
+ if (gameCanvas && context) {
context.fillStyle = "black";
context.fillRect(0, 0, map.size.x, map.size.y);
for (const wall of map.walls) {
wall.draw(context, "white");
}
+ const ball = new Ball(
+ new Point(map.size.x / 2, map.size.y / 2),
+ DEFAULT_BALL_SIZE
+ );
+ ball.draw(context, "white");
}
}
function click(e: MouseEvent, rightClick: boolean) {
- if (rightClick) removeWall(e);
- else addWall(e);
+ if (rightClick) {
+ e.preventDefault();
+ removeWall(e);
+ } else {
+ addWall(e);
+ }
drawMap();
}
function addWall(e: MouseEvent) {
+ const rect: any = gameCanvas.getBoundingClientRect();
const wall = new Rect(
- new Point(e.offsetX, e.offsetY),
+ getMapXY(e),
new Point(wallWidth, wallHeight)
);
const ballSpawnArea = new Rect(
new Point(map.size.x / 2, map.size.y / 2),
new Point(DEFAULT_BALL_SIZE.x * 5, DEFAULT_BALL_SIZE.y * 5)
);
+
if (map.walls.length < MAX_WALLS && !wall.collides(ballSpawnArea))
map.walls.push(wall);
}
function removeWall(e: MouseEvent) {
- e.preventDefault();
- const click = new Rect(new Point(e.offsetX, e.offsetY), new Point(1, 1));
+ const click = new Rect(getMapXY(e), new Point(1, 1));
const index = map.walls.findIndex((w) => w.collides(click));
if (index != -1) map.walls.splice(index, 1);
}
+
+ function getCanvasXY(pagePoint: Point): Point {
+ const rect: any = gameCanvas.getBoundingClientRect();
+ const x = pagePoint.x - rect.left;
+ const y = pagePoint.y - rect.top;
+ return new Point(x, y);
+ }
+
+ function getMapXY(e: MouseEvent): Point {
+ const canvasPoint: Point = getCanvasXY(new Point(e.pageX, e.pageY));
+ const x = canvasPoint.x * gameCanvas.width / gameCanvas.clientWidth;
+ const y = canvasPoint.y * gameCanvas.height / gameCanvas.clientHeight;
+ return new Point(x, y);
+ }
Map Customization:
Right click to add walls, left click to remove walls. (Max {MAX_WALLS} walls)
+
+
+
diff --git a/front/volume/src/components/Pong/Pong.svelte b/front/volume/src/components/Pong/Pong.svelte
index 9a0bb57..716b571 100644
--- a/front/volume/src/components/Pong/Pong.svelte
+++ b/front/volume/src/components/Pong/Pong.svelte
@@ -19,38 +19,47 @@
let spectateWindow: boolean = false;
let gamePlaying: boolean = false;
let matchmaking: boolean = false;
- let gameCanvas: HTMLCanvasElement;
let connected: boolean = false;
let loggedIn: boolean = false;
let failedLogIn: boolean = false;
let socket: WebSocket;
- let username: string = $store.username;
let elementsColor: string = "#FFFFFF";
let backgroundColor: string = "#000000";
let game: Game;
+ let renderCanvas: HTMLCanvasElement;
+ let canvas: HTMLCanvasElement;
+ let context: CanvasRenderingContext2D;
function setupSocket(
- canvas: HTMLCanvasElement,
- context: CanvasRenderingContext2D
+ _renderCanvas: HTMLCanvasElement,
+ _canvas: HTMLCanvasElement,
+ _context: CanvasRenderingContext2D
) {
socket = new WebSocket(SERVER_URL);
- game = new Game(canvas, context, elementsColor, backgroundColor);
+ renderCanvas = _renderCanvas;
+ canvas = _canvas;
+ context = _context;
+ game = new Game(_renderCanvas, canvas, context, elementsColor, backgroundColor);
+
socket.onmessage = function (e) {
const event_json = JSON.parse(e.data);
const event = event_json.event;
const data = event_json.data;
if (event == GAME_EVENTS.START_GAME) {
- matchmaking = false;
game.start(socket);
} else if (event == GAME_EVENTS.GAME_TICK) {
game.update(data);
} else if (event == GAME_EVENTS.GET_GAME_INFO) {
if (data && data.gameId != game.id) {
+ if (gamePlaying && data.gameId == '') {
+ resetMenus();
+ gamePlaying = false;
+ }
if (data.yourPaddleIndex !== -2) {
gamePlaying = true;
game.setInfo(data);
- } else gamePlaying = false;
+ }
}
} else if (event == GAME_EVENTS.REGISTER_PLAYER) {
if (data) {
@@ -78,26 +87,29 @@
);
}
};
- socket.onopen = () => {
- void logIn();
- connected = true;
- };
- socket.onclose = () => {
- connected = false;
- setupSocket(canvas, context);
- };
+ socket.onopen = onSocketOpen
+ socket.onclose = onSocketClose
}
+ async function onSocketOpen() {
+ await getUser();
+ void logIn();
+ connected = true;
+ };
+
+ async function onSocketClose() {
+ connected = false;
+ setupSocket(renderCanvas, canvas, context);
+ };
+
function updateGameInfo() {
socket.send(formatWebsocketData(GAME_EVENTS.GET_GAME_INFO));
}
async function logIn() {
- await getUser();
- const socketKey = $store.socketKey;
const data: { playerName: StringDto; socketKey: StringDto } = {
- playerName: { value: username },
- socketKey: { value: socketKey },
+ playerName: { value: $store.username },
+ socketKey: { value: $store.socketKey },
};
socket.send(formatWebsocketData(GAME_EVENTS.REGISTER_PLAYER, data));
}
@@ -112,6 +124,12 @@
socket.send(formatWebsocketData(GAME_EVENTS.MATCHMAKING, data));
}
+ function resetMenus() {
+ createMatchWindow = false;
+ spectateWindow = false;
+ matchmaking = false;
+ }
+
$: {
if (game !== undefined) {
game.updateColors(elementsColor, backgroundColor);
@@ -120,7 +138,7 @@
-
+
{#if gamePlaying}
{:else if loggedIn}
@@ -145,7 +163,7 @@
on:click={() => (createMatchWindow = false)}
on:keydown={() => (createMatchWindow = false)}
>
-
+
{:else if spectateWindow}