Created
October 28, 2012 22:23
-
-
Save drhayes/3970178 to your computer and use it in GitHub Desktop.
followCamera.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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