BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

optmisih
timfa Tim · pushed by timfa · 4 years ago
68c624b6 History
scene.js 5.2 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)
113 {
114 this.pendingRemoval.push(index);
115 }
116 }
117
118 update (deltaTime)
119 {
120 for (let i = 0; i < this.activeObjects.length; i++)
121 {
122 this.activeObjects[i].update(deltaTime);
123 }
124
125 if (this.pendingRemoval.length)
126 {
127 this.pendingRemoval.sort();
128
129 for (let i = this.pendingRemoval.length; i >= 0; i++)
130 {
131 if (this.activeObjects[this.pendingRemoval[i]].destroyed)
132 this.activeObjects.removeAt(this.pendingRemoval[i]);
133 }
134 }
135 }
136
137 draw ()
138 {
139 if (this.destroyed)
140 return;
141
142 if (this.displayMode == Scene.DisplayModes.relativeToParent)
143 {
144 if (this.parentScene)
145 {
146 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
147 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
148
149 this.real_size.x = this.parentScene.real_size.x * this.size.x;
150 this.real_size.y = this.parentScene.real_size.y * this.size.y;
151 }
152 else
153 {
154 this.real_position.x = window.innerWidth * this.position.x;
155 this.real_position.y = window.innerHeight * this.position.y;
156
157 this.real_size.x = window.innerWidth * this.size.x;
158 this.real_size.y = window.innerHeight * this.size.y;
159 }
160 }
161 else if (this.displayMode == Scene.DisplayModes.absolute)
162 {
163 this.real_position.x = this.position.x;
164 this.real_position.y = this.position.y;
165
166 this.real_size.x = this.size.x;
167 this.real_size.y = this.size.y;
168 }
169
170 if (this.canvas.width != this.real_size.x)
171 this.canvas.width = this.real_size.x;
172
173 if (this.canvas.height != this.real_size.y)
174 this.canvas.height = this.real_size.y;
175
176 if (this.px != this.real_position.x)
177 {
178 this.px = this.real_position.x;
179 this.canvas.style.left = this.real_position.x;
180 }
181
182 if (this.py != this.real_position.y)
183 {
184 this.py = this.real_position.y;
185 this.canvas.style.top = this.real_position.y;
186 }
187
188 if (this.renderBackground)
189 {
190 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
191 this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
192 }
193 else
194 {
195 this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
196 }
197
198 for (var i = 0; i < this.activeObjects.length; i++)
199 {
200 if (!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible)
201 this.activeObjects[i].draw(this.context);
202 }
203 }
204}