Archived
1
0
Fork 0

Made Snyc working again

This commit is contained in:
Simon Giesel 2017-01-21 00:50:32 +01:00
parent f17a921ca3
commit e07ee28d18
2 changed files with 95 additions and 57 deletions

View file

@ -39,6 +39,10 @@
gameState = state; gameState = state;
}); });
socket.on('shootball', function(){
shootBall();
});
/** Make Countdown serversided No cheating possible **/ /** Make Countdown serversided No cheating possible **/
socket.on('countdown', function(i){ socket.on('countdown', function(i){
text = 'Starting in ' + i; text = 'Starting in ' + i;
@ -47,25 +51,28 @@
socket.on('start', function(){ socket.on('start', function(){
leftOr = 'none'; leftOr = 'none';
rightOr = 'none'; rightOr = 'none';
leftKeyPressed = false;
rightKeyPressed = false;
leftY = 0; leftY = 0;
rightY = 0; rightY = 0;
leftV = 0; leftV = 0;
rightV = 0; rightV = 0;
ballX = cube_width+ball_size;
ballY = cube_height/2;
ball_owner = 1;
ball_VX = 0;
ball_VY = 0;
gameState = 2; gameState = 2;
}); });
socket.on('setOrr', function(id, orr){ socket.on('changeV', function(pos, val, y){
console.log(id); if(pos == 1){
if(id == 1){ leftV = val;
leftOr = orr; leftY = y;
leftKeyPressed = true;
} }
if(id == 2){ if(pos == 2){
rightOr = orr; rightV = val;
rightKeyPressed = true;// TODO CHANGE! rightY = y;
} }
console.log(leftY);
}); });
socket.on('test', function(arg){ socket.on('test', function(arg){
@ -84,8 +91,6 @@
var rightV = 0; var rightV = 0;
var leftColor = 'red'; var leftColor = 'red';
var rightColor = 'blue'; var rightColor = 'blue';
var leftKeyPressed = false;
var rightKeyPressed = false;
var gameState = 0; // 0 == Waiting for Players, 1 == Countdown, 2 == inGame, 3 == ??Win?? var gameState = 0; // 0 == Waiting for Players, 1 == Countdown, 2 == inGame, 3 == ??Win??
/** FPS Vars **/ /** FPS Vars **/
@ -94,7 +99,7 @@
fps; fps;
/** Global Configuration **/ /** Global Configuration **/
var localcoop = true, // Local Coop in 1 window var localcoop = false, // Local Coop in 1 window
width = $(window).width(), width = $(window).width(),
height = $(window).height(); height = $(window).height();
@ -264,16 +269,37 @@
$(window).on('keydown', function(e) { $(window).on('keydown', function(e) {
if(localcoop){ if(localcoop){
switch(e.keyCode) { switch(e.keyCode) {
case 38 : if(leftY <= 0) break; leftV = 1; leftKeyPressed = true; break; case 38 : if(leftY <= 0) break; setV(1, 1); break;
case 40 : if(leftY >= height-cube_height) break; leftV = -1; leftKeyPressed = true; break; case 40 : if(leftY >= height-cube_height) break; setV(1, -1); break;
case 87 : if(rightY <= 0) break; rightV = 1; rightKeyPressed = true; break; case 87 : if(rightY <= 0) break; setV(2, 1); break;
case 83 : if(rightY >= height-cube_height) break; rightV = -1; rightKeyPressed = true; break; case 83 : if(rightY >= height-cube_height) break; setV(2, -1); break;
case 37 : case 37 :
case 39 : if(ball_owner != 1) break; shootBall(); break; case 39 : if(ball_owner != 1) break; shootBall(); break;
case 65 : case 65 :
case 68 : if(ball_owner != 2) break; shootBall(); break; 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) { $(window).on('keyup', function(e) {
@ -281,44 +307,53 @@
switch(e.keyCode) { switch(e.keyCode) {
case 38 : case 38 :
case 40 : case 40 :
//TODO (maybe) add Smooth stopping to Paddles
// case 40 : var i = 100; // case 40 : var i = 100;
// var interval = setInterval(function(){ // var interval = setInterval(function(){
// i--; // i--;
// leftV = i/100*(0,002-i/100); // leftV = i/100*(0,002-i/100);
// if(i == 0) clearInterval(interval); // if(i == 0) clearInterval(interval);
// }, Math.round((1*delta)*60)*10); // }, Math.round((1*delta)*60)*10);
leftV = 0; leftKeyPressed = false; break; setV(1, 0);
break;
case 87 : 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() { function shootBall() {
/** TODO Tewak and Add MAX_Speed **/ /** TODO Tewak and Add MAX_Speed **/
if(ball_owner == 1) if(ball_owner == 1)

View file

@ -9,6 +9,7 @@ app.get('/', function(req, res){
var sockets = []; var sockets = [];
var gameState = 0; var gameState = 0;
var width, height; var width, height;
var startinterval;
/** Game-Config **/ /** Game-Config **/
var cube_width = 50, var cube_width = 50,
@ -42,17 +43,18 @@ if(gameState > 0) socket.emit('size', width, height);
fn(Date.now()); fn(Date.now());
}); });
/** ######## **/ /** #### **/
/** KEYPRESS **/ /** Ping **/
/** ######## **/ /** ### **/
/** TODO!!!! */ socket.on('ballshoot', function(){
socket.on('keydown', function(id, or){ socket.broadcast.emit('shootball');
console.log('Keypress'+ id + or);
socket.broadcast.emit('setOrr', id, or);
}); });
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'); console.log('Only one Player left. Sending win-event');
// Debug Restart: // Debug Restart:
gameState = 0; gameState = 0;
clearInterval(startinterval);
broadcastGameState(socket); broadcastGameState(socket);
return; return;
} }
@ -124,12 +127,12 @@ function setUp(socket){
socket.emit('size', width, height); socket.emit('size', width, height);
socket.broadcast.emit('size', width, height); socket.broadcast.emit('size', width, height);
var i = 5; var i = 5;
var interval = setInterval(function(){ startinterval = setInterval(function(){
i--; i--;
socket.emit('countdown', i); socket.emit('countdown', i);
socket.broadcast.emit('countdown', i); socket.broadcast.emit('countdown', i);
if(i == 0){ if(i == 0){
clearInterval(interval); clearInterval(startinterval);
gameState = 2; gameState = 2;
broadcastGameState(socket); broadcastGameState(socket);
socket.emit('start'); socket.emit('start');