phaser/src/cameras/controls/FixedKeyControl.js

269 lines
6.4 KiB
JavaScript
Raw Normal View History

var Class = require('../../utils/Class');
var GetValue = require('../../utils/object/GetValue');
// var camControl = new CameraControl({
// camera: this.cameras.main,
// left: cursors.left,
// right: cursors.right,
// speed: float OR { x: 0, y: 0 }
// })
var FixedKeyControl = new Class({
initialize:
2018-01-25 04:41:36 +00:00
/**
* [description]
*
* @class FixedKeyControl
* @memberOf Phaser.Cameras.Controls
* @constructor
* @since 3.0.0
*
* @param {object} config - [description]
*/
function FixedKeyControl (config)
{
2018-01-25 04:41:36 +00:00
/**
* The Camera that this Control will update.
*
* @property {Phaser.Cameras.Scene2D.Camera} camera
* @default null
* @since 3.0.0
*/
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.
*
* @property {Phaser.Input.Keyboard} left
* @default null
* @since 3.0.0
*/
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.
*
* @property {Phaser.Input.Keyboard} right
* @default null
* @since 3.0.0
*/
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.
*
* @property {Phaser.Input.Keyboard} up
* @default null
* @since 3.0.0
*/
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.
*
* @property {Phaser.Input.Keyboard} down
* @default null
* @since 3.0.0
*/
this.down = GetValue(config, 'down', null);
2018-01-25 04:41:36 +00:00
/**
* The Key to be pressed that will zoom the Camera in.
*
* @property {Phaser.Input.Keyboard} zoomIn
* @default null
* @since 3.0.0
*/
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.
*
* @property {Phaser.Input.Keyboard} zoomOut
* @default null
* @since 3.0.0
*/
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.
*
* @property {float} zoomSpeed
* @default 0.01
* @since 3.0.0
*/
this.zoomSpeed = GetValue(config, 'zoomSpeed', 0.01);
2018-01-25 04:41:36 +00:00
/**
* The horizontal speed the camera will move.
*
* @property {float} speedX
* @default 0
* @since 3.0.0
*/
/**
* The vertical speed the camera will move.
*
* @property {float} speedY
* @default 0
* @since 3.0.0
*/
var speed = GetValue(config, 'speed', null);
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);
}
2018-01-25 04:41:36 +00:00
/**
* [description]
*
* @property {number} _zoom
* @private
* @default 0
* @since 3.0.0
*/
this._zoom = 0;
2018-01-25 04:41:36 +00:00
/**
* A flag controlling if the Controls will update the Camera or not.
*
* @property {boolean} active
* @since 3.0.0
*/
this.active = (this.camera !== null);
},
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.
*/
start: function ()
{
this.active = (this.camera !== null);
2018-01-04 16:39:52 +00:00
return this;
},
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.
*/
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;
},
2018-01-25 04:41:36 +00:00
/**
* [description]
*
* @method Phaser.Cameras.Controls.FixedKeyControl#update
* @since 3.0.0
*
* @param {[type]} delta - [description]
*/
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);
}
// 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;
}
},
2018-01-25 04:41:36 +00:00
/**
* Destroys this Key Control.
*
* @method Phaser.Cameras.Controls.FixedKeyControl#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.camera = null;
this.left = null;
this.right = null;
this.up = null;
this.down = null;
this.zoomIn = null;
this.zoomOut = null;
}
});
module.exports = FixedKeyControl;