Added Powerups
This commit is contained in:
parent
f84a5bf850
commit
9dabae056d
2 changed files with 86 additions and 4 deletions
25
index.html
25
index.html
|
@ -44,6 +44,7 @@
|
|||
ball_VY = 0,
|
||||
ball_maxAngle = 5*Math.PI/12;
|
||||
|
||||
var powerups = [];
|
||||
/** Game Variables **/
|
||||
var uid, // Unique-ID
|
||||
pid, // PlayerID 0 == Specator, 1 == Player 1, 2 == Player 2
|
||||
|
@ -84,6 +85,7 @@
|
|||
cube_height = c_height;
|
||||
cube_speed = sp;
|
||||
score = score;
|
||||
powerups = [];
|
||||
fn($(window).width(), $(window).height());
|
||||
});
|
||||
|
||||
|
@ -118,6 +120,16 @@
|
|||
console.log('Additional Data: ' + data);
|
||||
});
|
||||
|
||||
socket.on('powerup', function(type, width, height, x, y){
|
||||
console.log(x);
|
||||
console.log(y);
|
||||
powerups.push(new PowerUp(type, width, height, x, y));
|
||||
});
|
||||
|
||||
socket.on('powerupremove', function(k){
|
||||
powerups.splice(k, 1);
|
||||
})
|
||||
|
||||
socket.on('paddlepos', function(pos, val, y){
|
||||
if(pos == 1){
|
||||
leftV = val;
|
||||
|
@ -244,6 +256,11 @@
|
|||
ctx.fillStyle = rightColor;
|
||||
ctx.fillRect(width-cube_width, rightY, cube_width, cube_height);
|
||||
|
||||
//Power UPS
|
||||
powerups.forEach(function(pu){
|
||||
ctx.fillStyle = 'green';
|
||||
ctx.fillRect(pu.x, pu.y, pu.width, pu.height);
|
||||
});
|
||||
|
||||
// NOTE Physics moved to Server
|
||||
// /** Move Ball with Paddle if owned by owner (@roundstart) **/
|
||||
|
@ -387,6 +404,14 @@
|
|||
}
|
||||
}
|
||||
|
||||
function PowerUp(type, width, height, x, y){
|
||||
this.type = type;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
function shootBall() {
|
||||
// NOTE moved to Server
|
||||
// console.log('### Ball shoot event ###');
|
||||
|
|
65
index.js
65
index.js
|
@ -15,7 +15,8 @@ var Configuration = {
|
|||
ball_speed : 15,
|
||||
ball_owner : Math.round(Math.random()) + 1,
|
||||
ball_maxAngle : 5*Math.PI/12,
|
||||
wincount : 5
|
||||
wincount : 3,
|
||||
powerupmaxtime : 10
|
||||
}
|
||||
|
||||
/** Parameters **/
|
||||
|
@ -26,7 +27,11 @@ var clients = [], // List of all conected clients
|
|||
startinterval, // Interval for Counting down (cancelable)
|
||||
calcPos,
|
||||
score = [0,0],
|
||||
id = 0;
|
||||
id = 0,
|
||||
powerups = [];
|
||||
|
||||
/** PowerUps **/
|
||||
var powerTime = 30;
|
||||
|
||||
/** Game-Config **/
|
||||
var cube_width = Configuration.cube_width,
|
||||
|
@ -230,8 +235,26 @@ function tickServer(){
|
|||
}, tickrate);
|
||||
}
|
||||
|
||||
function drawPowerUp(){
|
||||
var key = powerups.push(new PowerUp(1));
|
||||
console.log(powerups[key-1].x);
|
||||
console.log(powerups[key-1].y);
|
||||
players[0].socket.emit('powerup', powerups[key-1].type, powerups[key-1].width, powerups[key-1].height, powerups[key-1].x, powerups[key-1].y);
|
||||
players[0].socket.broadcast.emit('powerup', powerups[key-1].type, powerups[key-1].width, powerups[key-1].height, powerups[key-1].x, powerups[key-1].y);
|
||||
}
|
||||
|
||||
function calculatePlayerPosition(){
|
||||
players.forEach(function(player, k){
|
||||
// console.log(player.powerup.tick);
|
||||
if(player.powerup.type == 1){
|
||||
player.speed = Configuration.cube_speed + 10;
|
||||
player.powerup.tick++;
|
||||
if(player.powerup.tick == Configuration.powerupmaxtime*(1000/tickrate)){
|
||||
player.powerup = 0;
|
||||
}
|
||||
} else {
|
||||
player.speed = Configuration.cube_speed;
|
||||
}
|
||||
player.y -= player.vY*player.speed;
|
||||
if(player.y > height-player.height || player.y < 0) player.vY = 0;
|
||||
if(player.y < 0) player.y = 0;
|
||||
|
@ -267,6 +290,7 @@ function calculateBallPosition(){
|
|||
ball_VX = 0;
|
||||
ball_VY = 0;
|
||||
ball_owner = 1;
|
||||
drawPowerUp();
|
||||
} else {
|
||||
var intersect = ((players[1].y+cube_height/2)-ballY)/(cube_height/2);
|
||||
console.log('Intersect with right at:');
|
||||
|
@ -285,6 +309,8 @@ function calculateBallPosition(){
|
|||
ball_VX = 0;
|
||||
ball_VY = 0;
|
||||
ball_owner = 2;
|
||||
// Add PowerUp
|
||||
drawPowerUp();
|
||||
} else {
|
||||
var intersect = ((players[0].y+cube_height/2)-ballY)/(cube_height/2);
|
||||
console.log('Intersect with left at:');
|
||||
|
@ -301,6 +327,27 @@ function calculateBallPosition(){
|
|||
console.log(ballX + ' : ' + ballY);
|
||||
}
|
||||
|
||||
/** Collide with PowerUp **/
|
||||
powerups.forEach(function(pu, key){
|
||||
if(!(ballY > pu.y+pu.height || ballY+ball_size*2 < pu.y)){
|
||||
if(!(ballX > pu.x+pu.width || ballX+ball_size*2 < pu.x)){
|
||||
if(ball_VX > 0){
|
||||
players[0].powerup = pu;
|
||||
} else {
|
||||
players[1].powerup = pu;
|
||||
}
|
||||
console.log('#### KEY####');
|
||||
console.log(key);
|
||||
// console.log(pu);
|
||||
powerups.splice(key, 1);
|
||||
console.log(powerups.length);
|
||||
players[0].socket.emit('powerupremove', key);
|
||||
players[0].socket.broadcast.emit('powerupremove', key);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var data = {
|
||||
x : ballX,
|
||||
y : ballY,
|
||||
|
@ -350,7 +397,7 @@ function resetServer(){
|
|||
|
||||
cube_width = Configuration.cube_width;
|
||||
cube_height = Configuration.cube_height;
|
||||
cube_speed = Configuration.cube_speed;
|
||||
u = Configuration.cube_speed;
|
||||
ball_size = Configuration.ball_size;
|
||||
ball_speed = Configuration.ball_speed;
|
||||
ball_owner = Configuration.ball_owner;
|
||||
|
@ -359,7 +406,7 @@ function resetServer(){
|
|||
ball_VY = 0;
|
||||
ballX = Configuration.cube_width;
|
||||
ballY = Configuration.cube_height/2;
|
||||
|
||||
powerups = [];
|
||||
players = [];
|
||||
}
|
||||
|
||||
|
@ -380,4 +427,14 @@ function Player(id, socket, window_width, window_height){
|
|||
this.height = cube_height;
|
||||
this.y = 0;
|
||||
this.vY = 0;
|
||||
this.powerup = 0;
|
||||
}
|
||||
|
||||
function PowerUp(type){
|
||||
this.type = type;
|
||||
this.tick = 0;
|
||||
this.width = 50;
|
||||
this.height = 50;
|
||||
this.x = Math.random()*(width-Configuration.cube_width*4)+Configuration.cube_width*2;
|
||||
this.y = Math.random()*(height-50);
|
||||
}
|
||||
|
|
Reference in a new issue