BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

delete canvas element
timfa wsadhelios · pushed by timfa · 4 years ago
4281139f History
scene.js 5.8 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
54 setPrimary ()
55 {
56 this.game.setPrimaryScene(this);
57 }
58
59 onLoad ()
60 {
61
62 }
63
64 loadChildScene (scene)
65 {
66 scene.parentScene = this;
67 this.game.loadScene(scene);
68 }
69
70 destroy ()
71 {
72 if (!this.destroyed)
73 {
74 for (var i = 0; i < this.activeObjects.length; i++)
75 {
76 this.activeObjects[i].destroy();
77 }
78 this.game.removeScene(this);
79
80 //this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
81
82 this.removeCanvas();
83
84 this.destroyed = true;
85 }
86 }
87
88 removeCanvas ()
89 {
90 var elem = document.getElementById(this.id);
91 return elem.parentNode.removeChild(elem);
92 }
93
94 backgroundColor =
95 {
96 r: 255,
97 g: 255,
98 b: 255
99 };
100
101 get canvas ()
102 {
103 return this.cv_canvas;
104 }
105
106 get context ()
107 {
108 return this.cv_context;
109 }
110
111 addObject (obj)
112 {
113 if (this.destroyed)
114 return;
115
116 this.activeObjects.push(obj);
117 obj.scene = this;
118
119 obj.addedToScene && obj.addedToScene();
120
121 return obj;
122 }
123
124 removeObject (obj)
125 {
126 let index = this.activeObjects.indexOf(obj);
127 if (index >= 0 && !obj.destroyed)
128 {
129 if (!this.pendingRemoval.contains(index))
130 this.pendingRemoval.push(index);
131 }
132 }
133
134 update (deltaTime)
135 {
136 for (let i = 0; i < this.activeObjects.length; i++)
137 {
138 this.activeObjects[i].update(deltaTime);
139 }
140
141 if (this.pendingRemoval.length)
142 {
143 this.pendingRemoval.sort((a, b) => a - b);
144
145 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
146 {
147 if (this.activeObjects[this.pendingRemoval[i]].destroyed)
148 this.activeObjects.removeAt(this.pendingRemoval[i]);
149
150 this.pendingRemoval.splice(i, 1);
151 }
152 }
153 }
154
155 draw ()
156 {
157 if (this.destroyed)
158 return;
159
160 if (this.displayMode == Scene.DisplayModes.relativeToParent)
161 {
162 if (this.parentScene)
163 {
164 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
165 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
166
167 this.real_size.x = this.parentScene.real_size.x * this.size.x;
168 this.real_size.y = this.parentScene.real_size.y * this.size.y;
169 }
170 else
171 {
172 this.real_position.x = window.innerWidth * this.position.x;
173 this.real_position.y = window.innerHeight * this.position.y;
174
175 this.real_size.x = window.innerWidth * this.size.x;
176 this.real_size.y = window.innerHeight * this.size.y;
177 }
178 }
179 else if (this.displayMode == Scene.DisplayModes.absolute)
180 {
181 this.real_position.x = this.position.x;
182 this.real_position.y = this.position.y;
183
184 this.real_size.x = this.size.x;
185 this.real_size.y = this.size.y;
186 }
187
188 if (this.canvas.width != this.real_size.x)
189 this.canvas.width = this.real_size.x;
190
191 if (this.canvas.height != this.real_size.y)
192 this.canvas.height = this.real_size.y;
193
194 if (this.px != this.real_position.x)
195 {
196 this.px = this.real_position.x;
197 this.canvas.style.left = this.real_position.x;
198 }
199
200 if (this.py != this.real_position.y)
201 {
202 this.py = this.real_position.y;
203 this.canvas.style.top = this.real_position.y;
204 }
205
206 if (this.renderBackground)
207 {
208 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
209 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
210 }
211 else
212 {
213 this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
214 }
215
216 this.activeObjects.sort((a, b) => a.layer - b.layer);
217
218 for (var i = 0; i < this.activeObjects.length; i++)
219 {
220 if (!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)
221 this.activeObjects[i].draw(this.context);
222 }
223 }
224}