audio changes

This commit is contained in:
LuckyLaszlo
2023-01-09 07:46:31 +01:00
parent 2ae0ee043a
commit 0f30a3d8d4
3 changed files with 30 additions and 12 deletions

View File

@@ -2,20 +2,29 @@
import * as c from "./constants.js" import * as c from "./constants.js"
// export const soundPongArr: HTMLAudioElement[] = []; // export const soundPongArr: HTMLAudioElement[] = [];
export let muteFlag: boolean;
export const soundPongArr: HTMLAudioElement[] = []; export const soundPongArr: HTMLAudioElement[] = [];
export let soundRoblox: HTMLAudioElement; export let soundRoblox: HTMLAudioElement;
export function initAudio(sound: string) export function initAudio(sound: string)
{ {
let muteFlag: boolean; soundPongArr.length = 0;
soundRoblox = null;
if (sound === "on") { if (sound === "on") {
muteFlag = false; muteFlag = false;
} }
else { else {
muteFlag = true; 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/"+1+".ogg"));
soundPongArr.push(new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+2+".ogg")); soundPongArr.push(new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+2+".ogg"));
soundPongArr.forEach((value) => { soundPongArr.forEach((value) => {

View File

@@ -2,7 +2,7 @@
import { Vector, VectorInteger } from "../../shared_js/class/Vector.js"; import { Vector, VectorInteger } from "../../shared_js/class/Vector.js";
import type { GraphicComponent } from "../../shared_js/class/interface.js"; import type { GraphicComponent } from "../../shared_js/class/interface.js";
import { Rectangle, MovingRectangle, Racket, Ball } from "../../shared_js/class/Rectangle.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"; import { random } from "../utils.js";
function updateRectangle(this: RectangleClient) { function updateRectangle(this: RectangleClient) {
@@ -70,6 +70,7 @@ export class BallClient extends Ball implements GraphicComponent {
color: string; color: string;
update: () => void; update: () => void;
clear: (pos?: VectorInteger) => void; clear: (pos?: VectorInteger) => void;
soundSwitch = false;
constructor(pos: VectorInteger, size: number, baseSpeed: number, speedIncrease: number, constructor(pos: VectorInteger, size: number, baseSpeed: number, speedIncrease: number,
ctx: CanvasRenderingContext2D, color: string) ctx: CanvasRenderingContext2D, color: string)
{ {
@@ -81,9 +82,14 @@ export class BallClient extends Ball implements GraphicComponent {
} }
bounce(collider?: Rectangle) { bounce(collider?: Rectangle) {
this._bounceAlgo(collider); this._bounceAlgo(collider);
let i = Math.floor(random(0, soundPongArr.length)); if (!muteFlag)
soundPongArr[ i ].play(); {
console.log(`sound_i=${i}`); // debug log 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
}
} }
} }

View File

@@ -6,7 +6,7 @@ import * as en from "../shared_js/enums.js"
import * as msg from "./message.js"; import * as msg from "./message.js";
import type { RacketClient } from "./class/RectangleClient.js"; import type { RacketClient } from "./class/RectangleClient.js";
import { repeatInput } from "./handleInput.js"; import { repeatInput } from "./handleInput.js";
import { soundRoblox } from "./audio.js" import { muteFlag, soundRoblox } from "./audio.js"
import { sleep } from "./utils.js"; import { sleep } from "./utils.js";
import { Vector, VectorInteger } from "../shared_js/class/Vector.js"; import { Vector, VectorInteger } from "../shared_js/class/Vector.js";
@@ -198,11 +198,14 @@ function gameUpdate(data: ev.EventGameUpdate)
function scoreUpdate(data: ev.EventScoreUpdate) function scoreUpdate(data: ev.EventScoreUpdate)
{ {
// console.log("scoreUpdate"); // console.log("scoreUpdate");
if (clientInfo.side === en.PlayerSide.left && data.scoreRight > gc.scoreRight.value) { if (!muteFlag)
soundRoblox.play(); {
} if (clientInfo.side === en.PlayerSide.left && data.scoreRight > gc.scoreRight.value) {
else if (clientInfo.side === en.PlayerSide.right && data.scoreLeft > gc.scoreLeft.value) { soundRoblox.play();
soundRoblox.play(); }
else if (clientInfo.side === en.PlayerSide.right && data.scoreLeft > gc.scoreLeft.value) {
soundRoblox.play();
}
} }
gc.scoreLeft.value = data.scoreLeft; gc.scoreLeft.value = data.scoreLeft;
gc.scoreRight.value = data.scoreRight; gc.scoreRight.value = data.scoreRight;