1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- function Camera() {
- this.position = {x: -100, y: -100};
- this.viewBox = {x: 100, y: 100, width: 600, height: 400};
- this.init = function(canvas) {
- this.viewBox.x = canvas.width / 3;
- this.viewBox.y = canvas.height / 3;
- this.viewBox.width = canvas.width - 2 * this.viewBox.x;
- this.viewBox.height = canvas.height - 2 * this.viewBox.y;
- this.position = {x: canvas.width / 2, y: canvas.height / 2};
- }
-
- this.update = function(canvas, player, delta) {
- this.followPlayer(player, delta);
- }
- this.handleResize = function(canvas) {
- this.viewBox.x = canvas.width / 3;
- this.viewBox.y = canvas.height / 3;
- this.viewBox.width = canvas.width - 2 * this.viewBox.x;
- this.viewBox.height = canvas.height - 2 * this.viewBox.y;
- }
- this.followPlayer = function(player) {
- var truePos = {x: 0, y: 0};
- truePos.x = this.position.x + player.position.x;
- truePos.y = this.position.y + player.position.y;
- if(truePos.x <= this.viewBox.x) {
- this.position.x -= truePos.x - this.viewBox.x;
- }
- if(player.position.x + this.position.x >= this.viewBox.x + this.viewBox.width) {
- this.position.x += (this.viewBox.x + this.viewBox.width) - truePos.x;
- }
- if(truePos.y <= this.viewBox.y) {
- this.position.y -= truePos.y - this.viewBox.y;
- }
- if(player.position.y + this.position.y >= this.viewBox.y + this.viewBox.height) {
- this.position.y += (this.viewBox.y + this.viewBox.height) - truePos.y;
- }
- }
- };
|