BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / game.js

game.js

game.js 3.5 KiB Download Raw
1class Game
2{
3 activeScenes = [];
4 pendingRemoval = [];
5
6 previousStep = 0; //used for calculating deltaTime
7 isRunning = false;
8 rafId = null;
9
10 images = {};
11
12 constructor ()
13 {
14 this.previousStep = 0;
15 this.activeScenes = [];
16 this.isRunning = false;
17 this.rafId = null;
18 this.update = this.update.bind(this);
19 }
20
21 startGame ()
22 {
23 GameInput.g_init();
24
25 if (this.isRunning)
26 { return; }
27
28 this.isRunning = true;
29 this.previousStep = Date.now();
30 this.update();
31 }
32
33 stopGame ()
34 {
35 this.isRunning = false;
36 if (this.rafId != null)
37 {
38 cancelAnimationFrame(this.rafId);
39 this.rafId = null;
40 }
41 }
42
43 preloadImagesThenStart (imageData, onComplete, onError)
44 {
45 const cont = async () =>
46 {
47 try
48 {
49 await this.preloadImages(
50 imageData
51 );
52
53 this.startGame();
54 onComplete && onComplete();
55 }
56 catch (err)
57 {
58 console.error("GAME: Image preload failed.", err);
59 onError && onError(err);
60 }
61 };
62
63 cont();
64 }
65
66 setPrimaryScene (scene)
67 {
68 if (this.activeScenes.contains(scene))
69 {
70 for (let i = 0; i < this.activeScenes.length; i++)
71 { this.activeScenes[i].g_primary = false; }
72
73 scene.g_primary = true;
74 }
75 }
76
77 async preloadImages (imagesData)
78 {
79 const promises = [];
80 const context = this;
81
82 for (var imageData of imagesData)
83 {
84 if (context.images[imageData.name])
85 {
86 console.warn("Image '" + imageData.name + "' is already loaded! (overwriting anyway)");
87 }
88
89 promises.push(new Promise((resolve, reject) =>
90 {
91 var img = new Image();
92 var imageContext = { name: imageData.name, subImgTotal: imageData.subImgTotal, perRow: imageData.perRow, loop: imageData.loop, chain: imageData.chain };
93
94 img.onload = function ()
95 {
96 var finalImage = new image(img, imageContext.subImgTotal, imageContext.perRow, imageContext.loop, imageContext.chain);
97 finalImage.name = imageContext.name;
98
99 context.images[imageContext.name] = finalImage;
100
101 resolve();
102 };
103
104 img.onerror = function ()
105 {
106 reject(new Error("Failed to load image '" + imageContext.name + "' from " + imageData.url));
107 };
108
109 img.src = imageData.url;
110 img.setAttribute("style", "display:none");
111 }));
112 }
113 await Promise.all(promises);
114
115 console.log("all images loaded ", this.images);
116
117 return this.images;
118 }
119
120 loadScene (scene)
121 {
122 if (this.activeScenes.length == 0)
123 { scene.g_primary = true; }
124
125 this.activeScenes.push(scene);
126 scene.game = this;
127 scene.onLoad();
128 }
129
130 removeScene (scene)
131 {
132 const index = this.activeScenes.indexOf(scene);
133 if (index >= 0 && !scene.destroyed)
134 {
135 if (!this.pendingRemoval.contains(index))
136 { this.pendingRemoval.push(index); }
137 }
138 }
139
140 update ()
141 {
142 this.previousStep = this.previousStep || 0;
143 const stepTime = Date.now();
144 let stepTimeCurr = (stepTime - this.previousStep) * 0.001;
145 stepTimeCurr = Math.min(stepTimeCurr, 0.1);
146 if (isNaN(stepTimeCurr))
147 { stepTimeCurr = 0; }
148
149 GameInput.g_updateKeys();
150
151 for (let i = 0; i < this.activeScenes.length; i++)
152 {
153 this.activeScenes[i].update(stepTimeCurr);
154 this.activeScenes[i].draw();
155 }
156
157 this.cleanActiveScenes();
158
159 this.previousStep = stepTime;
160
161 TWEEN.update();
162
163 if (this.isRunning)
164 { this.rafId = requestAnimationFrame(this.update); }
165 }
166
167 cleanActiveScenes ()
168 {
169 if (this.pendingRemoval.length > 0)
170 {
171 this.pendingRemoval.sort((a, b) => a - b);
172
173 for (let i = this.pendingRemoval.length - 1; i >= 0; i--)
174 {
175 if (this.activeScenes[this.pendingRemoval[i]].destroyed)
176 { this.activeScenes.removeAt(this.pendingRemoval[i]); }
177
178 this.pendingRemoval.splice(i, 1);
179 }
180 }
181 }
182}