mirror of
https://github.com/photonstorm/phaser
synced 2024-12-29 22:43:08 +00:00
337 lines
8.1 KiB
JavaScript
337 lines
8.1 KiB
JavaScript
/**
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
*/
|
|
|
|
/**
|
|
* 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.
|
|
* The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y
|
|
*
|
|
* @class Phaser.Camera
|
|
* @constructor
|
|
* @param {Phaser.Game} game - Game reference to the currently running game.
|
|
* @param {number} id - Not being used at the moment, will be when Phaser supports multiple camera
|
|
* @param {number} x - Position of the camera on the X axis
|
|
* @param {number} y - Position of the camera on the Y axis
|
|
* @param {number} width - The width of the view rectangle
|
|
* @param {number} height - The height of the view rectangle
|
|
*/
|
|
Phaser.Camera = function (state, x, y, viewportWidth, viewportHeight)
|
|
{
|
|
/**
|
|
* The State that this Camera belongs to. A Camera can only belong to one State, and a State only
|
|
* has one Camera.
|
|
* @property {Phaser.State} state
|
|
*/
|
|
this.state = state;
|
|
|
|
/**
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
|
*/
|
|
this.game = state.game;
|
|
|
|
this.viewportWidth = viewportWidth;
|
|
|
|
this.viewportHeight = viewportHeight;
|
|
|
|
this.transform = new Phaser.Component.Transform(this, x, y);
|
|
|
|
/**
|
|
* 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.
|
|
* 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
|
|
* 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.
|
|
*
|
|
* @property {Phaser.Rectangle} bounds - The Rectangle in which the Camera is bounded. Set to null to allow for movement anywhere.
|
|
*/
|
|
// this.bounds = new Phaser.Rectangle(x, y, width, height);
|
|
|
|
// this.bounds = new Phaser.Circle(x, y)
|
|
|
|
/**
|
|
* @property {boolean} atLimit - Whether this camera is flush with the World Bounds or not.
|
|
*/
|
|
this.atLimit = { x: false, y: false };
|
|
};
|
|
|
|
Phaser.Camera.prototype.constructor = Phaser.Camera;
|
|
|
|
Phaser.Camera.prototype = {
|
|
|
|
/**
|
|
* Method called to ensure the camera doesn't venture outside of the game world.
|
|
* Called automatically by Camera.update.
|
|
*
|
|
* @method Phaser.Camera#checkBounds
|
|
* @protected
|
|
*/
|
|
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;
|
|
|
|
var vx = this.x;
|
|
var vw = this.x + this.viewportWidth;
|
|
var vy = this.y;
|
|
var vh = this.y + this.viewportHeight;
|
|
|
|
// Make sure we didn't go outside the cameras bounds
|
|
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)
|
|
{
|
|
// The camera is up against the bounds, so reset the shake
|
|
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)
|
|
{
|
|
// The camera is up against the bounds, so reset the shake
|
|
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)
|
|
{
|
|
// The camera is up against the bounds, so reset the shake
|
|
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)
|
|
{
|
|
// The camera is up against the bounds, so reset the shake
|
|
this._shake.y = 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Object.defineProperties(Phaser.Camera.prototype, {
|
|
|
|
// Transform getters / setters
|
|
|
|
x: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._posX;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.transform._posX = value;
|
|
this.transform.dirty = true;
|
|
}
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._posY;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.transform._posY = value;
|
|
this.transform.dirty = true;
|
|
}
|
|
|
|
},
|
|
|
|
right: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._posX + (this.viewportWidth * this.transform._scaleX);
|
|
}
|
|
|
|
},
|
|
|
|
bottom: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._posY + (this.viewportHeight * this.transform._scaleY);
|
|
}
|
|
|
|
},
|
|
|
|
scale: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._scaleX;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.transform._scaleX = value;
|
|
this.transform._scaleY = value;
|
|
this.transform.dirty = true;
|
|
this.transform.updateCache();
|
|
}
|
|
|
|
},
|
|
|
|
scaleX: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._scaleX;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.transform._scaleX = value;
|
|
this.transform.dirty = true;
|
|
this.transform.updateCache();
|
|
}
|
|
|
|
},
|
|
|
|
scaleY: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._scaleY;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.transform._scaleY = value;
|
|
this.transform.dirty = true;
|
|
this.transform.updateCache();
|
|
}
|
|
|
|
},
|
|
|
|
pivotX: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._pivotX;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.transform._pivotX = value;
|
|
this.transform.dirty = true;
|
|
this.transform.updateCache();
|
|
}
|
|
|
|
},
|
|
|
|
pivotY: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._pivotY;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.transform._pivotY = value;
|
|
this.transform.dirty = true;
|
|
this.transform.updateCache();
|
|
}
|
|
|
|
},
|
|
|
|
angle: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return Phaser.Math.wrapAngle(this.rotation * Phaser.Math.RAD_TO_DEG);
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
this.rotation = Phaser.Math.wrapAngle(value) * Phaser.Math.DEG_TO_RAD;
|
|
}
|
|
|
|
},
|
|
|
|
rotation: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function ()
|
|
{
|
|
return this.transform._rotation;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
if (this.transform._rotation === value)
|
|
{
|
|
return;
|
|
}
|
|
|
|
this.transform._rotation = value;
|
|
this.transform.dirty = true;
|
|
|
|
if (this.transform._rotation % Phaser.Math.PI2)
|
|
{
|
|
this.transform.cache.sr = Math.sin(this.transform._rotation);
|
|
this.transform.cache.cr = Math.cos(this.transform._rotation);
|
|
this.transform.updateCache();
|
|
this.transform.hasLocalRotation = true;
|
|
}
|
|
else
|
|
{
|
|
this.transform.hasLocalRotation = false;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
});
|