aboutsummaryrefslogtreecommitdiff
path: root/game.js
diff options
context:
space:
mode:
Diffstat (limited to 'game.js')
-rw-r--r--game.js31
1 files changed, 30 insertions, 1 deletions
diff --git a/game.js b/game.js
index 4be774a..77d4151 100644
--- a/game.js
+++ b/game.js
@@ -85,6 +85,16 @@ function displayStatus(msg) {
document.getElementById("status").innerHTML = msg;
}
+function displayScore(score) {
+ document.getElementById("score").classList.remove("invisible");
+ document.getElementById("score-left").innerHTML = score[0];
+ document.getElementById("score-right").innerHTML = score[1];
+
+ setTimeout(function() {
+ document.getElementById("score").classList.add("invisible");
+ }, 2000);
+}
+
function openGame() {
socket = io();
socket.on("redirect", function(url) {
@@ -95,6 +105,10 @@ function openGame() {
displayStatus(msg);
});
+ socket.on("score", function(score) {
+ displayScore(score);
+ });
+
socket.on("serve", function(side, x, y) {
playing = true;
flip = side == "right";
@@ -151,7 +165,12 @@ function stopGraphicsLoop() {
function startPhysicsLoop() {
var interval = 1 / 120;
physicsLoopId = setInterval(function() {
- advancePhysics(interval);
+ if (advancePhysics(interval)) {
+ socket.emit("ballvec", ballX, ballY, ballVX, ballVY);
+ }
+ if (ballX < -ballRadius) {
+ socket.emit("ballout");
+ }
}, interval * 1000);
}
@@ -168,7 +187,10 @@ function initPhysics() {
padVel = pad2Vel = 0;
}
+// Returns whether a new ballvec should be sent
function advancePhysics(deltaT) {
+ var sendBallvec = false;
+
var newballX = ballX + ballVX * deltaT;
var newballY = ballY + ballVY * deltaT;
var newpadPos = padPos + padVel * deltaT;
@@ -190,6 +212,7 @@ 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
} else if (newballX >= 1 - (padX + padWidth + ballRadius) &&
Math.abs(newballY - pad2Pos) < padHeight / 2 + ballRadius) {
t = (1 - (padX + padWidth + ballRadius) - ballX) / ballVX;
@@ -201,10 +224,16 @@ function advancePhysics(deltaT) {
ballVX = -ballVX;
}
+ if ((ballX < 0.5) ^ (newballX < 0.5)) {
+ sendBallvec = true;
+ }
+
ballX = newballX;
ballY = newballY;
padPos = newpadPos;
pad2Pos = newpad2Pos;
+
+ return sendBallvec;
}
function setupBindings() {