From 0f30a3d8d40f8c10cf0fc84771d559774b353520 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Mon, 9 Jan 2023 07:46:31 +0100 Subject: [PATCH] audio changes --- .../api_front/src/pages/game/client/audio.ts | 13 +++++++++++-- .../pages/game/client/class/RectangleClient.ts | 14 ++++++++++---- .../svelte/api_front/src/pages/game/client/ws.ts | 15 +++++++++------ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts index 49cc0482..23f266e4 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts @@ -2,20 +2,29 @@ import * as c from "./constants.js" // export const soundPongArr: HTMLAudioElement[] = []; +export let muteFlag: boolean; export const soundPongArr: HTMLAudioElement[] = []; export let soundRoblox: HTMLAudioElement; export function initAudio(sound: string) { - let muteFlag: boolean; + soundPongArr.length = 0; + soundRoblox = null; + if (sound === "on") { muteFlag = false; } else { muteFlag = true; + return; // Could be changed + /* + Stop initAudio() here because in the current state of the game + there no way to change muteFlag after game start. + If it becomes an option, + we should continue initAudio() regardless of the muteFlag. + */ } - soundPongArr.length = 0; soundPongArr.push(new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+1+".ogg")); soundPongArr.push(new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+2+".ogg")); soundPongArr.forEach((value) => { diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/class/RectangleClient.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/class/RectangleClient.ts index e671fb3f..368fbce7 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/class/RectangleClient.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/class/RectangleClient.ts @@ -2,7 +2,7 @@ import { Vector, VectorInteger } from "../../shared_js/class/Vector.js"; import type { GraphicComponent } from "../../shared_js/class/interface.js"; import { Rectangle, MovingRectangle, Racket, Ball } from "../../shared_js/class/Rectangle.js"; -import { soundPongArr } from "../audio.js" +import { muteFlag, soundPongArr } from "../audio.js" import { random } from "../utils.js"; function updateRectangle(this: RectangleClient) { @@ -70,6 +70,7 @@ export class BallClient extends Ball implements GraphicComponent { color: string; update: () => void; clear: (pos?: VectorInteger) => void; + soundSwitch = false; constructor(pos: VectorInteger, size: number, baseSpeed: number, speedIncrease: number, ctx: CanvasRenderingContext2D, color: string) { @@ -81,9 +82,14 @@ export class BallClient extends Ball implements GraphicComponent { } bounce(collider?: Rectangle) { this._bounceAlgo(collider); - let i = Math.floor(random(0, soundPongArr.length)); - soundPongArr[ i ].play(); - console.log(`sound_i=${i}`); // debug log + if (!muteFlag) + { + this.soundSwitch = !this.soundSwitch; + soundPongArr[this.soundSwitch ? 1 : 0].play(); + // let i = Math.floor(random(0, soundPongArr.length)); + // soundPongArr[ i ].play(); + // console.log(`sound_i=${i}`); // debug log + } } } diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts index 4c590539..c2d61abf 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts @@ -6,7 +6,7 @@ import * as en from "../shared_js/enums.js" import * as msg from "./message.js"; import type { RacketClient } from "./class/RectangleClient.js"; import { repeatInput } from "./handleInput.js"; -import { soundRoblox } from "./audio.js" +import { muteFlag, soundRoblox } from "./audio.js" import { sleep } from "./utils.js"; import { Vector, VectorInteger } from "../shared_js/class/Vector.js"; @@ -198,11 +198,14 @@ function gameUpdate(data: ev.EventGameUpdate) function scoreUpdate(data: ev.EventScoreUpdate) { // console.log("scoreUpdate"); - if (clientInfo.side === en.PlayerSide.left && data.scoreRight > gc.scoreRight.value) { - soundRoblox.play(); - } - else if (clientInfo.side === en.PlayerSide.right && data.scoreLeft > gc.scoreLeft.value) { - soundRoblox.play(); + if (!muteFlag) + { + if (clientInfo.side === en.PlayerSide.left && data.scoreRight > gc.scoreRight.value) { + soundRoblox.play(); + } + else if (clientInfo.side === en.PlayerSide.right && data.scoreLeft > gc.scoreLeft.value) { + soundRoblox.play(); + } } gc.scoreLeft.value = data.scoreLeft; gc.scoreRight.value = data.scoreRight;