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 if (this.activeScenes.indexOf(scene) >= 0)
133 {
134 if (!this.pendingRemoval.contains(scene))
135 { this.pendingRemoval.push(scene); }
136 }
137 }
138
139 update ()
140 {
141 this.previousStep = this.previousStep || 0;
142 const stepTime = Date.now();
143 let stepTimeCurr = (stepTime - this.previousStep) * 0.001;
144 stepTimeCurr = Math.min(stepTimeCurr, 0.1);
145 if (isNaN(stepTimeCurr))
146 { stepTimeCurr = 0; }
147
148 GameInput.g_updateKeys();
149
150 for (let i = 0; i < this.activeScenes.length; i++)
151 {
152 this.activeScenes[i].update(stepTimeCurr);
153 this.activeScenes[i].draw();
154 }
155
156 this.cleanActiveScenes();
157
158 this.previousStep = stepTime;
159
160 TWEEN.update();
161
162 if (this.isRunning)
163 { this.rafId = requestAnimationFrame(this.update); }
164 }
165
166 cleanActiveScenes ()
167 {
168 if (this.pendingRemoval.length > 0)
169 {
170 for (let i = 0; i < this.pendingRemoval.length; i++)
171 {
172 const scene = this.pendingRemoval[i];
173 const index = this.activeScenes.indexOf(scene);
174 if (index >= 0 && scene.destroyed)
175 { this.activeScenes.removeAt(index); }
176 }
177
178 this.pendingRemoval.length = 0;
179 }
180 }
181}