Rework movement code to fix Issue #2
This commit is contained in:
parent
1a5207e742
commit
df49cc6e4a
1 changed files with 39 additions and 24 deletions
63
js/game.js
63
js/game.js
|
@ -150,7 +150,7 @@ Player = function(id, x, y) {
|
||||||
// Constants
|
// Constants
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.topSpeed = 5;
|
this.topSpeed = 5;
|
||||||
this.acceleration = 1.5;
|
this.acceleration = 2;
|
||||||
this.friction = 0.6;
|
this.friction = 0.6;
|
||||||
this.alive = false;
|
this.alive = false;
|
||||||
|
|
||||||
|
@ -175,8 +175,8 @@ Player.prototype.update = function() {
|
||||||
|
|
||||||
// control force
|
// control force
|
||||||
var force = {
|
var force = {
|
||||||
x: 0,
|
x: 0.0,
|
||||||
y: 0
|
y: 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab the members, javascript is weird?
|
// Grab the members, javascript is weird?
|
||||||
|
@ -189,49 +189,64 @@ Player.prototype.update = function() {
|
||||||
// Left and right
|
// Left and right
|
||||||
if (controls.left.isDown) {
|
if (controls.left.isDown) {
|
||||||
// Move to the left
|
// Move to the left
|
||||||
force.x = -acceleration;
|
force.x = -1.0;
|
||||||
} else if (controls.right.isDown) {
|
} else if (controls.right.isDown) {
|
||||||
// Move to the right
|
// Move to the right
|
||||||
force.x = acceleration;
|
force.x = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Up and down
|
// Up and down
|
||||||
if (controls.up.isDown) {
|
if (controls.up.isDown) {
|
||||||
// Move up
|
// Move up
|
||||||
force.y = -acceleration;
|
force.y = -1.0;
|
||||||
} else if (controls.down.isDown) {
|
} else if (controls.down.isDown) {
|
||||||
// Move down
|
// Move down
|
||||||
force.y = acceleration;
|
force.y = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalise out force
|
// normalise and apply acceleration
|
||||||
if (lengthV(force) > 0) {
|
if (lengthV(force) !== 0) normaliseV(force);
|
||||||
normaliseV(force)
|
force.x = force.x * acceleration;
|
||||||
|
force.y = force.y * acceleration;
|
||||||
|
|
||||||
// Limit to pos
|
// apply the force
|
||||||
velocity.x = Math.min(velocity.x + force.x, topSpeed);
|
velocity.x += force.x;
|
||||||
velocity.y = Math.min(velocity.y + force.y, topSpeed);
|
velocity.y += force.y;
|
||||||
|
|
||||||
// Limit to neg
|
|
||||||
velocity.x = Math.max(velocity.x + force.x, -topSpeed);
|
|
||||||
velocity.y = Math.max(velocity.y + force.y, -topSpeed);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
|
// apply friction when no force
|
||||||
|
if (force.x === 0.0) {
|
||||||
// if no force, then apply friction
|
// if no force, then apply friction
|
||||||
velocity.x = velocity.x * friction;
|
velocity.x = velocity.x * friction;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (force.y === 0.0) {
|
||||||
velocity.y = velocity.y * friction;
|
velocity.y = velocity.y * friction;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we have a speed, then update pos and send info to server
|
// limit speed
|
||||||
if (lengthV(velocity) > 1) {
|
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.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.x < -50) sprite.position.x = width;
|
||||||
if (sprite.position.y < -50) sprite.position.y = height;
|
|
||||||
if (sprite.position.x > width) sprite.position.x = -50;
|
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;
|
if (sprite.position.y > height) sprite.position.y = -50;
|
||||||
|
|
||||||
var state = {
|
var state = {
|
||||||
|
|
Loading…
Add table
Reference in a new issue