BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

scene.js 6.0 KiB Download Raw
1class Scene
2{
3 static idCount = 0;
4 id = null;
5 activeObjects = [];
6 position = null;
7 size = null;
8 real_position = null;
9 px = 0; py = 0;
10 real_size = null;
11 cameraPosition = null;
12 parentScene = null;
13 renderBackground = true;
14 quadTree = null;
15
16 pendingRemoval = [];
17
18 g_primary = false;
19
20 static DisplayModes = { absolute: 0, relativeToParent: 1 };
21
22 displayMode = Scene.DisplayModes.absolute;
23
24 cv_canvas = null;
25 glContext = null;
26 destroyed = false;
27
28 game = null;
29
30 get primary ()
31 {
32 return this.g_primary;
33 }
34
35 constructor (x, y, width, height, displayMode = 0)
36 {
37 Scene.idCount++;
38 this.id = 'canvas_' + Scene.idCount;
39
40 this.position = new vector(x, y);
41 this.size = new vector(width, height);
42 this.real_position = new vector(x, y);
43 this.real_size = new vector(width, height);
44 this.cameraPosition = new vector(0, 0);
45 this.displayMode = displayMode;
46
47
48 const body = document.body;
49
50 body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');
51
52 this.cv_canvas = document.getElementById(this.id);
53 this.glContext = new GLContext(this.cv_canvas);
54
55 this.g_fixSize();
56 this.quadTree = new QuadTree(this.real_size.x, this.real_size.y);
57 }
58
59 setPrimary ()
60 {
61 this.game.setPrimaryScene(this);
62 }
63
64 onLoad ()
65 {
66
67 }
68
69 loadChildScene (scene)
70 {
71 scene.parentScene = this;
72 this.game.loadScene(scene);
73 }
74
75 destroy ()
76 {
77 if (this.destroyed)
78 { return; }
79
80 for (var i = 0; i < this.activeObjects.length; i++)
81 {
82 this.activeObjects[i].destroy();
83 }
84
85 this.destroyed = true;
86
87 if (this.game)
88 { this.game.removeScene(this); }
89
90 this.removeCanvas();
91
92 if (this.primary)
93 {
94 if (this.parentScene && !this.parentScene.destroyed)
95 {
96 this.parentScene.setPrimary();
97 }
98 else if (this.game)
99 {
100 let promoted = null;
101 for (let i = 0; i < this.game.activeScenes.length; i++)
102 {
103 const candidate = this.game.activeScenes[i];
104 if (candidate !== this && !candidate.destroyed)
105 {
106 promoted = candidate;
107 break;
108 }
109 }
110
111 if (promoted)
112 { this.game.setPrimaryScene(promoted); }
113 else
114 { this.g_primary = false; }
115 }
116 else
117 {
118 this.g_primary = false;
119 }
120 }
121 }
122
123 removeCanvas ()
124 {
125 var elem = this.cv_canvas || document.getElementById(this.id);
126 if (elem && elem.parentNode)
127 {
128 elem.parentNode.removeChild(elem);
129 }
130
131 this.cv_canvas = null;
132 this.glContext = null;
133 }
134
135 backgroundColor =
136 {
137 r: 255,
138 g: 255,
139 b: 255
140 };
141
142 get canvas ()
143 {
144 return this.cv_canvas;
145 }
146
147 get context ()
148 {
149 return this.glContext ? this.glContext.gl : null;
150 }
151
152 addObject (obj)
153 {
154 if (this.destroyed)
155 { return; }
156
157 this.activeObjects.push(obj);
158 obj.scene = this;
159 obj.initQuadtree(this.quadTree);
160 obj.checkQuadtree();
161
162 obj.addedToScene && obj.addedToScene();
163
164 return obj;
165 }
166
167 removeObject (obj)
168 {
169 if (this.activeObjects.indexOf(obj) >= 0)
170 {
171 if (!this.pendingRemoval.contains(obj))
172 { this.pendingRemoval.push(obj); }
173 }
174
175 if (this.quadTree === obj.quadTree)
176 { this.quadTree.remove(obj); }
177 }
178
179 update (deltaTime)
180 {
181 if (this.destroyed)
182 { return; }
183
184 for (let i = 0; i < this.activeObjects.length; i++)
185 {
186 this.activeObjects[i].update(deltaTime);
187 }
188
189 if (this.pendingRemoval.length)
190 {
191 for (let i = 0; i < this.pendingRemoval.length; i++)
192 {
193 const obj = this.pendingRemoval[i];
194 const index = this.activeObjects.indexOf(obj);
195 if (index >= 0 && obj.destroyed)
196 { this.activeObjects.removeAt(index); }
197 }
198
199 this.pendingRemoval.length = 0;
200 }
201
202 this.quadTree.update(deltaTime);
203 }
204
205 g_fixSize ()
206 {
207 if (this.displayMode == Scene.DisplayModes.relativeToParent)
208 {
209 if (this.parentScene)
210 {
211 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
212 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
213
214 this.real_size.x = this.parentScene.real_size.x * this.size.x;
215 this.real_size.y = this.parentScene.real_size.y * this.size.y;
216 }
217 else
218 {
219 this.real_position.x = window.innerWidth * this.position.x;
220 this.real_position.y = window.innerHeight * this.position.y;
221
222 this.real_size.x = window.innerWidth * this.size.x;
223 this.real_size.y = window.innerHeight * this.size.y;
224 }
225 }
226 else if (this.displayMode == Scene.DisplayModes.absolute)
227 {
228 this.real_position.x = this.position.x;
229 this.real_position.y = this.position.y;
230
231 this.real_size.x = this.size.x;
232 this.real_size.y = this.size.y;
233 }
234
235 const canvasW = Math.max(1, Math.round(this.real_size.x));
236 const canvasH = Math.max(1, Math.round(this.real_size.y));
237 let resized = false;
238
239 if (this.canvas.width != canvasW)
240 {
241 this.canvas.width = canvasW;
242 resized = true;
243 }
244
245 if (this.canvas.height != canvasH)
246 {
247 this.canvas.height = canvasH;
248 resized = true;
249 }
250
251 // Changing canvas dimensions resets WebGL — rebuild helper/buffers/programs.
252 if (resized || !this.glContext)
253 { this.glContext = new GLContext(this.cv_canvas); }
254 else
255 { this.glContext.setSize(this.canvas.width, this.canvas.height); }
256
257 if (this.px != this.real_position.x)
258 {
259 this.px = this.real_position.x;
260 this.canvas.style.left = this.real_position.x + "px";
261 }
262
263 if (this.py != this.real_position.y)
264 {
265 this.py = this.real_position.y;
266 this.canvas.style.top = this.real_position.y + "px";
267 }
268 }
269
270 draw ()
271 {
272 if (this.destroyed || !this.glContext)
273 { return; }
274
275 this.g_fixSize();
276
277 if (this.renderBackground)
278 {
279 this.glContext.clear(
280 this.backgroundColor.r,
281 this.backgroundColor.g,
282 this.backgroundColor.b,
283 1
284 );
285 }
286 else
287 {
288 this.glContext.clearTransparent();
289 }
290
291 const sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);
292
293 for (var i = 0; i < sortedActiveObjects.length; i++)
294 {
295 if (!sortedActiveObjects[i].destroyed && sortedActiveObjects[i].renderer.visible)
296 { sortedActiveObjects[i].draw(this.context); }
297 }
298 }
299}