2018-01-16 15:06:47 +00:00
|
|
|
var Class = require('../../../utils/Class');
|
|
|
|
var GetValue = require('../../../utils/object/GetValue');
|
2017-06-09 16:30:48 +00:00
|
|
|
|
|
|
|
// var camControl = new CameraControl({
|
|
|
|
// camera: this.cameras.main,
|
|
|
|
// left: cursors.left,
|
|
|
|
// right: cursors.right,
|
|
|
|
// speed: float OR { x: 0, y: 0 }
|
|
|
|
// })
|
|
|
|
|
2018-01-16 15:06:47 +00:00
|
|
|
// Phaser.Cameras.Scene2D.Controls.KeyControl
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var KeyControl = 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
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
function KeyControl (config)
|
2017-06-09 16:30:48 +00:00
|
|
|
{
|
2017-06-30 14:47:51 +00:00
|
|
|
this.camera = GetValue(config, 'camera', null);
|
|
|
|
|
|
|
|
this.left = GetValue(config, 'left', null);
|
|
|
|
this.right = GetValue(config, 'right', null);
|
|
|
|
this.up = GetValue(config, 'up', null);
|
|
|
|
this.down = GetValue(config, 'down', null);
|
2017-06-09 16:30:48 +00:00
|
|
|
|
2017-08-16 21:10:04 +00:00
|
|
|
this.zoomIn = GetValue(config, 'zoomIn', null);
|
|
|
|
this.zoomOut = GetValue(config, 'zoomOut', null);
|
|
|
|
this.zoomSpeed = GetValue(config, 'zoomSpeed', 0.01);
|
|
|
|
|
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
|
|
|
|
2017-08-16 21:10:04 +00:00
|
|
|
this._zoom = 0;
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.active = (this.camera !== null);
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
|
|
|
|
stop: function ()
|
|
|
|
{
|
|
|
|
this.active = false;
|
2018-01-04 16:39:52 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCamera: function (camera)
|
|
|
|
{
|
|
|
|
this.camera = camera;
|
|
|
|
|
|
|
|
return this;
|
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
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
module.exports = KeyControl;
|