diff --git a/client/js/board.js b/client/js/board.js index 36b0721..7f5c86e 100644 --- a/client/js/board.js +++ b/client/js/board.js @@ -32,11 +32,11 @@ export default class Board extends EventEmitter{ newSnake(x, y, name) { if(this.snakes.length < 10){ let snake = new Snake(this.context, x, y, this.getAvailableColor(), name); - + if (this.scoreboard) { this.scoreboard.addPlayer(snake); } - + snake.draw(); this.snakes.push(snake); @@ -78,24 +78,9 @@ export default class Board extends EventEmitter{ createScoreboard() { this.scoreboard = new Scoreboard(); - this.scoreboard.playersContainer.appendTo('#scoreboard'); } - render() { - this.intervalId = setInterval(() => { - this.snakes.forEach(snake => { - if(!snake.dead) { - snake.move(this); - } - }); - this.scoreboard.updateScores(this.snakes, this.clientLocalSnake); - this.checkSnakeSelfCollision(); - this.checkCollisionWithApples(); - // END TEMP - }, constant.DELAY); - } - stopRendering(){ clearInterval(this.intervalId); } @@ -123,15 +108,15 @@ export default class Board extends EventEmitter{ } checkCollisionWithApples() { - this.snakes.forEach((snake, i) => { + this.snakes.forEach((snake) => { let firstBodyPart = snake.bodyParts[0]; this.apples.forEach((apple, index) => { if (firstBodyPart.x < apple.x + apple.radius * 2 && - firstBodyPart.x + firstBodyPart.width > apple.x && - firstBodyPart.y < apple.y + apple.radius * 2 && - firstBodyPart.height + firstBodyPart.y > apple.y) { + firstBodyPart.x + firstBodyPart.width > apple.x && + firstBodyPart.y < apple.y + apple.radius * 2 && + firstBodyPart.height + firstBodyPart.y > apple.y) { this.apples.splice(index, 1); snake.addScore(); diff --git a/client/js/displayDisconnectMessage.js b/client/js/displayDisconnectMessage.js index 379f95f..82a5191 100644 --- a/client/js/displayDisconnectMessage.js +++ b/client/js/displayDisconnectMessage.js @@ -3,6 +3,6 @@ import $ from 'jquery'; export default function displayDisconnectMessage() { - $("#message").html("

Warning !

You have been deconnected, please check your internet connection and try again

").addClass("active"); - $('body').addClass("blur"); + $("#message").html("

Warning !

You have been deconnected, please check your internet connection and try again

").addClass("active"); + $('body').addClass("blur"); } \ No newline at end of file diff --git a/client/js/main.js b/client/js/main.js index c115e1e..d463ad0 100644 --- a/client/js/main.js +++ b/client/js/main.js @@ -63,11 +63,13 @@ document.addEventListener('DOMContentLoaded', function () { //server.sendAppleEaten(x, y); - board.render(); + //board.render(); board.on('appleEaten', function(position){ server.sendAppleEaten(position); }); + + server.on('disconnect', function(){ board.stopRendering(); displayMessage("Warning !", "You have been disconnected from the server ! Check your internet connection !"); diff --git a/client/js/snake.js b/client/js/snake.js index 303bcbc..52bd45c 100644 --- a/client/js/snake.js +++ b/client/js/snake.js @@ -50,8 +50,6 @@ export default class Snake { lastBodyPart.y = 0; } - lastBodyPart.draw(); - this.moveBodyPartsInArray(); } @@ -68,8 +66,6 @@ export default class Snake { lastBodyPart.y = this.context.canvas.clientHeight - (this.height + constant.BODY_PART_MARGIN); } - lastBodyPart.draw(); - this.moveBodyPartsInArray(); } @@ -87,8 +83,6 @@ export default class Snake { lastBodyPart.x = this.context.canvas.clientWidth - (this.width + constant.BODY_PART_MARGIN); } - lastBodyPart.draw(); - this.moveBodyPartsInArray(); } @@ -106,8 +100,6 @@ export default class Snake { lastBodyPart.x = 0; } - lastBodyPart.draw(); - this.moveBodyPartsInArray(); } @@ -119,41 +111,22 @@ export default class Snake { this.bodyParts.splice(0, 0, this.bodyParts.splice(this.bodyParts.length - 1, 1)[0]); } - move(board) { - this.checkCollisionWithApples(board.apples); - + move() { + /*this.checkCollisionWithApples(board.apples);*/ if (this.direction === 'right') { - this.moveRight(); + return this.moveRight(); } else if (this.direction === 'down') { - this.moveDown(); + return this.moveDown(); } else if (this.direction === 'up') { - this.moveUp(); + return this.moveUp(); } else if (this.direction === 'left') { - this.moveLeft(); + return this.moveLeft(); } } - checkCollisionWithApples(apples) { - let firstBodyPart = this.bodyParts[0]; - - apples.forEach((apple, index) => { - if (firstBodyPart.x < apple.x + apple.radius * 2 && - firstBodyPart.x + firstBodyPart.width > apple.x && - firstBodyPart.y < apple.y + apple.radius * 2 && - firstBodyPart.height + firstBodyPart.y > apple.y) { - - apples.splice(index, 1); - this.addScore(); - - this.addBodyPart(); - - } - }); - } - remove() { this.bodyParts.forEach((bodyPart) => { bodyPart.remove(); diff --git a/server/index.js b/server/index.js index 870272e..e4d7313 100644 --- a/server/index.js +++ b/server/index.js @@ -50,21 +50,30 @@ io.on('connection', function(socket) { console.log("snake : " + snake.name); io.emit('new_snake', snake); }); - + socket.on('changeDirection', (data) => { io.emit('setDirection', data); }); if(!inProgressGame){ + setInterval(function() { + b.checkSnakeSelfCollision(); + b.checkCollisionWithApples(); + b.snakes.forEach(snake => { + if(!snake.dead) { + snake.move(); + } + }); + if(b.apples.length < constant.DEFAULT_APPLES_NUMBER){ + let apple = b.generateApple(); + io.emit('new_apple', apple); + } + }, constant.DELAY); + setInterval(function() { inProgressGame = true; io.emit('start', 'Démarrage de la partie'); console.log('Démarrage de la partie'); - - while(b.apples.length < constant.DEFAULT_APPLES_NUMBER){ - let apple = b.generateApple(); - io.emit('new_apple', apple); - } setTimeout(function() { inProgressGame = false; @@ -85,4 +94,4 @@ io.on('connection', function(socket) { http.listen(3000, () => { console.log('listening on *:3000'); -}); +}); \ No newline at end of file