forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyControl.js
More file actions
92 lines (73 loc) · 1.99 KB
/
Copy pathKeyControl.js
File metadata and controls
92 lines (73 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 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;