Browse Source

added vasco missed commit

master
narnaud 2 years ago
parent
commit
82a826ea16
  1. 2
      volumes/front/src/components/Pong/Game.ts
  2. 62
      volumes/front/src/components/Pong/Pong.svelte
  3. 7
      volumes/front/src/components/Pong/constants.ts

2
volumes/front/src/components/Pong/Game.ts

@ -13,6 +13,7 @@ export class Game {
ball: Ball; ball: Ball;
players: Player[]; players: Player[];
my_paddle: Paddle; my_paddle: Paddle;
id: string;
constructor(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D) { constructor(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D) {
this.canvas = canvas; this.canvas = canvas;
@ -34,6 +35,7 @@ export class Game {
); );
this.players = [new Player(paddle1), new Player(paddle2)]; this.players = [new Player(paddle1), new Player(paddle2)];
this.my_paddle = this.players[data.yourPaddleIndex].paddle; this.my_paddle = this.players[data.yourPaddleIndex].paddle;
this.id = data.gameId;
} }
start(socket: WebSocket) { start(socket: WebSocket) {

62
volumes/front/src/components/Pong/Pong.svelte

@ -2,12 +2,14 @@
import { GAME_EVENTS } from './constants'; import { GAME_EVENTS } from './constants';
import { Game } from './Game'; import { Game } from './Game';
import { formatWebsocketData } from './utils'; import { formatWebsocketData } from './utils';
import { onMount } from 'svelte'
const FPS = 144; const FPS = 144;
const SERVER_URL = 'ws://localhost:3001'; const SERVER_URL = 'ws://localhost:3001';
let connected: boolean = false;
let socket: WebSocket; let socket: WebSocket;
let username: string = 'John';
let otherUsername: string = 'Garfield';
//Get canvas and its context //Get canvas and its context
window.onload = () => { window.onload = () => {
@ -19,6 +21,7 @@
} }
} }
}; };
function setupSocket(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D) { function setupSocket(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D) {
socket = new WebSocket(SERVER_URL); socket = new WebSocket(SERVER_URL);
const game = new Game(canvas, context); const game = new Game(canvas, context);
@ -32,25 +35,62 @@
} else if (event == GAME_EVENTS.GAME_TICK) { } else if (event == GAME_EVENTS.GAME_TICK) {
game.update(data); game.update(data);
} else if (event == GAME_EVENTS.GET_GAME_INFO) { } else if (event == GAME_EVENTS.GET_GAME_INFO) {
game.setInfo(data); if (data && data.gameId != game.id) {
setInterval(() => { game.setInfo(data);
game.draw(); setInterval(() => {
}, 1000 / FPS); game.draw();
console.log('Game loaded!'); }, 1000 / FPS);
console.log('Game updated!');
}
} else { } else {
console.log('Unknown event from server: ' + event); console.log('Unknown event from server: ' + event);
} }
}; };
socket.onopen = () => { socket.onopen = () => {
console.log('Connected to game server!'); connected = true;
socket.send(formatWebsocketData(GAME_EVENTS.GET_GAME_INFO)); };
socket.onclose = () => {
connected = false;
setupSocket(canvas, context);
}; };
} }
function updateGameInfo() {
socket.send(formatWebsocketData(GAME_EVENTS.GET_GAME_INFO));
}
function connectToServer() {
socket.send(formatWebsocketData(GAME_EVENTS.REGISTER_PLAYER, { playerName: username }));
setInterval(() => {
updateGameInfo();
}, 1000);
}
</script> </script>
<div> <div>
<button on:click={() => socket.send(formatWebsocketData(GAME_EVENTS.START_GAME))}>Start game</button> {#if connected}
<br /> Your name:
<br /> <input bind:value={username} />
<br />
<button on:click={connectToServer}> Connect </button>
<br />
Other player name:
<input bind:value={otherUsername} />
<br />
<button
on:click={() => {
socket.send(formatWebsocketData(GAME_EVENTS.CREATE_GAME, { playerNames: [username, otherUsername] }));
updateGameInfo();
}}
>
Create game vs {otherUsername}
</button>
<br />
<button on:click={() => socket.send(formatWebsocketData(GAME_EVENTS.READY))}>Ready</button>
<br />
<br />
{:else}
Connecting to game server...
{/if}
<canvas id="pong_canvas" /> <canvas id="pong_canvas" />
</div> </div>

7
volumes/front/src/components/Pong/constants.ts

@ -2,13 +2,17 @@ import { Point } from './utils';
export const GAME_EVENTS = { export const GAME_EVENTS = {
START_GAME: 'START_GAME', START_GAME: 'START_GAME',
READY: 'READY',
GAME_TICK: 'GAME_TICK', GAME_TICK: 'GAME_TICK',
PLAYER_MOVE: 'PLAYER_MOVE', PLAYER_MOVE: 'PLAYER_MOVE',
GET_GAME_INFO: 'GET_GAME_INFO' GET_GAME_INFO: 'GET_GAME_INFO',
CREATE_GAME: 'CREATE_GAME',
REGISTER_PLAYER: 'REGISTER_PLAYER'
}; };
export interface GameInfo extends GameInfoConstants { export interface GameInfo extends GameInfoConstants {
yourPaddleIndex: number; yourPaddleIndex: number;
gameId: string;
} }
export interface GameInfoConstants { export interface GameInfoConstants {
mapSize: Point; mapSize: Point;
@ -30,3 +34,4 @@ export interface GameUpdate {
ballPosition: Point; ballPosition: Point;
scores: number[]; scores: number[];
} }

Loading…
Cancel
Save