From e07ee28d189a5aca115f8c8d394c8ce05bdaa7d9 Mon Sep 17 00:00:00 2001 From: Simon G Date: Sat, 21 Jan 2017 00:50:32 +0100 Subject: [PATCH] Made Snyc working again --- index.html | 125 ++++++++++++++++++++++++++++++++++------------------- index.js | 27 +++++++----- 2 files changed, 95 insertions(+), 57 deletions(-) diff --git a/index.html b/index.html index 61b06b0..a89a1a0 100644 --- a/index.html +++ b/index.html @@ -39,6 +39,10 @@ gameState = state; }); + socket.on('shootball', function(){ + shootBall(); + }); + /** Make Countdown serversided No cheating possible **/ socket.on('countdown', function(i){ text = 'Starting in ' + i; @@ -47,25 +51,28 @@ socket.on('start', function(){ leftOr = 'none'; rightOr = 'none'; - leftKeyPressed = false; - rightKeyPressed = false; leftY = 0; rightY = 0; leftV = 0; rightV = 0; + ballX = cube_width+ball_size; + ballY = cube_height/2; + ball_owner = 1; + ball_VX = 0; + ball_VY = 0; gameState = 2; }); - socket.on('setOrr', function(id, orr){ - console.log(id); - if(id == 1){ - leftOr = orr; - leftKeyPressed = true; + socket.on('changeV', function(pos, val, y){ + if(pos == 1){ + leftV = val; + leftY = y; } - if(id == 2){ - rightOr = orr; - rightKeyPressed = true;// TODO CHANGE! + if(pos == 2){ + rightV = val; + rightY = y; } + console.log(leftY); }); socket.on('test', function(arg){ @@ -84,8 +91,6 @@ var rightV = 0; var leftColor = 'red'; var rightColor = 'blue'; - var leftKeyPressed = false; - var rightKeyPressed = false; var gameState = 0; // 0 == Waiting for Players, 1 == Countdown, 2 == inGame, 3 == ??Win?? /** FPS Vars **/ @@ -94,7 +99,7 @@ fps; /** Global Configuration **/ - var localcoop = true, // Local Coop in 1 window + var localcoop = false, // Local Coop in 1 window width = $(window).width(), height = $(window).height(); @@ -264,61 +269,91 @@ $(window).on('keydown', function(e) { if(localcoop){ switch(e.keyCode) { - case 38 : if(leftY <= 0) break; leftV = 1; leftKeyPressed = true; break; - case 40 : if(leftY >= height-cube_height) break; leftV = -1; leftKeyPressed = true; break; - case 87 : if(rightY <= 0) break; rightV = 1; rightKeyPressed = true; break; - case 83 : if(rightY >= height-cube_height) break; rightV = -1; rightKeyPressed = true; break; + case 38 : if(leftY <= 0) break; setV(1, 1); break; + case 40 : if(leftY >= height-cube_height) break; setV(1, -1); break; + case 87 : if(rightY <= 0) break; setV(2, 1); break; + case 83 : if(rightY >= height-cube_height) break; setV(2, -1); break; case 37 : case 39 : if(ball_owner != 1) break; shootBall(); break; case 65 : case 68 : if(ball_owner != 2) break; shootBall(); break; } - }// TODO !localcoop + } else if(pid == 1 || pid == 2){ + switch(e.keyCode) { + case 38 : + case 87 : if(pid == 1){ + if(leftY <= 0) break; setV(1, 1); + } else if(pid == 2) { + if(rightY <= 0) break; setV(2, 1); + } + break; + case 40 : + case 83 : if(pid == 1){ + if(leftY >= height-cube_height) break; setV(1, -1); + } else if(pid == 2){ + if(rightY >= height-cube_height) break; setV(2, -1); + } + break; + case 37 : + case 39 : + case 65 : + case 68 : if(ball_owner != pid)break; socket.emit('ballshoot'); shootBall(); break; + } + } }); $(window).on('keyup', function(e) { if(localcoop){ switch(e.keyCode) { case 38 : - case 40 : + case 40 : +//TODO (maybe) add Smooth stopping to Paddles // case 40 : var i = 100; // var interval = setInterval(function(){ // i--; // leftV = i/100*(0,002-i/100); // if(i == 0) clearInterval(interval); // }, Math.round((1*delta)*60)*10); - leftV = 0; leftKeyPressed = false; break; + setV(1, 0); + break; case 87 : - case 83 : rightV = 0; rightKeyPressed = false; break; + case 83 : setV(2, 0); break; + } + }else if(pid == 1 || pid == 2){ + switch(e.keyCode) { + case 38 : + case 87 : if(pid == 1){ + setV(1, 0); + } else if(pid == 2) { + setV(2, 0); + } + break; + case 40 : + case 83 : if(pid == 1){ + setV(1, 0); + } else if(pid == 2){ + setV(2, 0); + } + break; } } -// TODO edit - // } else if(pid == 1 || pid == 2){ - // switch(e.keyCode) { - // case 38 : - // case 87 : if(pid == 1){ - // if(leftY <= 0) break; leftOr = 'up'; leftKeyPressed = true; - // } else if(pid == 2) { - // if(rightY <= 0) break; rightOr = 'up'; rightKeyPressed = true; - // } - // socket.emit('keydown', pid, 'up'); break; - // - // case 40 : - // case 83 : if(pid == 1){ - // if(leftY >= height-cube_height) break; leftOr = 'down'; leftKeyPressed = true; - // } else if(pid == 2){ - // if(rightY >= height-cube_height) break; rightOr = 'down'; rightKeyPressed = true; - // } - // socket.emit('keydown', pid, 'down'); break; - // case 37 : - // case 39 : - // case 65 : - // case 68 : if(ball_owner != pid)break; shootBall(); break; - // } - // } }); + function setV(pos, val){ + console.log(leftY); + if(pos == 1){ + leftV = val; + if(!localcoop) + socket.emit('vchange', pos, val, leftY); + } + if(pos == 2){ + rightV = val; + if(!localcoop) + socket.emit('vchange', pos, val, rightY); + } + } + function shootBall() { /** TODO Tewak and Add MAX_Speed **/ if(ball_owner == 1) diff --git a/index.js b/index.js index edde7df..1074eaf 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ app.get('/', function(req, res){ var sockets = []; var gameState = 0; var width, height; +var startinterval; /** Game-Config **/ var cube_width = 50, @@ -42,17 +43,18 @@ if(gameState > 0) socket.emit('size', width, height); fn(Date.now()); }); - /** ######## **/ - /** KEYPRESS **/ - /** ######## **/ - /** TODO!!!! */ - socket.on('keydown', function(id, or){ - console.log('Keypress'+ id + or); - socket.broadcast.emit('setOrr', id, or); + /** #### **/ + /** Ping **/ + /** ### **/ + socket.on('ballshoot', function(){ + socket.broadcast.emit('shootball'); }); - socket.on('keyup', function(id, or){ - console.log('Keypress'+ id + or); - socket.broadcast.emit('setOrr', id, or); + + /** ################ **/ + /** Paddel V-Change **/ + /** ############## **/ + socket.on('vchange', function(pos, val, y){ + socket.broadcast.emit('changeV', pos, val, y); }); /** ########## **/ @@ -74,6 +76,7 @@ if(gameState > 0) socket.emit('size', width, height); console.log('Only one Player left. Sending win-event'); // Debug Restart: gameState = 0; + clearInterval(startinterval); broadcastGameState(socket); return; } @@ -124,12 +127,12 @@ function setUp(socket){ socket.emit('size', width, height); socket.broadcast.emit('size', width, height); var i = 5; - var interval = setInterval(function(){ + startinterval = setInterval(function(){ i--; socket.emit('countdown', i); socket.broadcast.emit('countdown', i); if(i == 0){ - clearInterval(interval); + clearInterval(startinterval); gameState = 2; broadcastGameState(socket); socket.emit('start');