BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

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