var Class = require('../utils/Class'); var GetValue = require('../utils/object/GetValue'); var KeyControl = new Class({ initialize: function KeyControl(config){ 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 ); 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); } this.active = (this.camera !== null ); } , start: function (){ this.active = (this.camera !== null ); } , stop: function (){ this.active = false ; } , 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); } } , destroy: function (){ this.camera = null ; this.left = null ; this.right = null ; this.up = null ; this.down = null ; } } ); module.exports = KeyControl;