Skip to content

Instantly share code, notes, and snippets.

@drhayes
Created October 28, 2012 22:23
Show Gist options
  • Save drhayes/3970178 to your computer and use it in GitHub Desktop.
Save drhayes/3970178 to your computer and use it in GitHub Desktop.
followCamera.js
calculatePlayerStats: function() {
this.player = ig.game.getEntityByName('player');
// Did we actually get a player?
if (this.player) {
// Cache some stats.
this.halfPlayerWidth = this.player.size.x / 2;
this.halfPlayerHeight = this.player.size.y / 2;
this.maxVel = this.player.maxVel;
}
},
update: function() {
this.parent();
ig.game.screen.x = this.pos.x;
ig.game.screen.y = this.pos.y;
if (!this.player) {
this.calculatePlayerStats();
}
// If we actually got a player, then start tracking.
if (this.player && this.player.alive) {
var currentX = this.pos.x;
var currentY = this.pos.y;
var targetX = this.player.pos.x - this.halfScreenWidth + this.halfPlayerWidth;
var targetY = this.player.pos.y - this.halfScreenHeight + this.halfPlayerHeight;
this.pos.x = Math.round(currentX + (targetX - currentX) * ACCEL_FACTOR);
this.pos.y = Math.round(currentY + (targetY - currentY) * ACCEL_FACTOR);
} else {
this.player = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment