Skip to content

Commit 737e21f

Browse files
committed
Added new Camera KeyControl and SmoothedKeyControl functions and exposed via the manager.
1 parent 8a23beb commit 737e21f

4 files changed

Lines changed: 306 additions & 0 deletions

File tree

v3/src/camera/KeyControl.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
var GetValue = require('../utils/object/GetValue');
2+
3+
// var camControl = new CameraControl({
4+
// camera: this.cameras.main,
5+
// left: cursors.left,
6+
// right: cursors.right,
7+
// speed: float OR { x: 0, y: 0 }
8+
// })
9+
10+
var KeyControl = function (config)
11+
{
12+
this.camera = GetValue(config, 'camera', null);
13+
14+
this.left = GetValue(config, 'left', null);
15+
this.right = GetValue(config, 'right', null);
16+
this.up = GetValue(config, 'up', null);
17+
this.down = GetValue(config, 'down', null);
18+
19+
var speed = GetValue(config, 'speed', null);
20+
21+
if (typeof speed === 'number')
22+
{
23+
this.speedX = speed;
24+
this.speedY = speed;
25+
}
26+
else
27+
{
28+
this.speedX = GetValue(config, 'speed.x', 0);
29+
this.speedY = GetValue(config, 'speed.y', 0);
30+
}
31+
32+
this.active = (this.camera !== null);
33+
};
34+
35+
KeyControl.prototype.constructor = KeyControl;
36+
37+
KeyControl.prototype = {
38+
39+
start: function ()
40+
{
41+
this.active = (this.camera !== null);
42+
},
43+
44+
stop: function ()
45+
{
46+
this.active = false;
47+
},
48+
49+
update: function (delta)
50+
{
51+
if (!this.active)
52+
{
53+
return;
54+
}
55+
56+
if (delta === undefined) { delta = 1; }
57+
58+
var cam = this.camera;
59+
60+
if (this.up && this.up.isDown)
61+
{
62+
cam.scrollY -= ((this.speedY * delta) | 0);
63+
}
64+
else if (this.down && this.down.isDown)
65+
{
66+
cam.scrollY += ((this.speedY * delta) | 0);
67+
}
68+
69+
if (this.left && this.left.isDown)
70+
{
71+
cam.scrollX -= ((this.speedX * delta) | 0);
72+
}
73+
else if (this.right && this.right.isDown)
74+
{
75+
cam.scrollX += ((this.speedX * delta) | 0);
76+
}
77+
},
78+
79+
destroy: function ()
80+
{
81+
this.camera = null;
82+
83+
this.left = null;
84+
this.right = null;
85+
this.up = null;
86+
this.down = null;
87+
}
88+
};
89+
90+
module.exports = KeyControl;
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
var GetValue = require('../utils/object/GetValue');
2+
3+
// var camControl = new SmoothedKeyControl({
4+
// camera: this.cameras.main,
5+
// left: cursors.left,
6+
// right: cursors.right,
7+
// acceleration: float || { x: 0, y: 0 }
8+
// drag: float || { x: 0, y: 0 }
9+
// maxSpeed: float || { x: 0, y: 0 }
10+
// })
11+
12+
var SmoothedKeyControl = function (config)
13+
{
14+
this.camera = GetValue(config, 'camera', null);
15+
16+
this.left = GetValue(config, 'left', null);
17+
this.right = GetValue(config, 'right', null);
18+
this.up = GetValue(config, 'up', null);
19+
this.down = GetValue(config, 'down', null);
20+
21+
var accel = GetValue(config, 'acceleration', null);
22+
23+
if (typeof accel === 'number')
24+
{
25+
this.accelX = accel;
26+
this.accelY = accel;
27+
}
28+
else
29+
{
30+
this.accelX = GetValue(config, 'acceleration.x', 0);
31+
this.accelY = GetValue(config, 'acceleration.y', 0);
32+
}
33+
34+
var drag = GetValue(config, 'drag', null);
35+
36+
if (typeof drag === 'number')
37+
{
38+
this.dragX = drag;
39+
this.dragY = drag;
40+
}
41+
else
42+
{
43+
this.dragX = GetValue(config, 'drag.x', 0);
44+
this.dragY = GetValue(config, 'drag.y', 0);
45+
}
46+
47+
var maxSpeed = GetValue(config, 'maxSpeed', null);
48+
49+
if (typeof maxSpeed === 'number')
50+
{
51+
this.maxSpeedX = maxSpeed;
52+
this.maxSpeedY = maxSpeed;
53+
}
54+
else
55+
{
56+
this.maxSpeedX = GetValue(config, 'maxSpeed.x', 0);
57+
this.maxSpeedY = GetValue(config, 'maxSpeed.y', 0);
58+
}
59+
60+
this._speedX = 0;
61+
this._speedY = 0;
62+
63+
this.active = (this.camera !== null);
64+
};
65+
66+
SmoothedKeyControl.prototype.constructor = SmoothedKeyControl;
67+
68+
SmoothedKeyControl.prototype = {
69+
70+
start: function ()
71+
{
72+
this.active = (this.camera !== null);
73+
},
74+
75+
stop: function ()
76+
{
77+
this.active = false;
78+
},
79+
80+
update: function (delta)
81+
{
82+
if (!this.active)
83+
{
84+
return;
85+
}
86+
87+
if (delta === undefined) { delta = 1; }
88+
89+
var cam = this.camera;
90+
91+
// Apply Deceleration
92+
93+
if (this._speedX > 0)
94+
{
95+
this._speedX -= this.dragX * delta;
96+
97+
if (this._speedX < 0)
98+
{
99+
this._speedX = 0;
100+
}
101+
}
102+
else if (this._speedX < 0)
103+
{
104+
this._speedX += this.dragX * delta;
105+
106+
if (this._speedX > 0)
107+
{
108+
this._speedX = 0;
109+
}
110+
}
111+
112+
if (this._speedY > 0)
113+
{
114+
this._speedY -= this.dragY * delta;
115+
116+
if (this._speedY < 0)
117+
{
118+
this._speedY = 0;
119+
}
120+
}
121+
else if (this._speedY < 0)
122+
{
123+
this._speedY += this.dragY * delta;
124+
125+
if (this._speedY > 0)
126+
{
127+
this._speedY = 0;
128+
}
129+
}
130+
131+
// Check for keys
132+
133+
if (this.up.isDown)
134+
{
135+
this._speedY += this.accelY;
136+
137+
if (this._speedY > this.maxSpeedY)
138+
{
139+
this._speedY = this.maxSpeedY;
140+
}
141+
}
142+
else if (this.down.isDown)
143+
{
144+
this._speedY -= this.accelY;
145+
146+
if (this._speedY < -this.maxSpeedY)
147+
{
148+
this._speedY = -this.maxSpeedY;
149+
}
150+
}
151+
152+
if (this.left.isDown)
153+
{
154+
this._speedX += this.accelX;
155+
156+
if (this._speedX > this.maxSpeedX)
157+
{
158+
this._speedX = this.maxSpeedX;
159+
}
160+
}
161+
else if (this.right.isDown)
162+
{
163+
this._speedX -= this.accelX;
164+
165+
if (this._speedX < -this.maxSpeedX)
166+
{
167+
this._speedX = -this.maxSpeedX;
168+
}
169+
}
170+
171+
// Apply to Camera
172+
173+
if (this._speedX !== 0)
174+
{
175+
cam.scrollX -= ((this._speedX * delta) | 0);
176+
}
177+
178+
if (this._speedY !== 0)
179+
{
180+
cam.scrollY -= ((this._speedY * delta) | 0);
181+
}
182+
},
183+
184+
destroy: function ()
185+
{
186+
this.camera = null;
187+
188+
this.left = null;
189+
this.right = null;
190+
this.up = null;
191+
this.down = null;
192+
}
193+
};
194+
195+
module.exports = SmoothedKeyControl;

v3/src/camera/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Phaser.Cameras
2+
3+
module.exports = {
4+
5+
Camera: require('./Camera'),
6+
KeyControl: require('./KeyControl'),
7+
SmoothedKeyControl: require('./SmoothedKeyControl')
8+
9+
};

v3/src/state/systems/CameraManager.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var Camera = require('../../camera/Camera');
2+
var KeyControl = require('../../camera/KeyControl');
3+
var SmoothedKeyControl = require('../../camera/SmoothedKeyControl');
24

35
var CameraManager = function (state)
46
{
@@ -40,6 +42,16 @@ CameraManager.prototype = {
4042
return camera;
4143
},
4244

45+
addKeyControl: function (config)
46+
{
47+
return new KeyControl(config);
48+
},
49+
50+
addSmoothedKeyControl: function (config)
51+
{
52+
return new SmoothedKeyControl(config);
53+
},
54+
4355
addReference: function (camera)
4456
{
4557
var index = this.cameras.indexOf(camera);

0 commit comments

Comments
 (0)