diff --git a/src/client/audio.ts b/src/client/audio.ts new file mode 100644 index 00000000..42e01526 --- /dev/null +++ b/src/client/audio.ts @@ -0,0 +1,12 @@ + +import * as c from "./constants.js" + +export const soundPong: HTMLAudioElement[] = []; + +for (let i = 0; i <= 32; i++) { + soundPong.push(new Audio("http://localhost:8080/sound/pong/"+i+".mp3")); + soundPong[i].volume = c.soundPongVolume; +} + +export const soundRoblox = new Audio("http://localhost:8080/sound/roblox-oof.mp3"); +soundRoblox.volume = c.soundRobloxVolume; diff --git a/src/client/class/RectangleClient.ts b/src/client/class/RectangleClient.ts index e0e83133..ad5b3da4 100644 --- a/src/client/class/RectangleClient.ts +++ b/src/client/class/RectangleClient.ts @@ -2,6 +2,8 @@ import {Vector, VectorInteger} from "../../shared_js/class/Vector.js"; import {Component, GraphicComponent, Moving} from "../../shared_js/class/interface.js"; import { Rectangle, MovingRectangle, Racket, Ball } from "../../shared_js/class/Rectangle.js"; +import {soundPong, soundRoblox} from "../audio.js" +import { random } from "../utils.js"; function updateRectangle(this: RectangleClient) { this.ctx.fillStyle = this.color; @@ -87,6 +89,14 @@ class BallClient extends Ball implements GraphicComponent { this.update = updateRectangle; this.clear = clearRectangle; } + bounce(collider?: Rectangle) { + this._bounceAlgo(collider); + soundPong[ Math.floor(random(0, soundPong.length)) ].play(); + } + /* protected _bouncePlayer(collider: Racket) { + this._bouncePlayerAlgo(collider); + soundRoblox.play(); + } */ } function updateLine(this: Line) { diff --git a/src/client/constants.ts b/src/client/constants.ts index df5c8e68..c724e89a 100644 --- a/src/client/constants.ts +++ b/src/client/constants.ts @@ -8,3 +8,5 @@ export const gridSize = Math.floor(w/500); // min interval on Firefox seems to be 15. Chrome can go lower. export const gameLoopIntervalMS = 15; // millisecond +export const soundRobloxVolume = 0.3; // between 0 and 1 +export const soundPongVolume = 0.3; // between 0 and 1 diff --git a/src/client/gameLoop.ts b/src/client/gameLoop.ts index a6028ec3..04d4d846 100644 --- a/src/client/gameLoop.ts +++ b/src/client/gameLoop.ts @@ -1,3 +1,4 @@ +import {gc} from "./global.js"; import * as d from "./draw.js"; import {handleInput} from "./handleInput.js"; @@ -19,7 +20,7 @@ function gameLoop() handleInput(delta_time); // prediction - // gc.ball.moveAndBounce(delta_time, [gc.wallTop, gc.wallBottom, gc.playerLeft, gc.playerRight]); + gc.ball.moveAndBounce(delta_time, [gc.wallTop, gc.wallBottom, gc.playerLeft, gc.playerRight]); d.draw(); } diff --git a/src/client/handleInput.ts b/src/client/handleInput.ts index 9eef3dad..2e929042 100644 --- a/src/client/handleInput.ts +++ b/src/client/handleInput.ts @@ -27,24 +27,23 @@ function handleInput(delta: number) function playerMove(delta: number, keys: string[]) { - if (keys.indexOf("w") != -1) { + if (keys.indexOf("w") != -1 || keys.indexOf("ArrowUp".toLowerCase()) != -1) { socket.send(JSON.stringify(new EventInput(InputEnum.up))); } - if (keys.indexOf("s") != -1) { + if (keys.indexOf("s") != -1 || keys.indexOf("ArrowDown".toLowerCase()) != -1) { socket.send(JSON.stringify(new EventInput(InputEnum.down))); } // prediction -/* const racket = clientInfo.racket; + const racket = clientInfo.racket; racket.dir.y = 0; - if (keys.indexOf("w") != -1) { + if (keys.indexOf("w") != -1 || keys.indexOf("ArrowUp".toLowerCase()) != -1) { racket.dir.y += -1; } - if (keys.indexOf("s") != -1) { + if (keys.indexOf("s") != -1 || keys.indexOf("ArrowDown".toLowerCase()) != -1) { racket.dir.y += 1; } - racket.moveAndCollide(delta, [gc.wallTop, gc.wallBottom]); */ - + racket.moveAndCollide(delta, [gc.wallTop, gc.wallBottom]); } export {handleInput} diff --git a/src/client/pong.ts b/src/client/pong.ts index 2c9550d6..b1db0f1b 100644 --- a/src/client/pong.ts +++ b/src/client/pong.ts @@ -9,8 +9,7 @@ import {countdown} from "./utils.js"; import {socket} from "./ws.js"; socket; // no-op /* Keys - Racket 1: W/S - Racket 2: Up/Down + Racket: W/S OR Up/Down Grid On-Off: G */ @@ -42,7 +41,5 @@ function resumeGame() pong.interval = window.setInterval(gameLoop, c.gameLoopIntervalMS); } -///////////////////////////////////////////////////////////////////////////// -///////////////////////////////////////////////////////////////////////////// export {matchmaking, matchmakingComplete, startGame} diff --git a/src/client/ws.ts b/src/client/ws.ts index b299d12f..8add7081 100644 --- a/src/client/ws.ts +++ b/src/client/ws.ts @@ -5,6 +5,8 @@ import {matchmaking, matchmakingComplete, startGame} from "./pong.js"; import * as en from "../shared_js/enums.js" import { Racket } from "../shared_js/class/Rectangle.js"; import { sleep } from "./utils.js"; +import * as c from "./constants.js" +import {soundRoblox} from "./audio.js" const wsPort = 8042; const wsUrl = "ws://" + document.location.hostname + ":" + wsPort + "/pong"; @@ -88,6 +90,12 @@ async function gameUpdate(data: ev.EventGameUpdate) function scoreUpdate(data: ev.EventScoreUpdate) { + 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; } diff --git a/src/server/server.ts b/src/server/server.ts index 5df88098..aa129cdb 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -23,9 +23,12 @@ const server = http.createServer((req, res) => { if (path.extname(filename) === ".html") { res.writeHead(200, {"Content-Type": "text/html"}); } - if (path.extname(filename) === ".js") { + else if (path.extname(filename) === ".js") { res.writeHead(200, {"Content-Type": "application/javascript"}); } + else if (path.extname(filename) === ".mp3") { + res.writeHead(200, {"Content-Type": "audio/mpeg"}); + } res.write(data); return res.end(); }); diff --git a/src/shared_js/class/Rectangle.ts b/src/shared_js/class/Rectangle.ts index 5049403a..35352b70 100644 --- a/src/shared_js/class/Rectangle.ts +++ b/src/shared_js/class/Rectangle.ts @@ -69,6 +69,9 @@ class Ball extends MovingRectangle { super(pos, size, size, baseSpeed); } bounce(collider?: Rectangle) { + this._bounceAlgo(collider); + } + protected _bounceAlgo(collider?: Rectangle) { /* Could be more generic, but testing only Racket is enough, because in Pong collider can only be Racket or Wall. */ if (collider instanceof Racket) { @@ -87,10 +90,13 @@ class Ball extends MovingRectangle { this.move(delta); } } - private _bounceWall() { // Should be enough for Wall + protected _bounceWall() { // Should be enough for Wall this.dir.y = this.dir.y * -1; } - private _bouncePlayer(collider: Racket) { // WIP + protected _bouncePlayer(collider: Racket) { + this._bouncePlayerAlgo(collider); + } + protected _bouncePlayerAlgo(collider: Racket) { // WIP // Bounce for Racket need to be more complexe than this this.speed += this.baseSpeed/20; this.dir.x = this.dir.x * -1; diff --git a/www/sound/pong/0.mp3 b/www/sound/pong/0.mp3 new file mode 100644 index 00000000..14f66a64 Binary files /dev/null and b/www/sound/pong/0.mp3 differ diff --git a/www/sound/pong/1.mp3 b/www/sound/pong/1.mp3 new file mode 100644 index 00000000..dae74cab Binary files /dev/null and b/www/sound/pong/1.mp3 differ diff --git a/www/sound/pong/10.mp3 b/www/sound/pong/10.mp3 new file mode 100644 index 00000000..f70de101 Binary files /dev/null and b/www/sound/pong/10.mp3 differ diff --git a/www/sound/pong/11.mp3 b/www/sound/pong/11.mp3 new file mode 100644 index 00000000..e64aa124 Binary files /dev/null and b/www/sound/pong/11.mp3 differ diff --git a/www/sound/pong/12.mp3 b/www/sound/pong/12.mp3 new file mode 100644 index 00000000..47c75bd6 Binary files /dev/null and b/www/sound/pong/12.mp3 differ diff --git a/www/sound/pong/13.mp3 b/www/sound/pong/13.mp3 new file mode 100644 index 00000000..724e8b6e Binary files /dev/null and b/www/sound/pong/13.mp3 differ diff --git a/www/sound/pong/14.mp3 b/www/sound/pong/14.mp3 new file mode 100644 index 00000000..4b24e4ad Binary files /dev/null and b/www/sound/pong/14.mp3 differ diff --git a/www/sound/pong/15.mp3 b/www/sound/pong/15.mp3 new file mode 100644 index 00000000..8bee98e0 Binary files /dev/null and b/www/sound/pong/15.mp3 differ diff --git a/www/sound/pong/16.mp3 b/www/sound/pong/16.mp3 new file mode 100644 index 00000000..20e994f2 Binary files /dev/null and b/www/sound/pong/16.mp3 differ diff --git a/www/sound/pong/17.mp3 b/www/sound/pong/17.mp3 new file mode 100644 index 00000000..c78ff6f9 Binary files /dev/null and b/www/sound/pong/17.mp3 differ diff --git a/www/sound/pong/18.mp3 b/www/sound/pong/18.mp3 new file mode 100644 index 00000000..ce844dbe Binary files /dev/null and b/www/sound/pong/18.mp3 differ diff --git a/www/sound/pong/19.mp3 b/www/sound/pong/19.mp3 new file mode 100644 index 00000000..884e9ec2 Binary files /dev/null and b/www/sound/pong/19.mp3 differ diff --git a/www/sound/pong/2.mp3 b/www/sound/pong/2.mp3 new file mode 100644 index 00000000..5286a9fb Binary files /dev/null and b/www/sound/pong/2.mp3 differ diff --git a/www/sound/pong/20.mp3 b/www/sound/pong/20.mp3 new file mode 100644 index 00000000..16a10734 Binary files /dev/null and b/www/sound/pong/20.mp3 differ diff --git a/www/sound/pong/21.mp3 b/www/sound/pong/21.mp3 new file mode 100644 index 00000000..3a7ab30d Binary files /dev/null and b/www/sound/pong/21.mp3 differ diff --git a/www/sound/pong/22.mp3 b/www/sound/pong/22.mp3 new file mode 100644 index 00000000..60be1f14 Binary files /dev/null and b/www/sound/pong/22.mp3 differ diff --git a/www/sound/pong/23.mp3 b/www/sound/pong/23.mp3 new file mode 100644 index 00000000..000da41c Binary files /dev/null and b/www/sound/pong/23.mp3 differ diff --git a/www/sound/pong/24.mp3 b/www/sound/pong/24.mp3 new file mode 100644 index 00000000..94d61a12 Binary files /dev/null and b/www/sound/pong/24.mp3 differ diff --git a/www/sound/pong/25.mp3 b/www/sound/pong/25.mp3 new file mode 100644 index 00000000..0b724c06 Binary files /dev/null and b/www/sound/pong/25.mp3 differ diff --git a/www/sound/pong/26.mp3 b/www/sound/pong/26.mp3 new file mode 100644 index 00000000..bb5518fb Binary files /dev/null and b/www/sound/pong/26.mp3 differ diff --git a/www/sound/pong/27.mp3 b/www/sound/pong/27.mp3 new file mode 100644 index 00000000..ebe5bdc1 Binary files /dev/null and b/www/sound/pong/27.mp3 differ diff --git a/www/sound/pong/28.mp3 b/www/sound/pong/28.mp3 new file mode 100644 index 00000000..2c265d07 Binary files /dev/null and b/www/sound/pong/28.mp3 differ diff --git a/www/sound/pong/29.mp3 b/www/sound/pong/29.mp3 new file mode 100644 index 00000000..7673dea9 Binary files /dev/null and b/www/sound/pong/29.mp3 differ diff --git a/www/sound/pong/3.mp3 b/www/sound/pong/3.mp3 new file mode 100644 index 00000000..91ca27d4 Binary files /dev/null and b/www/sound/pong/3.mp3 differ diff --git a/www/sound/pong/30.mp3 b/www/sound/pong/30.mp3 new file mode 100644 index 00000000..bbc57176 Binary files /dev/null and b/www/sound/pong/30.mp3 differ diff --git a/www/sound/pong/31.mp3 b/www/sound/pong/31.mp3 new file mode 100644 index 00000000..a0da9471 Binary files /dev/null and b/www/sound/pong/31.mp3 differ diff --git a/www/sound/pong/32.mp3 b/www/sound/pong/32.mp3 new file mode 100644 index 00000000..72e092ed Binary files /dev/null and b/www/sound/pong/32.mp3 differ diff --git a/www/sound/pong/4.mp3 b/www/sound/pong/4.mp3 new file mode 100644 index 00000000..c78bcbc4 Binary files /dev/null and b/www/sound/pong/4.mp3 differ diff --git a/www/sound/pong/5.mp3 b/www/sound/pong/5.mp3 new file mode 100644 index 00000000..50d7f57a Binary files /dev/null and b/www/sound/pong/5.mp3 differ diff --git a/www/sound/pong/6.mp3 b/www/sound/pong/6.mp3 new file mode 100644 index 00000000..dd3b16eb Binary files /dev/null and b/www/sound/pong/6.mp3 differ diff --git a/www/sound/pong/7.mp3 b/www/sound/pong/7.mp3 new file mode 100644 index 00000000..a721426b Binary files /dev/null and b/www/sound/pong/7.mp3 differ diff --git a/www/sound/pong/8.mp3 b/www/sound/pong/8.mp3 new file mode 100644 index 00000000..a0a6d907 Binary files /dev/null and b/www/sound/pong/8.mp3 differ diff --git a/www/sound/pong/9.mp3 b/www/sound/pong/9.mp3 new file mode 100644 index 00000000..79555530 Binary files /dev/null and b/www/sound/pong/9.mp3 differ diff --git a/www/sound/roblox-oof.mp3 b/www/sound/roblox-oof.mp3 new file mode 100644 index 00000000..79bd139c Binary files /dev/null and b/www/sound/roblox-oof.mp3 differ