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