diff --git a/js/game.js b/js/game.js index 0ccef76..38bd986 100644 --- a/js/game.js +++ b/js/game.js @@ -150,7 +150,7 @@ Player = function(id, x, y) { // Constants this.id = id; this.topSpeed = 5; - this.acceleration = 1.5; + this.acceleration = 2; this.friction = 0.6; this.alive = false; @@ -175,8 +175,8 @@ Player.prototype.update = function() { // control force var force = { - x: 0, - y: 0 + x: 0.0, + y: 0.0 } // Grab the members, javascript is weird? @@ -189,49 +189,64 @@ Player.prototype.update = function() { // Left and right if (controls.left.isDown) { // Move to the left - force.x = -acceleration; + force.x = -1.0; } else if (controls.right.isDown) { // Move to the right - force.x = acceleration; + force.x = 1.0; } // Up and down if (controls.up.isDown) { // Move up - force.y = -acceleration; + force.y = -1.0; } else if (controls.down.isDown) { // Move down - force.y = acceleration; + force.y = 1.0; } - // Normalise out force - if (lengthV(force) > 0) { - normaliseV(force) + // normalise and apply acceleration + if (lengthV(force) !== 0) normaliseV(force); + force.x = force.x * acceleration; + force.y = force.y * acceleration; - // Limit to pos - velocity.x = Math.min(velocity.x + force.x, topSpeed); - velocity.y = Math.min(velocity.y + force.y, topSpeed); - - // Limit to neg - velocity.x = Math.max(velocity.x + force.x, -topSpeed); - velocity.y = Math.max(velocity.y + force.y, -topSpeed); - - } else { + // apply the force + velocity.x += force.x; + velocity.y += force.y; + // apply friction when no force + if (force.x === 0.0) { // if no force, then apply friction velocity.x = velocity.x * friction; + } + + if (force.y === 0.0) { velocity.y = velocity.y * friction; } - // if we have a speed, then update pos and send info to server - if (lengthV(velocity) > 1) { + // limit speed + if (lengthV(velocity) > 0.1) { + + // Limit to pos + velocity.x = Math.min(velocity.x, topSpeed); + velocity.y = Math.min(velocity.y, topSpeed); + + // Limit to neg + velocity.x = Math.max(velocity.x, -topSpeed); + velocity.y = Math.max(velocity.y, -topSpeed); + } else { + velocity.x = 0.0; + velocity.y = 0.0; + } + + // set sprite position and update server + if (lengthV(velocity) !== 0.0) { sprite.position.x = sprite.position.x + velocity.x; - sprite.position.y = sprite.position.y + velocity.y; - if (sprite.position.x < -50) sprite.position.x = width; - if (sprite.position.y < -50) sprite.position.y = height; if (sprite.position.x > width) sprite.position.x = -50; + + sprite.position.y = sprite.position.y + velocity.y; + if (sprite.position.y < -50) sprite.position.y = height; if (sprite.position.y > height) sprite.position.y = -50; var state = {