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
15 pendingRemoval = [];
16
17 g_primary = false;
18
19 static DisplayModes = { absolute: 0, relativeToParent: 1 };
20
21 displayMode = Scene.DisplayModes.absolute;
22
23 cv_canvas = null;
24 cv_context = null;
25 destroyed = false;
26
27 game = null;
28
29 get primary ()
30 {
31 return this.g_primary;
32 }
33
34 constructor (x, y, width, height, displayMode = 0)
35 {
36 Scene.idCount++;
37 this.id = 'canvas_' + Scene.idCount;
38
39 this.position = new vector(x, y);
40 this.size = new vector(width, height);
41 this.real_position = new vector(x, y);
42 this.real_size = new vector(width, height);
43 this.cameraPosition = new vector(0, 0);
44 this.displayMode = displayMode;
45
46 let body = document.body;
47
48 body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');
49
50 this.cv_canvas = document.getElementById(this.id);
51 this.cv_context = this.cv_canvas.getContext('2d');
52
53 this.g_fixSize();
54 }
55
56 setPrimary ()
57 {
58 this.game.setPrimaryScene(this);
59 }
60
61 onLoad ()
62 {
63
64 }
65
66 loadChildScene (scene)
67 {
68 scene.parentScene = this;
69 this.game.loadScene(scene);
70 }
71
72 destroy ()
73 {
74 if (!this.destroyed)
75 {
76 for (var i = 0; i < this.activeObjects.length; i++)
77 {
78 this.activeObjects[i].destroy();
79 }
80 this.game.removeScene(this);
81
82 //this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
83
84 this.removeCanvas();
85
86 if (this.primary && this.parentScene)
87 {
88 this.parentScene.setPrimary();
89 }
90
91 this.destroyed = true;
92 }
93 }
94
95 removeCanvas ()
96 {
97 var elem = document.getElementById(this.id);
98 return elem.parentNode.removeChild(elem);
99 }
100
101 backgroundColor =
102 {
103 r: 255,
104 g: 255,
105 b: 255
106 };
107
108 get canvas ()
109 {
110 return this.cv_canvas;
111 }
112
113 get context ()
114 {
115 return this.cv_context;
116 }
117
118 addObject (obj)
119 {
120 if (this.destroyed)
121 return;
122
123 this.activeObjects.push(obj);
124 obj.scene = this;
125
126 obj.addedToScene && obj.addedToScene();
127
128 return obj;
129 }
130
131 removeObject (obj)
132 {
133 let index = this.activeObjects.indexOf(obj);
134 if (index >= 0 && !obj.destroyed)
135 {
136 if (!this.pendingRemoval.contains(index))
137 this.pendingRemoval.push(index);
138 }
139 }
140
141 update (deltaTime)
142 {
143 for (let i = 0; i < this.activeObjects.length; i++)
144 {
145 this.activeObjects[i].update(deltaTime);
146 }
147
148 if (this.pendingRemoval.length)
149 {
150 this.pendingRemoval.sort((a, b) => a - b);
151
152 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
153 {
154 if (this.activeObjects[this.pendingRemoval[i]].destroyed)
155 this.activeObjects.removeAt(this.pendingRemoval[i]);
156
157 this.pendingRemoval.splice(i, 1);
158 }
159 }
160 }
161
162 g_fixSize ()
163 {
164 if (this.displayMode == Scene.DisplayModes.relativeToParent)
165 {
166 if (this.parentScene)
167 {
168 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
169 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
170
171 this.real_size.x = this.parentScene.real_size.x * this.size.x;
172 this.real_size.y = this.parentScene.real_size.y * this.size.y;
173 }
174 else
175 {
176 this.real_position.x = window.innerWidth * this.position.x;
177 this.real_position.y = window.innerHeight * this.position.y;
178
179 this.real_size.x = window.innerWidth * this.size.x;
180 this.real_size.y = window.innerHeight * this.size.y;
181 }
182 }
183 else if (this.displayMode == Scene.DisplayModes.absolute)
184 {
185 this.real_position.x = this.position.x;
186 this.real_position.y = this.position.y;
187
188 this.real_size.x = this.size.x;
189 this.real_size.y = this.size.y;
190 }
191
192 if (this.canvas.width != this.real_size.x)
193 this.canvas.width = this.real_size.x;
194
195 if (this.canvas.height != this.real_size.y)
196 this.canvas.height = this.real_size.y;
197
198 if (this.px != this.real_position.x)
199 {
200 this.px = this.real_position.x;
201 this.canvas.style.left = this.real_position.x;
202 }
203
204 if (this.py != this.real_position.y)
205 {
206 this.py = this.real_position.y;
207 this.canvas.style.top = this.real_position.y;
208 }
209 }
210
211 draw ()
212 {
213 if (this.destroyed)
214 return;
215
216 this.g_fixSize();
217
218 if (this.renderBackground)
219 {
220 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
221 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
222 }
223 else
224 {
225 this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
226 }
227
228 let sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);
229
230 for (var i = 0; i < sortedActiveObjects.length; i++)
231 {
232 if (!sortedActiveObjects[i].destroyed && sortedActiveObjects[i].renderer.visible)
233 sortedActiveObjects[i].draw(this.context);
234 }
235 }
236}