2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-01-18 00:08:38 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var GetValue = require('../../utils/object/GetValue');
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2018-03-19 16:21:25 +00:00
|
|
|
/**
|
|
|
|
* @typedef {object} FixedKeyControlConfig
|
|
|
|
*
|
|
|
|
* @property {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera that this Control will update.
|
|
|
|
* @property {Phaser.Input.Keyboard.Key} [left] - The Key to be pressed that will move the Camera left.
|
|
|
|
* @property {Phaser.Input.Keyboard.Key} [right] - The Key to be pressed that will move the Camera right.
|
|
|
|
* @property {Phaser.Input.Keyboard.Key} [up] - The Key to be pressed that will move the Camera up.
|
2018-07-30 23:36:04 +00:00
|
|
|
* @property {Phaser.Input.Keyboard.Key} [down] - The Key to be pressed that will move the Camera down.
|
2018-03-19 16:21:25 +00:00
|
|
|
* @property {Phaser.Input.Keyboard.Key} [zoomIn] - The Key to be pressed that will zoom the Camera in.
|
|
|
|
* @property {Phaser.Input.Keyboard.Key} [zoomOut] - The Key to be pressed that will zoom the Camera out.
|
2018-06-26 22:19:14 +00:00
|
|
|
* @property {number} [zoomSpeed=0.01] - The speed at which the camera will zoom if the `zoomIn` or `zoomOut` keys are pressed.
|
|
|
|
* @property {(number|{x:number,y:number})} [speed=0] - The horizontal and vertical speed the camera will move.
|
2018-03-19 16:21:25 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-10-19 15:14:51 +00:00
|
|
|
* A Fixed Key Camera Control.
|
|
|
|
*
|
|
|
|
* This allows you to control the movement and zoom of a camera using the defined keys.
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var camControl = new FixedKeyControl({
|
|
|
|
* camera: this.cameras.main,
|
|
|
|
* left: cursors.left,
|
|
|
|
* right: cursors.right,
|
|
|
|
* speed: float OR { x: 0, y: 0 }
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Movement is precise and has no 'smoothing' applied to it.
|
|
|
|
*
|
|
|
|
* You must call the `update` method of this controller every frame.
|
2018-02-07 15:27:21 +00:00
|
|
|
*
|
|
|
|
* @class FixedKeyControl
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Cameras.Controls
|
2018-02-07 15:27:21 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-10-19 15:14:51 +00:00
|
|
|
* @param {FixedKeyControlConfig} config - The Fixed Key Control configuration object.
|
2018-02-07 15:27:21 +00:00
|
|
|
*/
|
2018-01-18 00:08:38 +00:00
|
|
|
var FixedKeyControl = new Class({
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2018-01-18 00:08:38 +00:00
|
|
|
function FixedKeyControl (config)
|
2017-06-09 16:30:48 +00:00
|
|
|
{
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* The Camera that this Control will update.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#camera
|
2018-03-27 11:51:45 +00:00
|
|
|
* @type {?Phaser.Cameras.Scene2D.Camera}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.camera = GetValue(config, 'camera', null);
|
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* The Key to be pressed that will move the Camera left.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#left
|
2018-03-27 11:51:45 +00:00
|
|
|
* @type {?Phaser.Input.Keyboard.Key}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.left = GetValue(config, 'left', null);
|
2018-01-25 04:41:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Key to be pressed that will move the Camera right.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#right
|
2018-03-27 11:51:45 +00:00
|
|
|
* @type {?Phaser.Input.Keyboard.Key}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.right = GetValue(config, 'right', null);
|
2018-01-25 04:41:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Key to be pressed that will move the Camera up.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#up
|
2018-03-27 11:51:45 +00:00
|
|
|
* @type {?Phaser.Input.Keyboard.Key}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.up = GetValue(config, 'up', null);
|
2018-01-25 04:41:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Key to be pressed that will move the Camera down.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#down
|
2018-03-27 11:51:45 +00:00
|
|
|
* @type {?Phaser.Input.Keyboard.Key}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.down = GetValue(config, 'down', null);
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* The Key to be pressed that will zoom the Camera in.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#zoomIn
|
2018-03-27 11:51:45 +00:00
|
|
|
* @type {?Phaser.Input.Keyboard.Key}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-16 21:10:04 +00:00
|
|
|
this.zoomIn = GetValue(config, 'zoomIn', null);
|
2018-01-25 04:41:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Key to be pressed that will zoom the Camera out.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#zoomOut
|
2018-03-27 11:51:45 +00:00
|
|
|
* @type {?Phaser.Input.Keyboard.Key}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-16 21:10:04 +00:00
|
|
|
this.zoomOut = GetValue(config, 'zoomOut', null);
|
2018-01-25 04:41:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The speed at which the camera will zoom if the `zoomIn` or `zoomOut` keys are pressed.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#zoomSpeed
|
2018-06-26 22:19:14 +00:00
|
|
|
* @type {number}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default 0.01
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-16 21:10:04 +00:00
|
|
|
this.zoomSpeed = GetValue(config, 'zoomSpeed', 0.01);
|
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* The horizontal speed the camera will move.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#speedX
|
2018-06-26 22:19:14 +00:00
|
|
|
* @type {number}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-03-19 16:21:25 +00:00
|
|
|
this.speedX = 0;
|
2018-01-25 04:41:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The vertical speed the camera will move.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#speedY
|
2018-06-26 22:19:14 +00:00
|
|
|
* @type {number}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-03-19 16:55:21 +00:00
|
|
|
this.speedY = 0;
|
2018-03-19 16:21:25 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var speed = GetValue(config, 'speed', null);
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
if (typeof speed === 'number')
|
|
|
|
{
|
|
|
|
this.speedX = speed;
|
|
|
|
this.speedY = speed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.speedX = GetValue(config, 'speed.x', 0);
|
|
|
|
this.speedY = GetValue(config, 'speed.y', 0);
|
|
|
|
}
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
2018-10-19 15:14:51 +00:00
|
|
|
* Internal property to track the current zoom level.
|
2018-01-25 04:41:36 +00:00
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#_zoom
|
|
|
|
* @type {number}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @private
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-16 21:10:04 +00:00
|
|
|
this._zoom = 0;
|
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* A flag controlling if the Controls will update the Camera or not.
|
|
|
|
*
|
2018-02-12 23:51:47 +00:00
|
|
|
* @name Phaser.Cameras.Controls.FixedKeyControl#active
|
|
|
|
* @type {boolean}
|
2018-01-25 04:41:36 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.active = (this.camera !== null);
|
|
|
|
},
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* Starts the Key Control running, providing it has been linked to a camera.
|
|
|
|
*
|
|
|
|
* @method Phaser.Cameras.Controls.FixedKeyControl#start
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Cameras.Controls.FixedKeyControl} This Key Control instance.
|
|
|
|
*/
|
2017-06-09 16:30:48 +00:00
|
|
|
start: function ()
|
|
|
|
{
|
|
|
|
this.active = (this.camera !== null);
|
2018-01-04 16:39:52 +00:00
|
|
|
|
|
|
|
return this;
|
2017-06-09 16:30:48 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* Stops this Key Control from running. Call `start` to start it again.
|
|
|
|
*
|
|
|
|
* @method Phaser.Cameras.Controls.FixedKeyControl#stop
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Cameras.Controls.FixedKeyControl} This Key Control instance.
|
|
|
|
*/
|
2017-06-09 16:30:48 +00:00
|
|
|
stop: function ()
|
|
|
|
{
|
|
|
|
this.active = false;
|
2018-01-04 16:39:52 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* Binds this Key Control to a camera.
|
|
|
|
*
|
|
|
|
* @method Phaser.Cameras.Controls.FixedKeyControl#setCamera
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Cameras.Scene2D.Camera} camera - The camera to bind this Key Control to.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Cameras.Controls.FixedKeyControl} This Key Control instance.
|
|
|
|
*/
|
2018-01-04 16:39:52 +00:00
|
|
|
setCamera: function (camera)
|
|
|
|
{
|
|
|
|
this.camera = camera;
|
|
|
|
|
|
|
|
return this;
|
2017-06-09 16:30:48 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
2018-10-19 15:14:51 +00:00
|
|
|
* Applies the results of pressing the control keys to the Camera.
|
|
|
|
*
|
|
|
|
* You must call this every step, it is not called automatically.
|
2018-01-25 04:41:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Cameras.Controls.FixedKeyControl#update
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-13 07:09:44 +00:00
|
|
|
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
|
2018-01-25 04:41:36 +00:00
|
|
|
*/
|
2017-06-09 16:30:48 +00:00
|
|
|
update: function (delta)
|
|
|
|
{
|
|
|
|
if (!this.active)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delta === undefined) { delta = 1; }
|
|
|
|
|
|
|
|
var cam = this.camera;
|
|
|
|
|
|
|
|
if (this.up && this.up.isDown)
|
|
|
|
{
|
|
|
|
cam.scrollY -= ((this.speedY * delta) | 0);
|
|
|
|
}
|
|
|
|
else if (this.down && this.down.isDown)
|
|
|
|
{
|
|
|
|
cam.scrollY += ((this.speedY * delta) | 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.left && this.left.isDown)
|
|
|
|
{
|
|
|
|
cam.scrollX -= ((this.speedX * delta) | 0);
|
|
|
|
}
|
|
|
|
else if (this.right && this.right.isDown)
|
|
|
|
{
|
|
|
|
cam.scrollX += ((this.speedX * delta) | 0);
|
|
|
|
}
|
2017-08-16 21:10:04 +00:00
|
|
|
|
|
|
|
// Camera zoom
|
|
|
|
|
|
|
|
if (this.zoomIn && this.zoomIn.isDown)
|
|
|
|
{
|
|
|
|
cam.zoom -= this.zoomSpeed;
|
|
|
|
|
|
|
|
if (cam.zoom < 0.1)
|
|
|
|
{
|
|
|
|
cam.zoom = 0.1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (this.zoomOut && this.zoomOut.isDown)
|
|
|
|
{
|
|
|
|
cam.zoom += this.zoomSpeed;
|
|
|
|
}
|
2017-06-09 16:30:48 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 04:41:36 +00:00
|
|
|
/**
|
|
|
|
* Destroys this Key Control.
|
|
|
|
*
|
|
|
|
* @method Phaser.Cameras.Controls.FixedKeyControl#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-09 16:30:48 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.camera = null;
|
|
|
|
|
|
|
|
this.left = null;
|
|
|
|
this.right = null;
|
|
|
|
this.up = null;
|
|
|
|
this.down = null;
|
2017-08-16 21:10:04 +00:00
|
|
|
|
|
|
|
this.zoomIn = null;
|
|
|
|
this.zoomOut = null;
|
2017-06-09 16:30:48 +00:00
|
|
|
}
|
2017-06-30 14:47:51 +00:00
|
|
|
|
|
|
|
});
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2018-01-18 00:08:38 +00:00
|
|
|
module.exports = FixedKeyControl;
|