BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / game.js

game.js

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