Browse Source

* Added sound to game

master
vvandenb 2 years ago
parent
commit
b3e649e9cf
  1. 1
      back/volume/src/pong/dtos/GameUpdate.ts
  2. 1
      back/volume/src/pong/game/Game.ts
  3. BIN
      front/volume/public/audio/edge_hit.wav
  4. BIN
      front/volume/public/audio/paddle_hit.wav
  5. BIN
      front/volume/public/audio/score.wav
  6. 2
      front/volume/src/components/Pong/Ball.ts
  7. 20
      front/volume/src/components/Pong/Game.ts
  8. 1
      front/volume/src/components/Pong/dtos/GameUpdate.ts

1
back/volume/src/pong/dtos/GameUpdate.ts

@ -2,6 +2,7 @@ import { type Point } from '../game/utils'
export class GameUpdate { export class GameUpdate {
paddlesPositions!: Point[] paddlesPositions!: Point[]
ballSpeed!: Point
ballPosition!: Point ballPosition!: Point
scores!: number[] scores!: number[]
} }

1
back/volume/src/pong/game/Game.ts

@ -157,6 +157,7 @@ export class Game {
const data: GameUpdate = { const data: GameUpdate = {
paddlesPositions: this.players.map((p) => p.paddle.rect.center), paddlesPositions: this.players.map((p) => p.paddle.rect.center),
ballSpeed: this.ball.speed,
ballPosition: this.ball.rect.center, ballPosition: this.ball.rect.center,
scores: this.players.map((p) => p.score) scores: this.players.map((p) => p.score)
} }

BIN
front/volume/public/audio/edge_hit.wav

Binary file not shown.

BIN
front/volume/public/audio/paddle_hit.wav

Binary file not shown.

BIN
front/volume/public/audio/score.wav

Binary file not shown.

2
front/volume/src/components/Pong/Ball.ts

@ -1,3 +1,4 @@
import { DEFAULT_BALL_INITIAL_SPEED } from "./constants";
import { Point, Rect } from "./utils"; import { Point, Rect } from "./utils";
export class Ball { export class Ball {
@ -6,6 +7,7 @@ export class Ball {
constructor(spawn: Point, size: Point = new Point(20, 20)) { constructor(spawn: Point, size: Point = new Point(20, 20)) {
this.rect = new Rect(spawn, size); this.rect = new Rect(spawn, size);
this.speed = DEFAULT_BALL_INITIAL_SPEED;
} }
draw(context: CanvasRenderingContext2D, color: string) { draw(context: CanvasRenderingContext2D, color: string) {

20
front/volume/src/components/Pong/Game.ts

@ -12,6 +12,7 @@ export class Game {
renderCanvas: HTMLCanvasElement; renderCanvas: HTMLCanvasElement;
canvas: HTMLCanvasElement; canvas: HTMLCanvasElement;
context: CanvasRenderingContext2D; context: CanvasRenderingContext2D;
ballLastVelocity: Point;
ball: Ball; ball: Ball;
players: Player[]; players: Player[];
my_paddle: Paddle; my_paddle: Paddle;
@ -23,6 +24,10 @@ export class Game {
ranked: boolean; ranked: boolean;
youAreReady: boolean; youAreReady: boolean;
private readonly score_audio = new Audio('audio/score.wav');
private readonly paddle_hit_audio = new Audio('audio/paddle_hit.wav');
private readonly edge_hit_audio = new Audio('audio/edge_hit.wav');
constructor( constructor(
renderCanvas: HTMLCanvasElement, renderCanvas: HTMLCanvasElement,
canvas: HTMLCanvasElement, canvas: HTMLCanvasElement,
@ -83,14 +88,12 @@ export class Game {
} }
start(socket: WebSocket) { start(socket: WebSocket) {
// if (this.my_paddle) {
this.renderCanvas.addEventListener("pointermove", (e) => { this.renderCanvas.addEventListener("pointermove", (e) => {
this.my_paddle.move(e); this.my_paddle.move(e);
const data: Point = this.my_paddle.rect.center; const data: Point = this.my_paddle.rect.center;
socket.send(formatWebsocketData(GAME_EVENTS.PLAYER_MOVE, data)); socket.send(formatWebsocketData(GAME_EVENTS.PLAYER_MOVE, data));
}); });
console.log("Game started!"); console.log("Game started!");
// }
} }
update(data: GameUpdate) { update(data: GameUpdate) {
@ -101,8 +104,21 @@ export class Game {
if (this.players[1].paddle != this.my_paddle) { if (this.players[1].paddle != this.my_paddle) {
this.players[1].paddle.rect.center = data.paddlesPositions[1]; this.players[1].paddle.rect.center = data.paddlesPositions[1];
} }
if (data.ballSpeed.x * this.ball.speed.x < 0) {
this.paddle_hit_audio.play();
}
if (data.ballSpeed.y * this.ball.speed.y < 0) {
this.edge_hit_audio.play();
}
this.ball.speed = data.ballSpeed;
this.ball.rect.center = data.ballPosition; this.ball.rect.center = data.ballPosition;
for (let i = 0; i < data.scores.length; i++) { for (let i = 0; i < data.scores.length; i++) {
if (this.players[i].score != data.scores[i]) {
this.score_audio.play();
}
this.players[i].score = data.scores[i]; this.players[i].score = data.scores[i];
} }
} }

1
front/volume/src/components/Pong/dtos/GameUpdate.ts

@ -2,6 +2,7 @@ import type { Point } from "../utils";
export class GameUpdate { export class GameUpdate {
paddlesPositions!: Point[]; paddlesPositions!: Point[];
ballSpeed!: Point;
ballPosition!: Point; ballPosition!: Point;
scores!: number[]; scores!: number[];
} }

Loading…
Cancel
Save