From 335c0a843636cb4318e71afbf0ada693fb52957c Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 25 Jun 2018 17:08:31 +0200 Subject: Noise --- game.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++------ server.js | 31 ++++++++++++++++++----- snd/bounce.mp3 | Bin 0 -> 22151 bytes snd/bounce.ogg | Bin 0 -> 5420 bytes snd/bounce.wav | Bin 0 -> 235610 bytes snd/join.mp3 | Bin 0 -> 22151 bytes snd/join.ogg | Bin 0 -> 11254 bytes snd/join.wav | Bin 0 -> 235610 bytes snd/leave.mp3 | Bin 0 -> 22151 bytes snd/leave.ogg | Bin 0 -> 8629 bytes snd/leave.wav | Bin 0 -> 235610 bytes snd/miss.mp3 | Bin 0 -> 22151 bytes snd/miss.ogg | Bin 0 -> 7142 bytes snd/miss.wav | Bin 0 -> 235610 bytes snd/ready.mp3 | Bin 0 -> 22151 bytes snd/ready.ogg | Bin 0 -> 5339 bytes snd/ready.wav | Bin 0 -> 235610 bytes snd/samples.mmpz | Bin 0 -> 2155 bytes snd/score.mp3 | Bin 0 -> 22151 bytes snd/score.ogg | Bin 0 -> 7494 bytes snd/score.wav | Bin 0 -> 235610 bytes snd/start.mp3 | Bin 0 -> 22151 bytes snd/start.ogg | Bin 0 -> 7020 bytes snd/start.wav | Bin 0 -> 235610 bytes 24 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 snd/bounce.mp3 create mode 100644 snd/bounce.ogg create mode 100644 snd/bounce.wav create mode 100644 snd/join.mp3 create mode 100644 snd/join.ogg create mode 100644 snd/join.wav create mode 100644 snd/leave.mp3 create mode 100644 snd/leave.ogg create mode 100644 snd/leave.wav create mode 100644 snd/miss.mp3 create mode 100644 snd/miss.ogg create mode 100644 snd/miss.wav create mode 100644 snd/ready.mp3 create mode 100644 snd/ready.ogg create mode 100644 snd/ready.wav create mode 100644 snd/samples.mmpz create mode 100644 snd/score.mp3 create mode 100644 snd/score.ogg create mode 100644 snd/score.wav create mode 100644 snd/start.mp3 create mode 100644 snd/start.ogg create mode 100644 snd/start.wav diff --git a/game.js b/game.js index 1d06a2e..c1083b4 100644 --- a/game.js +++ b/game.js @@ -109,12 +109,29 @@ function openGame() { displayScore(score); }); - socket.on("serve", function(side, x, y) { + socket.on("join", function() { + playSound("join"); + }); + + socket.on("leave", function() { + playSound("leave"); + }); + + socket.on("othermiss", function() { + playSound("score"); + }); + + socket.on("bounce", function() { + playSound("bounce"); + }); + + socket.on("serve", function(side, x, y, initServe) { playing = true; flip = side == "right"; ballX = x; ballY = y; padPos = pad2Pos = 0.5; redraw(); + if (!initServe) playSound("ready"); }); socket.on("stop", function() { @@ -130,6 +147,7 @@ function openGame() { ballVY = -0.5; startPhysicsLoop(); startGraphicsLoop(); + playSound("start"); }); socket.on("start", function() { @@ -138,6 +156,7 @@ function openGame() { ballVY = -0.5; startPhysicsLoop(); startGraphicsLoop(); + playSound("start"); }); socket.on("padvec", function(pos, vel) { @@ -170,12 +189,21 @@ function stopGraphicsLoop() { function startPhysicsLoop() { var interval = 1 / 120; physicsLoopId = setInterval(function() { - if (advancePhysics(interval)) { + var info = advancePhysics(interval); + if (info.bounce || info.otherBounce) { + playSound("bounce"); + } + if (info.bounce || info.ourSide) { socket.emit("ballvec", ballX, ballY, ballVX, ballVY); } + if (info.bounce) { + socket.emit("bounce"); + } + if (ballX < -ballRadius) { socket.emit("ballout"); stopPhysicsLoop(); + playSound("miss"); } }, interval * 1000); } @@ -193,9 +221,9 @@ function initPhysics() { padVel = pad2Vel = 0; } -// Returns whether a new ballvec should be sent +// Returns {bounce, otherBounce, ourSide : Bool} function advancePhysics(deltaT) { - var sendBallvec = false; + var ret = {bounce: false, otherBounce: false, ourSide: false}; var newballX = ballX + ballVX * deltaT; var newballY = ballY + ballVY * deltaT; @@ -218,10 +246,11 @@ function advancePhysics(deltaT) { if (newballX <= padX + padWidth + ballRadius && Math.abs(newballY - padPos) < padHeight / 2 + ballRadius) { t = (padX + padWidth + ballRadius - ballX) / ballVX; - sendBallvec = true; // rebound from *our* pad + ret.bounce = true; } else if (newballX >= 1 - (padX + padWidth + ballRadius) && Math.abs(newballY - pad2Pos) < padHeight / 2 + ballRadius) { t = (1 - (padX + padWidth + ballRadius) - ballX) / ballVX; + ret.otherBounce = true; } else { t = -1; } @@ -232,7 +261,7 @@ function advancePhysics(deltaT) { // If the ball has come to our side, send a ballvec if (ballX >= 0.5 && newballX < 0.5) { - sendBallvec = true; + ret.ourSide = true; } ballX = newballX; @@ -240,7 +269,36 @@ function advancePhysics(deltaT) { padPos = newpadPos; pad2Pos = newpad2Pos; - return sendBallvec; + return ret; +} + +var soundMap = {}; + +function preloadSounds() { + var names = ["bounce", "leave", "ready", "start", "join", "miss", "score"]; + var types = [["audio/ogg", "ogg"], ["audio/mpeg", "mp3"], ["audio/wav", "wav"]]; + for (var i = 0; i < names.length; i++) { + var audio = new Audio(); + for (var j = 0; j < types.length; j++) { + var source = document.createElement("source"); + source.type = types[j][0]; + source.src = "/snd/" + names[i] + "." + types[j][1]; + audio.appendChild(source); + } + soundMap[names[i]] = audio; + audio.preload = "auto"; + } +} + +var lastSoundStampMap = {}; + +function playSound(name) { + var now = new Date(); + console.log("playSound(\"" + name + "\")"); + if (lastSoundStampMap[name] == null || now - lastSoundStampMap[name] >= 500) { + soundMap[name].play(); + } + lastSoundStampMap[name] = now; } function setupBindings() { @@ -274,6 +332,8 @@ window.addEventListener("load", function() { setupBindings(); + preloadSounds(); + openGame(); }); diff --git a/server.js b/server.js index 73add35..08812a7 100755 --- a/server.js +++ b/server.js @@ -46,8 +46,15 @@ app.get("/game/:gameid", (req, res) => { res.sendFile(__dirname + "/game.html"); }); -app.get("/game.css", (req, res) => res.sendFile(__dirname + "/game.css")); -app.get("/game.js", (req, res) => res.sendFile(__dirname + "/game.js")); +const staticFiles = [ + "/game.css", + "/game.js", + /^\/snd\//, +]; + +app.get(staticFiles, (req, res) => { + res.sendFile(__dirname + req.path); +}); io.on("connection", (conn) => { let gameid, gameobj; @@ -65,10 +72,11 @@ io.on("connection", (conn) => { console.log(" serve"); gameid = id; gameobj.pl.push(conn); + gameobj.pl[0].emit("join"); gameobj.pl[0].emit("status", "Get ready..."); gameobj.pl[1].emit("status", "Get ready..."); - gameobj.pl[0].emit("serve", "left", 0.5, 0.5); - gameobj.pl[1].emit("serve", "right", 0.5, 0.5); + gameobj.pl[0].emit("serve", "left", 0.5, 0.5, true); + gameobj.pl[1].emit("serve", "right", 0.5, 0.5, true); gameobj.started = true; setTimeout(() => { if (!gameobj || gameobj.pl.length != 2) return; @@ -105,6 +113,7 @@ io.on("connection", (conn) => { for (const p of gameobj.pl) { if (p != conn) { p.emit("stop"); + p.emit("leave"); p.emit("status", "Other player left."); setTimeout(() => { p.emit("redirect", "/"); @@ -140,6 +149,15 @@ io.on("connection", (conn) => { } }); + conn.on("bounce", () => { + if (!gameobj) return; + for (const p of gameobj.pl) { + if (p != conn) { + p.emit("bounce"); + } + } + }); + conn.on("ballout", () => { if (!gameobj || gameobj.pl.length != 2) return; @@ -151,6 +169,7 @@ io.on("connection", (conn) => { for (let i = 0; i < gameobj.pl.length; i++) { if (gameobj.pl[i] != conn) { gameobj.score[i]++; + gameobj.pl[i].emit("othermiss"); } } @@ -161,8 +180,8 @@ io.on("connection", (conn) => { if (!gameobj || gameobj.pl.length != 2) return; gameobj.pl[0].emit("status", "Get ready..."); gameobj.pl[1].emit("status", "Get ready..."); - gameobj.pl[0].emit("serve", "left", 0.5, 0.5); - gameobj.pl[1].emit("serve", "right", 0.5, 0.5); + gameobj.pl[0].emit("serve", "left", 0.5, 0.5, false); + gameobj.pl[1].emit("serve", "right", 0.5, 0.5, false); setTimeout(() => { if (!gameobj || gameobj.pl.length != 2) return; diff --git a/snd/bounce.mp3 b/snd/bounce.mp3 new file mode 100644 index 0000000..4149105 Binary files /dev/null and b/snd/bounce.mp3 differ diff --git a/snd/bounce.ogg b/snd/bounce.ogg new file mode 100644 index 0000000..3329f05 Binary files /dev/null and b/snd/bounce.ogg differ diff --git a/snd/bounce.wav b/snd/bounce.wav new file mode 100644 index 0000000..a87491c Binary files /dev/null and b/snd/bounce.wav differ diff --git a/snd/join.mp3 b/snd/join.mp3 new file mode 100644 index 0000000..61c8f86 Binary files /dev/null and b/snd/join.mp3 differ diff --git a/snd/join.ogg b/snd/join.ogg new file mode 100644 index 0000000..fb43a9d Binary files /dev/null and b/snd/join.ogg differ diff --git a/snd/join.wav b/snd/join.wav new file mode 100644 index 0000000..8acd95c Binary files /dev/null and b/snd/join.wav differ diff --git a/snd/leave.mp3 b/snd/leave.mp3 new file mode 100644 index 0000000..c7a2e03 Binary files /dev/null and b/snd/leave.mp3 differ diff --git a/snd/leave.ogg b/snd/leave.ogg new file mode 100644 index 0000000..9a0fb8b Binary files /dev/null and b/snd/leave.ogg differ diff --git a/snd/leave.wav b/snd/leave.wav new file mode 100644 index 0000000..90048eb Binary files /dev/null and b/snd/leave.wav differ diff --git a/snd/miss.mp3 b/snd/miss.mp3 new file mode 100644 index 0000000..c1ea4cc Binary files /dev/null and b/snd/miss.mp3 differ diff --git a/snd/miss.ogg b/snd/miss.ogg new file mode 100644 index 0000000..7d396c6 Binary files /dev/null and b/snd/miss.ogg differ diff --git a/snd/miss.wav b/snd/miss.wav new file mode 100644 index 0000000..87ac06a Binary files /dev/null and b/snd/miss.wav differ diff --git a/snd/ready.mp3 b/snd/ready.mp3 new file mode 100644 index 0000000..515eb98 Binary files /dev/null and b/snd/ready.mp3 differ diff --git a/snd/ready.ogg b/snd/ready.ogg new file mode 100644 index 0000000..3eccf64 Binary files /dev/null and b/snd/ready.ogg differ diff --git a/snd/ready.wav b/snd/ready.wav new file mode 100644 index 0000000..1b6a7ec Binary files /dev/null and b/snd/ready.wav differ diff --git a/snd/samples.mmpz b/snd/samples.mmpz new file mode 100644 index 0000000..d59a072 Binary files /dev/null and b/snd/samples.mmpz differ diff --git a/snd/score.mp3 b/snd/score.mp3 new file mode 100644 index 0000000..ce7f5cc Binary files /dev/null and b/snd/score.mp3 differ diff --git a/snd/score.ogg b/snd/score.ogg new file mode 100644 index 0000000..eda0b2d Binary files /dev/null and b/snd/score.ogg differ diff --git a/snd/score.wav b/snd/score.wav new file mode 100644 index 0000000..a9bbf4b Binary files /dev/null and b/snd/score.wav differ diff --git a/snd/start.mp3 b/snd/start.mp3 new file mode 100644 index 0000000..3624e3e Binary files /dev/null and b/snd/start.mp3 differ diff --git a/snd/start.ogg b/snd/start.ogg new file mode 100644 index 0000000..dd10bb2 Binary files /dev/null and b/snd/start.ogg differ diff --git a/snd/start.wav b/snd/start.wav new file mode 100644 index 0000000..54301b7 Binary files /dev/null and b/snd/start.wav differ -- cgit v1.2.3-70-g09d2