|
| 1 | +/** |
| 2 | +* Phaser - Camera |
| 3 | +* |
| 4 | +* A Camera is your view into the game world. It has a position and size and renders only those objects within its field of view. |
| 5 | +* The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y |
| 6 | +*/ |
| 7 | + |
| 8 | +Phaser.Camera = function (game, id, x, y, width, height) { |
| 9 | + |
| 10 | + this.game = game; |
| 11 | + this.world = game.world; |
| 12 | + this.id = 0; // reserved for future multiple camera set-ups |
| 13 | + |
| 14 | + // The view into the world we wish to render (by default the game dimensions) |
| 15 | + // The x/y values are in world coordinates, not screen coordinates, the width/height is how many pixels to render |
| 16 | + // Objects outside of this view are not rendered (unless set to ignore the Camera, i.e. UI?) |
| 17 | + this.view = new Phaser.Rectangle(x, y, width, height); |
| 18 | + |
| 19 | +}; |
| 20 | + |
| 21 | +// Consts |
| 22 | +Phaser.Camera.FOLLOW_LOCKON = 0; |
| 23 | +Phaser.Camera.FOLLOW_PLATFORMER = 1; |
| 24 | +Phaser.Camera.FOLLOW_TOPDOWN = 2; |
| 25 | +Phaser.Camera.FOLLOW_TOPDOWN_TIGHT = 3; |
| 26 | + |
| 27 | +Phaser.Camera.prototype = { |
| 28 | + |
| 29 | + game: null, |
| 30 | + world: null, |
| 31 | + |
| 32 | + id: 0, |
| 33 | + |
| 34 | + /** |
| 35 | + * Camera view. |
| 36 | + * @type {Rectangle} |
| 37 | + */ |
| 38 | + view: null, |
| 39 | + |
| 40 | + /** |
| 41 | + * Sprite moving inside this Rectangle will not cause camera moving. |
| 42 | + * @type {Rectangle} |
| 43 | + */ |
| 44 | + deadzone: null, |
| 45 | + |
| 46 | + /** |
| 47 | + * Whether this camera is visible or not. (default is true) |
| 48 | + * @type {bool} |
| 49 | + */ |
| 50 | + visible: true, |
| 51 | + |
| 52 | + /** |
| 53 | + * If the camera is tracking a Sprite, this is a reference to it, otherwise null |
| 54 | + * @type {Sprite} |
| 55 | + */ |
| 56 | + target: null, |
| 57 | + |
| 58 | + /** |
| 59 | + * Tells this camera which sprite to follow. |
| 60 | + * @param target {Sprite} The object you want the camera to track. Set to null to not follow anything. |
| 61 | + * @param [style] {number} Leverage one of the existing "deadzone" presets. If you use a custom deadzone, ignore this parameter and manually specify the deadzone after calling follow(). |
| 62 | + */ |
| 63 | + follow: function (target, style) { |
| 64 | + |
| 65 | + if (typeof style === "undefined") { style = Phaser.Camera.FOLLOW_LOCKON; } |
| 66 | + |
| 67 | + this.target = target; |
| 68 | + |
| 69 | + var helper; |
| 70 | + |
| 71 | + switch (style) { |
| 72 | + |
| 73 | + case Phaser.Types.CAMERA_FOLLOW_PLATFORMER: |
| 74 | + var w = this.width / 8; |
| 75 | + var h = this.height / 3; |
| 76 | + this.deadzone = new Phaser.Rectangle((this.width - w) / 2, (this.height - h) / 2 - h * 0.25, w, h); |
| 77 | + break; |
| 78 | + |
| 79 | + case Phaser.Types.CAMERA_FOLLOW_TOPDOWN: |
| 80 | + helper = Math.max(this.width, this.height) / 4; |
| 81 | + this.deadzone = new Phaser.Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper); |
| 82 | + break; |
| 83 | + |
| 84 | + case Phaser.Types.CAMERA_FOLLOW_TOPDOWN_TIGHT: |
| 85 | + helper = Math.max(this.width, this.height) / 8; |
| 86 | + this.deadzone = new Phaser.Rectangle((this.width - helper) / 2, (this.height - helper) / 2, helper, helper); |
| 87 | + break; |
| 88 | + |
| 89 | + case Phaser.Types.CAMERA_FOLLOW_LOCKON: |
| 90 | + default: |
| 91 | + this.deadzone = null; |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + }, |
| 96 | + |
| 97 | + /** |
| 98 | + * Move the camera focus to this location instantly. |
| 99 | + * @param x {number} X position. |
| 100 | + * @param y {number} Y position. |
| 101 | + */ |
| 102 | + focusOnXY: function (x, y) { |
| 103 | + |
| 104 | + x += (x > 0) ? 0.0000001 : -0.0000001; |
| 105 | + y += (y > 0) ? 0.0000001 : -0.0000001; |
| 106 | + |
| 107 | + this.view.x = Math.round(x - this.view.halfWidth); |
| 108 | + this.view.y = Math.round(y - this.view.halfHeight); |
| 109 | + |
| 110 | + }, |
| 111 | + |
| 112 | + /** |
| 113 | + * Update focusing and scrolling. |
| 114 | + */ |
| 115 | + update: function () { |
| 116 | + |
| 117 | + // this.plugins.preUpdate(); |
| 118 | + |
| 119 | + if (this.target !== null) |
| 120 | + { |
| 121 | + if (this.deadzone == null) |
| 122 | + { |
| 123 | + this.focusOnXY(this.target.x, this.target.y); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + var edge; |
| 128 | + var targetX = this.target.x + ((this.target.x > 0) ? 0.0000001 : -0.0000001); |
| 129 | + var targetY = this.target.y + ((this.target.y > 0) ? 0.0000001 : -0.0000001); |
| 130 | + |
| 131 | + edge = targetX - this.deadzone.x; |
| 132 | + |
| 133 | + if (this.view.x > edge) |
| 134 | + { |
| 135 | + this.view.x = edge; |
| 136 | + } |
| 137 | + |
| 138 | + edge = targetX + this.target.width - this.deadzone.x - this.deadzone.width; |
| 139 | + |
| 140 | + if (this.view.x < edge) |
| 141 | + { |
| 142 | + this.view.x = edge; |
| 143 | + } |
| 144 | + |
| 145 | + edge = targetY - this.deadzone.y; |
| 146 | + |
| 147 | + if (this.view.y > edge) |
| 148 | + { |
| 149 | + this.view.y = edge; |
| 150 | + } |
| 151 | + |
| 152 | + edge = targetY + this.target.height - this.deadzone.y - this.deadzone.height; |
| 153 | + |
| 154 | + if (this.view.y < edge) |
| 155 | + { |
| 156 | + this.view.y = edge; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Make sure we didn't go outside the cameras worldBounds |
| 162 | + if (this.view.x < this.world.bounds.left) |
| 163 | + { |
| 164 | + this.view.x = this.world.bounds.left; |
| 165 | + } |
| 166 | + |
| 167 | + if (this.view.x > this.world.bounds.right - this.width) |
| 168 | + { |
| 169 | + this.view.x = (this.world.bounds.right - this.width) + 1; |
| 170 | + } |
| 171 | + |
| 172 | + if (this.view.y < this.world.bounds.top) |
| 173 | + { |
| 174 | + this.view.y = this.world.bounds.top; |
| 175 | + } |
| 176 | + |
| 177 | + if (this.view.y > this.world.bounds.bottom - this.height) |
| 178 | + { |
| 179 | + this.view.y = (this.world.bounds.bottom - this.height) + 1; |
| 180 | + } |
| 181 | + |
| 182 | + this.view.floor(); |
| 183 | + |
| 184 | + // this.plugins.update(); |
| 185 | + |
| 186 | + }, |
| 187 | + |
| 188 | + setPosition: function (x, y) { |
| 189 | + |
| 190 | + this.view.x = x; |
| 191 | + this.view.y = y; |
| 192 | + |
| 193 | + }, |
| 194 | + |
| 195 | + setSize: function (width, height) { |
| 196 | + |
| 197 | + this.view.width = width; |
| 198 | + this.view.height = height; |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | +}; |
| 203 | + |
| 204 | +Object.defineProperty(Phaser.Camera.prototype, "x", { |
| 205 | + |
| 206 | + get: function () { |
| 207 | + return this.view.x; |
| 208 | + }, |
| 209 | + |
| 210 | + set: function (value) { |
| 211 | + this.view.x = value; |
| 212 | + }, |
| 213 | + |
| 214 | + enumerable: true, |
| 215 | + configurable: true |
| 216 | +}); |
| 217 | + |
| 218 | +Object.defineProperty(Phaser.Camera.prototype, "y", { |
| 219 | + |
| 220 | + get: function () { |
| 221 | + return this.view.y; |
| 222 | + }, |
| 223 | + |
| 224 | + set: function (value) { |
| 225 | + this.view.y = value; |
| 226 | + }, |
| 227 | + |
| 228 | + enumerable: true, |
| 229 | + configurable: true |
| 230 | +}); |
| 231 | + |
| 232 | +Object.defineProperty(Phaser.Camera.prototype, "width", { |
| 233 | + |
| 234 | + get: function () { |
| 235 | + return this.view.width; |
| 236 | + }, |
| 237 | + |
| 238 | + set: function (value) { |
| 239 | + this.view.width = value; |
| 240 | + }, |
| 241 | + |
| 242 | + enumerable: true, |
| 243 | + configurable: true |
| 244 | +}); |
| 245 | + |
| 246 | +Object.defineProperty(Phaser.Camera.prototype, "height", { |
| 247 | + |
| 248 | + get: function () { |
| 249 | + return this.view.height; |
| 250 | + }, |
| 251 | + |
| 252 | + set: function (value) { |
| 253 | + this.view.height = value; |
| 254 | + }, |
| 255 | + |
| 256 | + enumerable: true, |
| 257 | + configurable: true |
| 258 | +}); |
0 commit comments