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;
});
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,16 +269,37 @@
$(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) {
@ -281,44 +307,53 @@
switch(e.keyCode) {
case 38 :
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)

View file

@ -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');