BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

poosh
timfa Tim · pushed by timfa · 4 years ago
d21347d7 History
scene.js 5.3 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 this.destroyed = true;
73 for (var i = 0; i < this.activeObjects.length; i++)
74 {
75 this.activeObjects[i].destroy();
76 }
77 }
78
79 backgroundColor =
80 {
81 r: 255,
82 g: 255,
83 b: 255
84 };
85
86 get canvas ()
87 {
88 return this.cv_canvas;
89 }
90
91 get context ()
92 {
93 return this.cv_context;
94 }
95
96 addObject (obj)
97 {
98 if (this.destroyed)
99 return;
100
101 this.activeObjects.push(obj);
102 obj.scene = this;
103
104 obj.addedToScene && obj.addedToScene();
105
106 return obj;
107 }
108
109 removeObject (obj)
110 {
111 let index = this.activeObjects.indexOf(obj);
112 if (index >= 0 && !obj.destroyed)
113 {
114 if (!this.pendingRemoval.contains(index))
115 this.pendingRemoval.push(index);
116 }
117 }
118
119 update (deltaTime)
120 {
121 for (let i = 0; i < this.activeObjects.length; i++)
122 {
123 this.activeObjects[i].update(deltaTime);
124 }
125
126 if (this.pendingRemoval.length)
127 {
128 this.pendingRemoval.sort();
129
130 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
131 {
132 if (this.activeObjects[this.pendingRemoval[i]].destroyed)
133 this.activeObjects.removeAt(this.pendingRemoval[i]);
134
135 this.pendingRemoval.splice(i, 1);
136 }
137 }
138 }
139
140 draw ()
141 {
142 if (this.destroyed)
143 return;
144
145 if (this.displayMode == Scene.DisplayModes.relativeToParent)
146 {
147 if (this.parentScene)
148 {
149 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
150 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
151
152 this.real_size.x = this.parentScene.real_size.x * this.size.x;
153 this.real_size.y = this.parentScene.real_size.y * this.size.y;
154 }
155 else
156 {
157 this.real_position.x = window.innerWidth * this.position.x;
158 this.real_position.y = window.innerHeight * this.position.y;
159
160 this.real_size.x = window.innerWidth * this.size.x;
161 this.real_size.y = window.innerHeight * this.size.y;
162 }
163 }
164 else if (this.displayMode == Scene.DisplayModes.absolute)
165 {
166 this.real_position.x = this.position.x;
167 this.real_position.y = this.position.y;
168
169 this.real_size.x = this.size.x;
170 this.real_size.y = this.size.y;
171 }
172
173 if (this.canvas.width != this.real_size.x)
174 this.canvas.width = this.real_size.x;
175
176 if (this.canvas.height != this.real_size.y)
177 this.canvas.height = this.real_size.y;
178
179 if (this.px != this.real_position.x)
180 {
181 this.px = this.real_position.x;
182 this.canvas.style.left = this.real_position.x;
183 }
184
185 if (this.py != this.real_position.y)
186 {
187 this.py = this.real_position.y;
188 this.canvas.style.top = this.real_position.y;
189 }
190
191 if (this.renderBackground)
192 {
193 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
194 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
195 }
196 else
197 {
198 this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
199 }
200
201 for (var i = 0; i < this.activeObjects.length; i++)
202 {
203 if (!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)
204 this.activeObjects[i].draw(this.context);
205 }
206 }
207}