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.lerp = new Phaser.Point(1, 1); this.onShakeComplete = new Phaser.Signal(); this.onFlashComplete = new Phaser.Signal(); this.onFadeComplete = new Phaser.Signal(); this.fx = null ; this._targetPosition = new Phaser.Point(); this._edge = 0; this._position = new Phaser.Point(); this._shake = { intensity: 0, duration: 0, horizontal: false , vertical: false , shakeBounds: true , x: 0, y: 0} ; this._fxDuration = 0; this._fxType = 0; } ; Phaser.Camera.FOLLOW_LOCKON = 0; Phaser.Camera.FOLLOW_PLATFORMER = 1; Phaser.Camera.FOLLOW_TOPDOWN = 2; Phaser.Camera.FOLLOW_TOPDOWN_TIGHT = 3; Phaser.Camera.SHAKE_BOTH = 4; Phaser.Camera.SHAKE_HORIZONTAL = 5; Phaser.Camera.SHAKE_VERTICAL = 6; Phaser.Camera.ENABLE_FX = true ; Phaser.Camera.prototype = { boot: function (){ this.displayObject = this.game.world; this.scale = this.game.world.scale; this.game.camera = this; if (Phaser.Graphics && Phaser.Camera.ENABLE_FX) { this.fx = new Phaser.Graphics(this.game); this.game.stage.addChild(this.fx); } } , preUpdate: function (){ this.totalInView = 0; } , follow: function (target, style, lerpX, lerpY){ if (style === undefined) { style = Phaser.Camera.FOLLOW_LOCKON; } if (lerpX === undefined) { lerpX = 1; } if (lerpY === undefined) { lerpY = 1; } _AN_Write_target('target', this, false , target); this.lerp.set(lerpX, lerpY); 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)); } , shake: function (intensity, duration, force, direction, shakeBounds){ if (intensity === undefined) { intensity = 0.05; } if (duration === undefined) { duration = 500; } if (force === undefined) { force = true ; } if (direction === undefined) { direction = Phaser.Camera.SHAKE_BOTH; } if (shakeBounds === undefined) { shakeBounds = true ; } if (!force && this._shake.duration > 0) { return false ; } this._shake.intensity = intensity; this._shake.duration = duration; this._shake.shakeBounds = shakeBounds; this._shake.x = 0; this._shake.y = 0; this._shake.horizontal = (direction === Phaser.Camera.SHAKE_BOTH || direction === Phaser.Camera.SHAKE_HORIZONTAL); this._shake.vertical = (direction === Phaser.Camera.SHAKE_BOTH || direction === Phaser.Camera.SHAKE_VERTICAL); return true ; } , flash: function (color, duration, force){ if (color === undefined) { color = 16777215; } if (duration === undefined) { duration = 500; } if (force === undefined) { force = false ; } if (!this.fx || (!force && this._fxDuration > 0)) { return false ; } _AN_Call_clear('clear', this.fx); this.fx.beginFill(color); this.fx.drawRect(0, 0, this.width, this.height); this.fx.endFill(); this.fx.alpha = 1; this._fxDuration = duration; this._fxType = 0; return true ; } , fade: function (color, duration, force){ if (color === undefined) { color = 0; } if (duration === undefined) { duration = 500; } if (force === undefined) { force = false ; } if (!this.fx || (!force && this._fxDuration > 0)) { return false ; } _AN_Call_clear('clear', this.fx); this.fx.beginFill(color); this.fx.drawRect(0, 0, this.width, this.height); this.fx.endFill(); this.fx.alpha = 0; this._fxDuration = duration; this._fxType = 1; return true ; } , update: function (){ if (this._fxDuration > 0) { this.updateFX(); } if (this._shake.duration > 0) { this.updateShake(); } if (this.bounds) { this.checkBounds(); } if (this.roundPx) { this.view.floor(); this._shake.x = Math.floor(this._shake.x); this._shake.y = Math.floor(this._shake.y); } this.displayObject.position.x = - this.view.x; this.displayObject.position.y = - this.view.y; } , updateFX: function (){ if (this._fxType === 0) { this.fx.alpha -= this.game.time.elapsedMS / this._fxDuration; if (this.fx.alpha <= 0) { this._fxDuration = 0; this.fx.alpha = 0; this.onFlashComplete.dispatch(); } } else { this.fx.alpha += this.game.time.elapsedMS / this._fxDuration; if (this.fx.alpha >= 1) { this._fxDuration = 0; this.fx.alpha = 1; this.onFadeComplete.dispatch(); } } } , updateShake: function (){ this._shake.duration -= this.game.time.elapsedMS; if (this._shake.duration <= 0) { this.onShakeComplete.dispatch(); this._shake.x = 0; this._shake.y = 0; } else { if (this._shake.horizontal) { this._shake.x = this.game.rnd.frac() * this._shake.intensity * this.view.width * 2 - this._shake.intensity * this.view.width; } if (this._shake.vertical) { this._shake.y = this.game.rnd.frac() * this._shake.intensity * this.view.height * 2 - this._shake.intensity * this.view.height; } } } , updateTarget: function (){ this._targetPosition.x = this.view.x + _AN_Read_target('target', this).worldPosition.x; this._targetPosition.y = this.view.y + _AN_Read_target('target', this).worldPosition.y; if (this.deadzone) { this._edge = this._targetPosition.x - this.view.x; if (this._edge < this.deadzone.left) { this.view.x = this.game.math.linear(this.view.x, this._targetPosition.x - this.deadzone.left, this.lerp.x); } else if (this._edge > this.deadzone.right) { this.view.x = this.game.math.linear(this.view.x, this._targetPosition.x - this.deadzone.right, this.lerp.x); } this._edge = this._targetPosition.y - this.view.y; if (this._edge < this.deadzone.top) { this.view.y = this.game.math.linear(this.view.y, this._targetPosition.y - this.deadzone.top, this.lerp.y); } else if (this._edge > this.deadzone.bottom) { this.view.y = this.game.math.linear(this.view.y, this._targetPosition.y - this.deadzone.bottom, this.lerp.y); } } else { this.view.x = this.game.math.linear(this.view.x, this._targetPosition.x - this.view.halfWidth, this.lerp.x); this.view.y = this.game.math.linear(this.view.y, this._targetPosition.y - this.view.halfHeight, this.lerp.y); } 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; } , setBoundsToWorld: function (){ if (this.bounds) { this.bounds.copyFrom(this.game.world.bounds); } } , checkBounds: function (){ this.atLimit.x = false ; this.atLimit.y = false ; var vx = this.view.x + this._shake.x; var vw = this.view.right + this._shake.x; var vy = this.view.y + this._shake.y; var vh = this.view.bottom + this._shake.y; if (vx <= this.bounds.x * this.scale.x) { this.atLimit.x = true ; this.view.x = this.bounds.x * this.scale.x; if (!this._shake.shakeBounds) { this._shake.x = 0; } } if (vw >= this.bounds.right * this.scale.x) { this.atLimit.x = true ; this.view.x = (this.bounds.right * this.scale.x) - this.width; if (!this._shake.shakeBounds) { this._shake.x = 0; } } if (vy <= this.bounds.top * this.scale.y) { this.atLimit.y = true ; this.view.y = this.bounds.top * this.scale.y; if (!this._shake.shakeBounds) { this._shake.y = 0; } } if (vh >= this.bounds.bottom * this.scale.y) { this.atLimit.y = true ; this.view.y = (this.bounds.bottom * this.scale.y) - this.height; if (!this._shake.shakeBounds) { this._shake.y = 0; } } } , 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; this._shake.duration = 0; this.resetFX(); } , resetFX: function (){ _AN_Call_clear('clear', this.fx); this.fx.alpha = 0; this._fxDuration = 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.x, this.view.y); 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; } } ); Object.defineProperty(Phaser.Camera.prototype, "shakeIntensity", { get: function (){ return this._shake.intensity; } , set: function (value){ this._shake.intensity = value; } } );