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
128 lines (99 loc) · 2.72 KB
/
Copy pathKeyControl.js
File metadata and controls
128 lines (99 loc) · 2.72 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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);
this.zoomIn = GetValue(config, 'zoomIn', null);
this.zoomOut = GetValue(config, 'zoomOut', null);
this.zoomSpeed = GetValue(config, 'zoomSpeed', 0.01);
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._zoom = 0;
this.active = (this.camera !== null);
},
start: function ()
{
this.active = (this.camera !== null);
return this;
},
stop: function ()
{
this.active = false;
return this;
},
setCamera: function (camera)
{
this.camera = camera;
return this;
},
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;
}
},
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 = KeyControl;