diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2018-06-25 17:08:31 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2018-06-25 17:08:31 +0200 |
commit | 335c0a843636cb4318e71afbf0ada693fb52957c (patch) | |
tree | 5938fd492b7c0046b80d441a53d90d559f299b21 | |
parent | 4acd53db27b652834ed096307b3561b880ebbd72 (diff) |
Noise
-rw-r--r-- | game.js | 74 | ||||
-rwxr-xr-x | server.js | 31 | ||||
-rw-r--r-- | snd/bounce.mp3 | bin | 0 -> 22151 bytes | |||
-rw-r--r-- | snd/bounce.ogg | bin | 0 -> 5420 bytes | |||
-rw-r--r-- | snd/bounce.wav | bin | 0 -> 235610 bytes | |||
-rw-r--r-- | snd/join.mp3 | bin | 0 -> 22151 bytes | |||
-rw-r--r-- | snd/join.ogg | bin | 0 -> 11254 bytes | |||
-rw-r--r-- | snd/join.wav | bin | 0 -> 235610 bytes | |||
-rw-r--r-- | snd/leave.mp3 | bin | 0 -> 22151 bytes | |||
-rw-r--r-- | snd/leave.ogg | bin | 0 -> 8629 bytes | |||
-rw-r--r-- | snd/leave.wav | bin | 0 -> 235610 bytes | |||
-rw-r--r-- | snd/miss.mp3 | bin | 0 -> 22151 bytes | |||
-rw-r--r-- | snd/miss.ogg | bin | 0 -> 7142 bytes | |||
-rw-r--r-- | snd/miss.wav | bin | 0 -> 235610 bytes | |||
-rw-r--r-- | snd/ready.mp3 | bin | 0 -> 22151 bytes | |||
-rw-r--r-- | snd/ready.ogg | bin | 0 -> 5339 bytes | |||
-rw-r--r-- | snd/ready.wav | bin | 0 -> 235610 bytes | |||
-rw-r--r-- | snd/samples.mmpz | bin | 0 -> 2155 bytes | |||
-rw-r--r-- | snd/score.mp3 | bin | 0 -> 22151 bytes | |||
-rw-r--r-- | snd/score.ogg | bin | 0 -> 7494 bytes | |||
-rw-r--r-- | snd/score.wav | bin | 0 -> 235610 bytes | |||
-rw-r--r-- | snd/start.mp3 | bin | 0 -> 22151 bytes | |||
-rw-r--r-- | snd/start.ogg | bin | 0 -> 7020 bytes | |||
-rw-r--r-- | snd/start.wav | bin | 0 -> 235610 bytes |
24 files changed, 92 insertions, 13 deletions
@@ -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(); }); @@ -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 Binary files differnew file mode 100644 index 0000000..4149105 --- /dev/null +++ b/snd/bounce.mp3 diff --git a/snd/bounce.ogg b/snd/bounce.ogg Binary files differnew file mode 100644 index 0000000..3329f05 --- /dev/null +++ b/snd/bounce.ogg diff --git a/snd/bounce.wav b/snd/bounce.wav Binary files differnew file mode 100644 index 0000000..a87491c --- /dev/null +++ b/snd/bounce.wav diff --git a/snd/join.mp3 b/snd/join.mp3 Binary files differnew file mode 100644 index 0000000..61c8f86 --- /dev/null +++ b/snd/join.mp3 diff --git a/snd/join.ogg b/snd/join.ogg Binary files differnew file mode 100644 index 0000000..fb43a9d --- /dev/null +++ b/snd/join.ogg diff --git a/snd/join.wav b/snd/join.wav Binary files differnew file mode 100644 index 0000000..8acd95c --- /dev/null +++ b/snd/join.wav diff --git a/snd/leave.mp3 b/snd/leave.mp3 Binary files differnew file mode 100644 index 0000000..c7a2e03 --- /dev/null +++ b/snd/leave.mp3 diff --git a/snd/leave.ogg b/snd/leave.ogg Binary files differnew file mode 100644 index 0000000..9a0fb8b --- /dev/null +++ b/snd/leave.ogg diff --git a/snd/leave.wav b/snd/leave.wav Binary files differnew file mode 100644 index 0000000..90048eb --- /dev/null +++ b/snd/leave.wav diff --git a/snd/miss.mp3 b/snd/miss.mp3 Binary files differnew file mode 100644 index 0000000..c1ea4cc --- /dev/null +++ b/snd/miss.mp3 diff --git a/snd/miss.ogg b/snd/miss.ogg Binary files differnew file mode 100644 index 0000000..7d396c6 --- /dev/null +++ b/snd/miss.ogg diff --git a/snd/miss.wav b/snd/miss.wav Binary files differnew file mode 100644 index 0000000..87ac06a --- /dev/null +++ b/snd/miss.wav diff --git a/snd/ready.mp3 b/snd/ready.mp3 Binary files differnew file mode 100644 index 0000000..515eb98 --- /dev/null +++ b/snd/ready.mp3 diff --git a/snd/ready.ogg b/snd/ready.ogg Binary files differnew file mode 100644 index 0000000..3eccf64 --- /dev/null +++ b/snd/ready.ogg diff --git a/snd/ready.wav b/snd/ready.wav Binary files differnew file mode 100644 index 0000000..1b6a7ec --- /dev/null +++ b/snd/ready.wav diff --git a/snd/samples.mmpz b/snd/samples.mmpz Binary files differnew file mode 100644 index 0000000..d59a072 --- /dev/null +++ b/snd/samples.mmpz diff --git a/snd/score.mp3 b/snd/score.mp3 Binary files differnew file mode 100644 index 0000000..ce7f5cc --- /dev/null +++ b/snd/score.mp3 diff --git a/snd/score.ogg b/snd/score.ogg Binary files differnew file mode 100644 index 0000000..eda0b2d --- /dev/null +++ b/snd/score.ogg diff --git a/snd/score.wav b/snd/score.wav Binary files differnew file mode 100644 index 0000000..a9bbf4b --- /dev/null +++ b/snd/score.wav diff --git a/snd/start.mp3 b/snd/start.mp3 Binary files differnew file mode 100644 index 0000000..3624e3e --- /dev/null +++ b/snd/start.mp3 diff --git a/snd/start.ogg b/snd/start.ogg Binary files differnew file mode 100644 index 0000000..dd10bb2 --- /dev/null +++ b/snd/start.ogg diff --git a/snd/start.wav b/snd/start.wav Binary files differnew file mode 100644 index 0000000..54301b7 --- /dev/null +++ b/snd/start.wav |