diff options
Diffstat (limited to 'game.js')
-rw-r--r-- | game.js | 74 |
1 files changed, 67 insertions, 7 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(); }); |