|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @copyright 2016 Photon Storm Ltd. |
| 4 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +* 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. |
| 9 | +* The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y |
| 10 | +* |
| 11 | +* @class Phaser.Camera |
| 12 | +* @constructor |
| 13 | +* @param {Phaser.Game} game - Game reference to the currently running game. |
| 14 | +* @param {number} id - Not being used at the moment, will be when Phaser supports multiple camera |
| 15 | +* @param {number} x - Position of the camera on the X axis |
| 16 | +* @param {number} y - Position of the camera on the Y axis |
| 17 | +* @param {number} width - The width of the view rectangle |
| 18 | +* @param {number} height - The height of the view rectangle |
| 19 | +*/ |
| 20 | +Phaser.Camera = function (state, x, y, width, height) |
| 21 | +{ |
| 22 | + console.log('Camera'); |
| 23 | + |
| 24 | + /** |
| 25 | + * The State that this Camera belongs to. A Camera can only belong to one State, and a State only |
| 26 | + * has one Camera. |
| 27 | + * @property {Phaser.State} state |
| 28 | + */ |
| 29 | + this.state = state; |
| 30 | + |
| 31 | + /** |
| 32 | + * @property {Phaser.Game} game - A reference to the currently running Game. |
| 33 | + */ |
| 34 | + this.game = state.game; |
| 35 | + |
| 36 | + this.width = width; |
| 37 | + |
| 38 | + this.height = height; |
| 39 | + |
| 40 | + this.transform = new Phaser.Component.Transform(this, x, y); |
| 41 | + |
| 42 | + /** |
| 43 | + * The Camera is bound to this Rectangle and cannot move outside of it. By default it is enabled and set to the size of the World. |
| 44 | + * The Rectangle can be located anywhere in the world and updated as often as you like. If you don't wish the Camera to be bound |
| 45 | + * at all then set this to null. The values can be anything and are in World coordinates, with 0,0 being the top-left of the world. |
| 46 | + * |
| 47 | + * @property {Phaser.Rectangle} bounds - The Rectangle in which the Camera is bounded. Set to null to allow for movement anywhere. |
| 48 | + */ |
| 49 | + // this.bounds = new Phaser.Rectangle(x, y, width, height); |
| 50 | + |
| 51 | + // this.bounds = new Phaser.Circle(x, y) |
| 52 | + |
| 53 | + /** |
| 54 | + * @property {boolean} atLimit - Whether this camera is flush with the World Bounds or not. |
| 55 | + */ |
| 56 | + this.atLimit = { x: false, y: false }; |
| 57 | +}; |
| 58 | + |
| 59 | +Phaser.Camera.prototype.constructor = Phaser.Camera; |
| 60 | + |
| 61 | +Phaser.Camera.prototype = { |
| 62 | + |
| 63 | + /** |
| 64 | + * Method called to ensure the camera doesn't venture outside of the game world. |
| 65 | + * Called automatically by Camera.update. |
| 66 | + * |
| 67 | + * @method Phaser.Camera#checkBounds |
| 68 | + * @protected |
| 69 | + */ |
| 70 | + checkBounds: function () |
| 71 | + { |
| 72 | + this.atLimit.x = false; |
| 73 | + this.atLimit.y = false; |
| 74 | + |
| 75 | + // var vx = this.view.x + this._shake.x; |
| 76 | + // var vw = this.view.right + this._shake.x; |
| 77 | + // var vy = this.view.y + this._shake.y; |
| 78 | + // var vh = this.view.bottom + this._shake.y; |
| 79 | + |
| 80 | + var vx = this.x; |
| 81 | + var vw = this.x + this.width; |
| 82 | + var vy = this.y; |
| 83 | + var vh = this.y + this.height; |
| 84 | + |
| 85 | + // Make sure we didn't go outside the cameras bounds |
| 86 | + if (vx <= this.bounds.x * this.scale.x) |
| 87 | + { |
| 88 | + this.atLimit.x = true; |
| 89 | + this.view.x = this.bounds.x * this.scale.x; |
| 90 | + |
| 91 | + if (!this._shake.shakeBounds) |
| 92 | + { |
| 93 | + // The camera is up against the bounds, so reset the shake |
| 94 | + this._shake.x = 0; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (vw >= this.bounds.right * this.scale.x) |
| 99 | + { |
| 100 | + this.atLimit.x = true; |
| 101 | + this.view.x = (this.bounds.right * this.scale.x) - this.width; |
| 102 | + |
| 103 | + if (!this._shake.shakeBounds) |
| 104 | + { |
| 105 | + // The camera is up against the bounds, so reset the shake |
| 106 | + this._shake.x = 0; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (vy <= this.bounds.top * this.scale.y) |
| 111 | + { |
| 112 | + this.atLimit.y = true; |
| 113 | + this.view.y = this.bounds.top * this.scale.y; |
| 114 | + |
| 115 | + if (!this._shake.shakeBounds) |
| 116 | + { |
| 117 | + // The camera is up against the bounds, so reset the shake |
| 118 | + this._shake.y = 0; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (vh >= this.bounds.bottom * this.scale.y) |
| 123 | + { |
| 124 | + this.atLimit.y = true; |
| 125 | + this.view.y = (this.bounds.bottom * this.scale.y) - this.height; |
| 126 | + |
| 127 | + if (!this._shake.shakeBounds) |
| 128 | + { |
| 129 | + // The camera is up against the bounds, so reset the shake |
| 130 | + this._shake.y = 0; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + }, |
| 135 | + |
| 136 | + |
| 137 | +}; |
| 138 | + |
| 139 | +Object.defineProperties(Phaser.Camera.prototype, { |
| 140 | + |
| 141 | + // Transform getters / setters |
| 142 | + |
| 143 | + x: { |
| 144 | + |
| 145 | + enumerable: true, |
| 146 | + |
| 147 | + get: function () |
| 148 | + { |
| 149 | + return this.transform._posX; |
| 150 | + }, |
| 151 | + |
| 152 | + set: function (value) |
| 153 | + { |
| 154 | + this.transform._posX = value; |
| 155 | + this.transform.dirty = true; |
| 156 | + } |
| 157 | + |
| 158 | + }, |
| 159 | + |
| 160 | + y: { |
| 161 | + |
| 162 | + enumerable: true, |
| 163 | + |
| 164 | + get: function () |
| 165 | + { |
| 166 | + return this.transform._posY; |
| 167 | + }, |
| 168 | + |
| 169 | + set: function (value) |
| 170 | + { |
| 171 | + this.transform._posY = value; |
| 172 | + this.transform.dirty = true; |
| 173 | + } |
| 174 | + |
| 175 | + }, |
| 176 | + |
| 177 | + right: { |
| 178 | + |
| 179 | + enumerable: true, |
| 180 | + |
| 181 | + get: function () |
| 182 | + { |
| 183 | + return this.transform._posX + (this.width * this.transform._scaleX); |
| 184 | + } |
| 185 | + |
| 186 | + }, |
| 187 | + |
| 188 | + bottom: { |
| 189 | + |
| 190 | + enumerable: true, |
| 191 | + |
| 192 | + get: function () |
| 193 | + { |
| 194 | + return this.transform._posY + (this.height * this.transform._scaleY); |
| 195 | + } |
| 196 | + |
| 197 | + }, |
| 198 | + |
| 199 | + scale: { |
| 200 | + |
| 201 | + enumerable: true, |
| 202 | + |
| 203 | + get: function () |
| 204 | + { |
| 205 | + return this.transform._scaleX; |
| 206 | + }, |
| 207 | + |
| 208 | + set: function (value) |
| 209 | + { |
| 210 | + this.transform._scaleX = value; |
| 211 | + this.transform._scaleY = value; |
| 212 | + this.transform.dirty = true; |
| 213 | + this.transform.updateCache(); |
| 214 | + } |
| 215 | + |
| 216 | + }, |
| 217 | + |
| 218 | + scaleX: { |
| 219 | + |
| 220 | + enumerable: true, |
| 221 | + |
| 222 | + get: function () |
| 223 | + { |
| 224 | + return this.transform._scaleX; |
| 225 | + }, |
| 226 | + |
| 227 | + set: function (value) |
| 228 | + { |
| 229 | + this.transform._scaleX = value; |
| 230 | + this.transform.dirty = true; |
| 231 | + this.transform.updateCache(); |
| 232 | + } |
| 233 | + |
| 234 | + }, |
| 235 | + |
| 236 | + scaleY: { |
| 237 | + |
| 238 | + enumerable: true, |
| 239 | + |
| 240 | + get: function () |
| 241 | + { |
| 242 | + return this.transform._scaleY; |
| 243 | + }, |
| 244 | + |
| 245 | + set: function (value) |
| 246 | + { |
| 247 | + this.transform._scaleY = value; |
| 248 | + this.transform.dirty = true; |
| 249 | + this.transform.updateCache(); |
| 250 | + } |
| 251 | + |
| 252 | + }, |
| 253 | + |
| 254 | + pivotX: { |
| 255 | + |
| 256 | + enumerable: true, |
| 257 | + |
| 258 | + get: function () |
| 259 | + { |
| 260 | + return this.transform._pivotX; |
| 261 | + }, |
| 262 | + |
| 263 | + set: function (value) |
| 264 | + { |
| 265 | + this.transform._pivotX = value; |
| 266 | + this.transform.dirty = true; |
| 267 | + this.transform.updateCache(); |
| 268 | + } |
| 269 | + |
| 270 | + }, |
| 271 | + |
| 272 | + pivotY: { |
| 273 | + |
| 274 | + enumerable: true, |
| 275 | + |
| 276 | + get: function () |
| 277 | + { |
| 278 | + return this.transform._pivotY; |
| 279 | + }, |
| 280 | + |
| 281 | + set: function (value) |
| 282 | + { |
| 283 | + this.transform._pivotY = value; |
| 284 | + this.transform.dirty = true; |
| 285 | + this.transform.updateCache(); |
| 286 | + } |
| 287 | + |
| 288 | + }, |
| 289 | + |
| 290 | + angle: { |
| 291 | + |
| 292 | + enumerable: true, |
| 293 | + |
| 294 | + get: function () |
| 295 | + { |
| 296 | + return Phaser.Math.wrapAngle(this.rotation * Phaser.Math.RAD_TO_DEG); |
| 297 | + }, |
| 298 | + |
| 299 | + set: function (value) |
| 300 | + { |
| 301 | + this.rotation = Phaser.Math.wrapAngle(value) * Phaser.Math.DEG_TO_RAD; |
| 302 | + } |
| 303 | + |
| 304 | + }, |
| 305 | + |
| 306 | + rotation: { |
| 307 | + |
| 308 | + enumerable: true, |
| 309 | + |
| 310 | + get: function () |
| 311 | + { |
| 312 | + return this.transform._rotation; |
| 313 | + }, |
| 314 | + |
| 315 | + set: function (value) |
| 316 | + { |
| 317 | + if (this.transform._rotation === value) |
| 318 | + { |
| 319 | + return; |
| 320 | + } |
| 321 | + |
| 322 | + this.transform._rotation = value; |
| 323 | + this.transform.dirty = true; |
| 324 | + |
| 325 | + if (this.transform._rotation % Phaser.Math.PI2) |
| 326 | + { |
| 327 | + this.transform.cache.sr = Math.sin(this.transform._rotation); |
| 328 | + this.transform.cache.cr = Math.cos(this.transform._rotation); |
| 329 | + this.transform.updateCache(); |
| 330 | + this.transform.hasLocalRotation = true; |
| 331 | + } |
| 332 | + else |
| 333 | + { |
| 334 | + this.transform.hasLocalRotation = false; |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + } |
| 339 | + |
| 340 | +}); |
0 commit comments