Phaser.Camera = function (game, id, x, y, width, height){ this.game = game; this.world = game.world; this.id = 0; this.view = new Phaser.Rectangle(x, y, width, height); this.bounds = new Phaser.Rectangle(x, y, width, height); this.deadzone = null ; this.visible = true ; this.roundPx = true ; this.atLimit = { x: false , y: false } ; _AN_Write_target('target', this, false , null ); this.displayObject = null ; this.scale = null ; this.totalInView = 0; this._targetPosition = new Phaser.Point(); this._edge = 0; this._position = new Phaser.Point(); } ; Phaser.Camera.FOLLOW_LOCKON = 0; Phaser.Camera.FOLLOW_PLATFORMER = 1; Phaser.Camera.FOLLOW_TOPDOWN = 2; Phaser.Camera.FOLLOW_TOPDOWN_TIGHT = 3; Phaser.Camera.prototype = { preUpdate: function (){ this.totalInView = 0; } , follow: function (target, style){ if (style === undefined) { style = Phaser.Camera.FOLLOW_LOCKON; } _AN_Write_target('target', this, false , target); var helper; switch (style){ case Phaser.Camera.FOLLOW_PLATFORMER: var w = this.width / 8; var h = this.height / 3; this.deadzone = new Phaser.Rectangle((this.width - w) / 2, (this.height - h) / 2 - h * 0.25, w, h); break ; case Phaser.Camera.FOLLOW_TOPDOWN: helper = Math.max(this.width, this.height) / 4; this.deadzone = new Phaser.Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper); break ; case Phaser.Camera.FOLLOW_TOPDOWN_TIGHT: helper = Math.max(this.width, this.height) / 8; this.deadzone = new Phaser.Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper); break ; case Phaser.Camera.FOLLOW_LOCKON: this.deadzone = null ; break ; default : { this.deadzone = null ; break ; } } } , unfollow: function (){ _AN_Write_target('target', this, false , null ); } , focusOn: function (displayObject){ this.setPosition(Math.round(displayObject.x - this.view.halfWidth), Math.round(displayObject.y - this.view.halfHeight)); } , focusOnXY: function (x, y){ this.setPosition(Math.round(x - this.view.halfWidth), Math.round(y - this.view.halfHeight)); } , update: function (){ if (this.target) { this.updateTarget(); } if (this.bounds) { this.checkBounds(); } if (this.roundPx) { this.view.floor(); } this.displayObject.position.x = - this.view.x; this.displayObject.position.y = - this.view.y; } , updateTarget: function (){ this._targetPosition.copyFrom(_AN_Read_target('target', this)); if (_AN_Read_target('target', this).parent) { this._targetPosition.multiply(_AN_Read_target('target', this).parent.worldTransform.a, _AN_Read_target('target', this).parent.worldTransform.d); } if (this.deadzone) { this._edge = this._targetPosition.x - this.view.x; if (this._edge < this.deadzone.left) { this.view.x = this._targetPosition.x - this.deadzone.left; } else if (this._edge > this.deadzone.right) { this.view.x = this._targetPosition.x - this.deadzone.right; } this._edge = this._targetPosition.y - this.view.y; if (this._edge < this.deadzone.top) { this.view.y = this._targetPosition.y - this.deadzone.top; } else if (this._edge > this.deadzone.bottom) { this.view.y = this._targetPosition.y - this.deadzone.bottom; } } else { this.view.x = this._targetPosition.x - this.view.halfWidth; this.view.y = this._targetPosition.y - this.view.halfHeight; } } , setBoundsToWorld: function (){ if (this.bounds) { this.bounds.copyFrom(this.game.world.bounds); } } , checkBounds: function (){ this.atLimit.x = false ; this.atLimit.y = false ; if (this.view.x <= this.bounds.x) { this.atLimit.x = true ; this.view.x = this.bounds.x; } if (this.view.right >= this.bounds.right) { this.atLimit.x = true ; this.view.x = this.bounds.right - this.width; } if (this.view.y <= this.bounds.top) { this.atLimit.y = true ; this.view.y = this.bounds.top; } if (this.view.bottom >= this.bounds.bottom) { this.atLimit.y = true ; this.view.y = this.bounds.bottom - this.height; } } , setPosition: function (x, y){ this.view.x = x; this.view.y = y; if (this.bounds) { this.checkBounds(); } } , setSize: function (width, height){ this.view.width = width; this.view.height = height; } , reset: function (){ _AN_Write_target('target', this, false , null ); this.view.x = 0; this.view.y = 0; } } ; Phaser.Camera.prototype.constructor = Phaser.Camera; Object.defineProperty(Phaser.Camera.prototype, "x", { get: function (){ return this.view.x; } , set: function (value){ this.view.x = value; if (this.bounds) { this.checkBounds(); } } } ); Object.defineProperty(Phaser.Camera.prototype, "y", { get: function (){ return this.view.y; } , set: function (value){ this.view.y = value; if (this.bounds) { this.checkBounds(); } } } ); Object.defineProperty(Phaser.Camera.prototype, "position", { get: function (){ this._position.set(this.view.centerX, this.view.centerY); return this._position; } , set: function (value){ if (typeof value.x !== "undefined") { this.view.x = value.x; } if (typeof value.y !== "undefined") { this.view.y = value.y; } if (this.bounds) { this.checkBounds(); } } } ); Object.defineProperty(Phaser.Camera.prototype, "width", { get: function (){ return this.view.width; } , set: function (value){ this.view.width = value; } } ); Object.defineProperty(Phaser.Camera.prototype, "height", { get: function (){ return this.view.height; } , set: function (value){ this.view.height = value; } } );