aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--game.js74
-rwxr-xr-xserver.js31
-rw-r--r--snd/bounce.mp3bin0 -> 22151 bytes
-rw-r--r--snd/bounce.oggbin0 -> 5420 bytes
-rw-r--r--snd/bounce.wavbin0 -> 235610 bytes
-rw-r--r--snd/join.mp3bin0 -> 22151 bytes
-rw-r--r--snd/join.oggbin0 -> 11254 bytes
-rw-r--r--snd/join.wavbin0 -> 235610 bytes
-rw-r--r--snd/leave.mp3bin0 -> 22151 bytes
-rw-r--r--snd/leave.oggbin0 -> 8629 bytes
-rw-r--r--snd/leave.wavbin0 -> 235610 bytes
-rw-r--r--snd/miss.mp3bin0 -> 22151 bytes
-rw-r--r--snd/miss.oggbin0 -> 7142 bytes
-rw-r--r--snd/miss.wavbin0 -> 235610 bytes
-rw-r--r--snd/ready.mp3bin0 -> 22151 bytes
-rw-r--r--snd/ready.oggbin0 -> 5339 bytes
-rw-r--r--snd/ready.wavbin0 -> 235610 bytes
-rw-r--r--snd/samples.mmpzbin0 -> 2155 bytes
-rw-r--r--snd/score.mp3bin0 -> 22151 bytes
-rw-r--r--snd/score.oggbin0 -> 7494 bytes
-rw-r--r--snd/score.wavbin0 -> 235610 bytes
-rw-r--r--snd/start.mp3bin0 -> 22151 bytes
-rw-r--r--snd/start.oggbin0 -> 7020 bytes
-rw-r--r--snd/start.wavbin0 -> 235610 bytes
24 files changed, 92 insertions, 13 deletions
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
--- /dev/null
+++ b/snd/bounce.mp3
Binary files differ
diff --git a/snd/bounce.ogg b/snd/bounce.ogg
new file mode 100644
index 0000000..3329f05
--- /dev/null
+++ b/snd/bounce.ogg
Binary files differ
diff --git a/snd/bounce.wav b/snd/bounce.wav
new file mode 100644
index 0000000..a87491c
--- /dev/null
+++ b/snd/bounce.wav
Binary files differ
diff --git a/snd/join.mp3 b/snd/join.mp3
new file mode 100644
index 0000000..61c8f86
--- /dev/null
+++ b/snd/join.mp3
Binary files differ
diff --git a/snd/join.ogg b/snd/join.ogg
new file mode 100644
index 0000000..fb43a9d
--- /dev/null
+++ b/snd/join.ogg
Binary files differ
diff --git a/snd/join.wav b/snd/join.wav
new file mode 100644
index 0000000..8acd95c
--- /dev/null
+++ b/snd/join.wav
Binary files differ
diff --git a/snd/leave.mp3 b/snd/leave.mp3
new file mode 100644
index 0000000..c7a2e03
--- /dev/null
+++ b/snd/leave.mp3
Binary files differ
diff --git a/snd/leave.ogg b/snd/leave.ogg
new file mode 100644
index 0000000..9a0fb8b
--- /dev/null
+++ b/snd/leave.ogg
Binary files differ
diff --git a/snd/leave.wav b/snd/leave.wav
new file mode 100644
index 0000000..90048eb
--- /dev/null
+++ b/snd/leave.wav
Binary files differ
diff --git a/snd/miss.mp3 b/snd/miss.mp3
new file mode 100644
index 0000000..c1ea4cc
--- /dev/null
+++ b/snd/miss.mp3
Binary files differ
diff --git a/snd/miss.ogg b/snd/miss.ogg
new file mode 100644
index 0000000..7d396c6
--- /dev/null
+++ b/snd/miss.ogg
Binary files differ
diff --git a/snd/miss.wav b/snd/miss.wav
new file mode 100644
index 0000000..87ac06a
--- /dev/null
+++ b/snd/miss.wav
Binary files differ
diff --git a/snd/ready.mp3 b/snd/ready.mp3
new file mode 100644
index 0000000..515eb98
--- /dev/null
+++ b/snd/ready.mp3
Binary files differ
diff --git a/snd/ready.ogg b/snd/ready.ogg
new file mode 100644
index 0000000..3eccf64
--- /dev/null
+++ b/snd/ready.ogg
Binary files differ
diff --git a/snd/ready.wav b/snd/ready.wav
new file mode 100644
index 0000000..1b6a7ec
--- /dev/null
+++ b/snd/ready.wav
Binary files differ
diff --git a/snd/samples.mmpz b/snd/samples.mmpz
new file mode 100644
index 0000000..d59a072
--- /dev/null
+++ b/snd/samples.mmpz
Binary files differ
diff --git a/snd/score.mp3 b/snd/score.mp3
new file mode 100644
index 0000000..ce7f5cc
--- /dev/null
+++ b/snd/score.mp3
Binary files differ
diff --git a/snd/score.ogg b/snd/score.ogg
new file mode 100644
index 0000000..eda0b2d
--- /dev/null
+++ b/snd/score.ogg
Binary files differ
diff --git a/snd/score.wav b/snd/score.wav
new file mode 100644
index 0000000..a9bbf4b
--- /dev/null
+++ b/snd/score.wav
Binary files differ
diff --git a/snd/start.mp3 b/snd/start.mp3
new file mode 100644
index 0000000..3624e3e
--- /dev/null
+++ b/snd/start.mp3
Binary files differ
diff --git a/snd/start.ogg b/snd/start.ogg
new file mode 100644
index 0000000..dd10bb2
--- /dev/null
+++ b/snd/start.ogg
Binary files differ
diff --git a/snd/start.wav b/snd/start.wav
new file mode 100644
index 0000000..54301b7
--- /dev/null
+++ b/snd/start.wav
Binary files differ