-
Notifications
You must be signed in to change notification settings - Fork 6
Gestion/Envoie des coordonnées depuis le serveur #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,10 +35,10 @@ document.addEventListener('DOMContentLoaded', function () { | |
| }); | ||
|
|
||
| server.on('new_snake', function(data){ | ||
| clientLocaleSnake = board.newSnake(data.x, data.y, data.name); | ||
| clientLocaleSnake = board.newSnake(data.x, data.y, data.name, data.id); | ||
| }); | ||
| server.on('joinGame', function(apples){ | ||
| apples.forEach((apple) => { | ||
| apples.forEach(apple => { | ||
| let drawApple = board.newApple(apple.x, apple.y); | ||
| drawApple.draw(); | ||
| }); | ||
|
|
@@ -53,16 +53,30 @@ document.addEventListener('DOMContentLoaded', function () { | |
| }); | ||
| }); | ||
|
|
||
| //server.sendDeleteUser(); | ||
| server.sendMove(); | ||
|
|
||
| //server.sendAppleEaten(x, y); | ||
|
|
||
| board.render(); | ||
| board.on('appleEaten', function(position){ | ||
| server.sendAppleEaten(position); | ||
| }); | ||
|
|
||
| server.on('sendPositions', function(snakes, apples){ | ||
| snakes.forEach(snake => { | ||
| var snakeData = board.snakes.find(function (data) {snake.id === data.id;}); | ||
| if(!snakeData){ | ||
| console.log(snakeData); | ||
| board.newSnake(snake.x, snake.y, snake.name, snake.id); | ||
| }else{ | ||
| console.log('màj'); | ||
| snake.x = laonsaitpas.x; | ||
| snake.y = laonsaitpas.y; // trouver l'ordre à faire, pour créer les serpents non existant, et màj les serpents déjà dans board.snake | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs fixing |
||
| } | ||
| }); | ||
|
|
||
| board.render(snakes, apples); | ||
| }); | ||
|
|
||
| server.on('disconnect', function(){ | ||
| board.stopRendering(); | ||
| displayMessage("Warning !", "You have been disconnected from the server ! Check your internet connection !"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,10 @@ socket.on('joinGame', function(apples) { | |
| serverObject.emit('joinGame', apples); | ||
| }); | ||
|
|
||
| socket.on('sendPositions', function(snakes, apples) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use a verb to name an event |
||
| serverObject.emit('sendPositions', snakes, apples); | ||
| }); | ||
|
|
||
| socket.on('appleEaten', function(data) { | ||
| console.log('sendto'); | ||
| serverObject.emit('new_apple', data); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import * as constant from '../client/js/constant'; | |
| import Board from '../client/js/board.js'; | ||
| import Snake from '../client/js/snake.js'; | ||
| import Scoreboard from '../client/js/scoreboard.js'; | ||
| import identifier from 'identifier'; | ||
|
|
||
| var b = new Board(); | ||
| let inProgressGame = false; | ||
|
|
@@ -44,31 +45,46 @@ io.on('connection', function(socket) { | |
|
|
||
| socket.on('snakeNew', name => { | ||
|
|
||
| var id = identifier(constant.ID_SIZE); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a case where you don't need a constant and it can just be hardcoded |
||
| console.log(id); | ||
|
|
||
| let long = Math.floor(Math.random() * (constant.CANVAS_WIDTH/constant.GRID_SIZE)) * constant.GRID_SIZE; | ||
| let lat = Math.floor(Math.random() * (constant.CANVAS_HEIGHT/constant.GRID_SIZE)) * constant.GRID_SIZE; | ||
| let snake = b.newSnake(long, lat, name); | ||
| let snake = b.newSnake(long, lat, name, id); | ||
| console.log("snake : " + snake.name); | ||
| io.emit('new_snake', snake); | ||
|
|
||
| if(inProgressGame){ | ||
| io.emit('joinGame', b.apples); | ||
| } | ||
| }); | ||
|
|
||
| socket.on('changeDirection', (data) => { | ||
| io.emit('setDirection', data); | ||
| }); | ||
|
|
||
| if(!inProgressGame){ | ||
| inProgressGame = true; | ||
| setInterval(function() { | ||
|
|
||
| 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); | ||
| } | ||
| io.emit('sendPositions', b.snakes, b.apples); | ||
| }, 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; | ||
|
|
@@ -84,4 +100,4 @@ io.on('connection', function(socket) { | |
|
|
||
| http.listen(3000, () => { | ||
| console.log('listening on *:3000'); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the board already has the snakes and apples as property. It shouldn't need them as arguments