Skip to content

Commit

Permalink
wub wub wub 🎱
Browse files Browse the repository at this point in the history
  • Loading branch information
omgimanerd committed Apr 3, 2016
1 parent 2f63d67 commit f0656f5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
23 changes: 17 additions & 6 deletions lib/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,27 @@ Entity.prototype.setHeight = function(height) {
this.hitboxSize[1] = height;
};

/**
* This method returns the coordinates of the center of the Entity.
*/
Entity.prototype.getCenterPoint = function() {
return [
this.getX() + this.getWidth() / 2,
this.getY() + this.getHeight() / 2
];
};

/**
* Returns true if this entity has collided with the given entity using
* AABB collision detection.
* @param {Entity} other The entity to check collision against.
* @return {boolean}
*/
Entity.prototype.isCollidedWith = function(other) {
var xDifference = Math.abs((other.getX() + other.getWidth() / 2) - (
this.getX() + this.getWidth() / 2));
var yDifference = Math.abs((other.getY() + other.getHeight() / 2) - (
this.getY() + this.getHeight() / 2));
var thisCenter = this.getCenterPoint();
var otherCenter = other.getCenterPoint();
var xDifference = Math.abs(thisCenter[0] - otherCenter[0]);
var yDifference = Math.abs(thisCenter[1] - otherCenter[1]);
return xDifference < (this.getWidth() / 2 + other.getWidth() / 2) &&
yDifference < (this.getHeight() / 2 + other.getHeight() / 2);
};
Expand All @@ -193,15 +203,16 @@ Entity.prototype.isVisibleTo = function(player) {
* update.
*/
Entity.prototype.update = function() {
var currentTime = (new Date()).getTime();
if (this.lastUpdateTime == 0) {
this.updateTimeDifference = 0;
} else {
this.updateTimeDifference = (new Date()).getTime() - this.lastUpdateTime;
this.updateTimeDifference = currentTime - this.lastUpdateTime;
}
for (var i = 0; i < this.position.length; ++i) {
this.position[i] += this.velocity[i] * this.updateTimeDifference;
this.position[i] = Util.bound(
this.position[i], Constants.WORLD_MIN, Constnants.WORLD_MAX);
this.position[i], Constants.WORLD_MIN, Constants.WORLD_MAX);
this.velocity[i] += this.acceleration[i] * this.updateTimeDifference;
}
this.lastUpdateTime = currentTime;
Expand Down
3 changes: 2 additions & 1 deletion lib/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Game.prototype.removePlayer = function(id) {
Game.prototype.updatePlayerOnInput = function(id, data) {
this.players.get(id).updateOnInput(
data.keyboardState, data.leftClick, data.mouseAngle,
bind(this, function(position, orientation) {
Util.bind(this, function(position, orientation) {
this.projectiles.push(Bullet.create(id, position, orientation));
}));
};
Expand All @@ -84,6 +84,7 @@ Game.prototype.update = function() {

this.players.forEach(function(player, id) {
player.update();
console.log(player.position);
});

for (var projectile in this.projectiles) {
Expand Down
28 changes: 20 additions & 8 deletions lib/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function Player(id, name, position) {
this.lastShotTime = 0;

this.health = Constants.PLAYER_MAX_HEALTH;
this.score = 0;
}
require('../shared/inheritable');
Player.inheritsFrom(Entity);
Expand All @@ -50,13 +51,19 @@ Player.MOVE_VELOCITY = 1;
* @const
* @type {type}
*/
Player.JUMP_VELOCITY = 4;
Player.JUMP_VELOCITY = 1.75;

/**
* @const
* @type {type}
*/
Player.DECELERATION = 0.01;

/**
* @const
* @type {number}
*/
Player.GRAVITY = -0.001;
Player.GRAVITY = -0.0125;

/**
* @const
Expand All @@ -78,7 +85,6 @@ Player.SHOT_COOLDOWN = 800;
*/
Player.create = function(id, name) {
var positionX = Util.randRange(Constants.WORLD_MIN, Constants.WORLD_MAX);
console.log(positionX);
return new Player(id, name, [positionX, Player.START_Y]);
};

Expand All @@ -95,7 +101,7 @@ Player.prototype.updateOnInput = function(keyboardState, leftClick, mouseAngle,
addProjectileCallback) {
if (keyboardState.up && !this.isJumping) {
this.setVY(Player.JUMP_VELOCITY);
this.isJumping = true;
// this.isJumping = true;
}
if (keyboardState.left && !keyboardState.right) {
this.setVX(-Player.MOVE_VELOCITY);
Expand All @@ -115,13 +121,19 @@ Player.prototype.updateOnInput = function(keyboardState, leftClick, mouseAngle,

/**
* This method updates the position and state of the Player.
* @param {Array<Projectile>} projectiles The Projectiles in existence on the
* server.
*/
Player.prototype.update = function(projectiles) {
Player.prototype.update = function() {
this.parent.update.call(this);

this.setVX(this.getVX() - Player.GRAVITY);
for (var i = 0; i < 2; ++i) {
if (Math.abs(this.velocity[i]) < Player.DECELERATION) {
this.acceleration[i] = 0;
} else {
this.acceleration[i] = -Util.getSign(this.velocity[i]) * (
Player.DECELERATION);
}
}
this.setAY(Player.GRAVITY);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Projectile.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Projectile.prototype.update = function(players) {
if (this.isCollidedWith(player)) {
player.health--;
this.shouldExist = false;
// TODO: implement bump
}
// TODO: implement bump
}
};
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ io.on('connection', function(socket) {
/**
* Connected players sending will update their player state.
*/
socket.on('player-input', function(data) {
game.updateOnInput(socket.id, data);
socket.on('player-action', function(data) {
game.updatePlayerOnInput(socket.id, data);
});

/**
Expand All @@ -85,6 +85,7 @@ io.on('connection', function(socket) {
* necessary connected clients.
*/
setInterval(function() {
game.update();
game.sendState();
}, FRAME_RATE);

Expand Down

0 comments on commit f0656f5

Please sign in to comment.