Keep track of score and reset game after score

Client: only set direction on change
This commit is contained in:
2022-11-04 13:00:54 +01:00
parent cd7a1f15a9
commit 2fb395a8d4
2 changed files with 49 additions and 22 deletions

View File

@ -86,10 +86,6 @@ function movePaddle(direction) {
});
}
function moveUp() { return movePaddle(-1); }
function stopPaddle() { return movePaddle(0); }
function moveDown() { return movePaddle(1); }
// Create the application helper and add its render target to the page
let app = new PIXI.Application({ width: 1000, height: 500 });
getElement('canvas-container').appendChild(app.view);
@ -124,18 +120,25 @@ connection.on("ReceiveGameState", function (state) {
const keyEvent = (function () {
var upPressed = false;
var downPressed = false;
var direction = 0;
function setDirection(dir) {
if (direction != dir) {
direction = dir;
movePaddle(direction);
}
}
function moveUpdated() {
if (upPressed == downPressed) stopPaddle();
else if (upPressed) moveUp();
else if (downPressed) moveDown();
if (upPressed == downPressed) setDirection(0);
else if (upPressed) setDirection(-1);
else if (downPressed) setDirection(1);
else console.error("unknown move!");
}
function handler(event) {
if (event.repeat) return;
if (event.path.indexOf(document.body) != 0) return; // only use key if it was pressed on empty space
var pressed = event.type == "keyup";
var pressed = event.type == "keydown";
// W Key is 87, Up arrow is 87
// S Key is 83, Down arrow is 40
switch (event.keyCode) {
@ -150,6 +153,9 @@ const keyEvent = (function () {
default: return;
}
if (event.repeat) return;
if (event.path.indexOf(document.body) != 0) return; // only use key if it was pressed on empty space
event.preventDefault();
moveUpdated();
}