forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraManager.js
More file actions
231 lines (184 loc) · 5.76 KB
/
Copy pathCameraManager.js
File metadata and controls
231 lines (184 loc) · 5.76 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var Camera = require('../camera/Camera');
var Class = require('../utils/Class');
var GetFastValue = require('../utils/object/GetFastValue');
var KeyControl = require('../camera/KeyControl');
var SmoothedKeyControl = require('../camera/SmoothedKeyControl');
var CameraManager = new Class({
initialize:
function CameraManager (state)
{
// The State that owns this plugin
this.state = state;
this.cameras = [];
this.cameraPool = [];
if (state.sys.settings.cameras)
{
// We have cameras to create
this.fromJSON(state.sys.settings.cameras);
}
else
{
// Make one
this.add();
}
// Set the default camera
this.main = this.cameras[0];
},
/*
{
cameras: [
{
name: string
x: int
y: int
width: int
height: int
zoom: float
rotation: float
roundPixels: bool
scrollX: float
scrollY: float
backgroundColor: string
bounds: {
x: int
y: int
width: int
height: int
}
}
]
}
*/
fromJSON: function (config)
{
if (!Array.isArray(config))
{
config = [ config ];
}
var gameWidth = this.state.sys.game.config.width;
var gameHeight = this.state.sys.game.config.height;
for (var i = 0; i < config.length; i++)
{
var cameraConfig = config[i];
var x = GetFastValue(cameraConfig, 'x', 0);
var y = GetFastValue(cameraConfig, 'y', 0);
var width = GetFastValue(cameraConfig, 'width', gameWidth);
var height = GetFastValue(cameraConfig, 'height', gameHeight);
var camera = this.add(x, y, width, height);
// Direct properties
camera.name = GetFastValue(cameraConfig, 'name', '');
camera.zoom = GetFastValue(cameraConfig, 'zoom', 1);
camera.rotation = GetFastValue(cameraConfig, 'rotation', 0);
camera.scrollX = GetFastValue(cameraConfig, 'scrollX', 0);
camera.scrollY = GetFastValue(cameraConfig, 'scrollY', 0);
camera.roundPixels = GetFastValue(cameraConfig, 'roundPixels', false);
// Background Color
var backgroundColor = GetFastValue(cameraConfig, 'backgroundColor', false);
if (backgroundColor)
{
camera.setBackgroundColor(backgroundColor);
}
// Bounds
var boundsConfig = GetFastValue(cameraConfig, 'bounds', null);
if (boundsConfig)
{
var bx = GetFastValue(boundsConfig, 'x', 0);
var by = GetFastValue(boundsConfig, 'y', 0);
var bwidth = GetFastValue(boundsConfig, 'width', gameWidth);
var bheight = GetFastValue(boundsConfig, 'height', gameHeight);
camera.setBounds(bx, by, bwidth, bheight);
}
}
return this;
},
add: function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.state.sys.game.config.width; }
if (height === undefined) { height = this.state.sys.game.config.height; }
var camera = null;
if (this.cameraPool.length > 0)
{
camera = this.cameraPool.pop();
camera.setViewport(x, y, width, height);
}
else
{
camera = new Camera(x, y, width, height);
}
camera.setState(this.state);
this.cameras.push(camera);
return camera;
},
addKeyControl: function (config)
{
return new KeyControl(config);
},
addSmoothedKeyControl: function (config)
{
return new SmoothedKeyControl(config);
},
addReference: function (camera)
{
var index = this.cameras.indexOf(camera);
var poolIndex = this.cameraPool.indexOf(camera);
if (index < 0 && poolIndex >= 0)
{
this.cameras.push(camera);
this.cameraPool.slice(poolIndex, 1);
return camera;
}
return null;
},
remove: function (camera)
{
var cameraIndex = this.cameras.indexOf(camera);
if (cameraIndex >= 0)
{
this.cameraPool.push(this.cameras[cameraIndex]);
this.cameras.splice(cameraIndex, 1);
}
},
resetAll: function ()
{
while (this.cameras.length > 0)
{
this.cameraPool.push(this.cameras.pop());
}
this.main = this.add();
},
update: function (timestep, delta)
{
for (var i = 0, l = this.cameras.length; i < l; ++i)
{
this.cameras[i].update(timestep, delta);
}
},
render: function (renderer, children, interpolation)
{
var cameras = this.cameras;
for (var i = 0, l = cameras.length; i < l; ++i)
{
var camera = cameras[i];
camera.preRender();
renderer.render(this.state, children, interpolation, camera);
}
},
destroy: function ()
{
this.main = undefined;
for (var i = 0; i < this.cameras.length; i++)
{
this.cameras[i].destroy();
}
for (i = 0; i < this.cameraPool.length; i++)
{
this.cameraPool[i].destroy();
}
this.cameras = [];
this.cameraPool = [];
this.state = undefined;
}
});
module.exports = CameraManager;