| 1 | class 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 | if (!this.destroyed) |
| 73 | { |
| 74 | for (var i = 0; i < this.activeObjects.length; i++) |
| 75 | { |
| 76 | this.activeObjects[i].destroy(); |
| 77 | } |
| 78 | this.game.removeScene(this); |
| 79 | |
| 80 | this.context.clearRect(0, 0, this.real_size.x, this.real_size.y); |
| 81 | |
| 82 | this.destroyed = true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | backgroundColor = |
| 87 | { |
| 88 | r: 255, |
| 89 | g: 255, |
| 90 | b: 255 |
| 91 | }; |
| 92 | |
| 93 | get canvas () |
| 94 | { |
| 95 | return this.cv_canvas; |
| 96 | } |
| 97 | |
| 98 | get context () |
| 99 | { |
| 100 | return this.cv_context; |
| 101 | } |
| 102 | |
| 103 | addObject (obj) |
| 104 | { |
| 105 | if (this.destroyed) |
| 106 | return; |
| 107 | |
| 108 | this.activeObjects.push(obj); |
| 109 | obj.scene = this; |
| 110 | |
| 111 | obj.addedToScene && obj.addedToScene(); |
| 112 | |
| 113 | return obj; |
| 114 | } |
| 115 | |
| 116 | removeObject (obj) |
| 117 | { |
| 118 | let index = this.activeObjects.indexOf(obj); |
| 119 | if (index >= 0 && !obj.destroyed) |
| 120 | { |
| 121 | if (!this.pendingRemoval.contains(index)) |
| 122 | this.pendingRemoval.push(index); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | update (deltaTime) |
| 127 | { |
| 128 | for (let i = 0; i < this.activeObjects.length; i++) |
| 129 | { |
| 130 | this.activeObjects[i].update(deltaTime); |
| 131 | } |
| 132 | |
| 133 | if (this.pendingRemoval.length) |
| 134 | { |
| 135 | this.pendingRemoval.sort((a, b) => a - b); |
| 136 | |
| 137 | for (let i = this.pendingRemoval.length - 1; i >= 0; i--) |
| 138 | { |
| 139 | if (this.activeObjects[this.pendingRemoval[i]].destroyed) |
| 140 | this.activeObjects.removeAt(this.pendingRemoval[i]); |
| 141 | |
| 142 | this.pendingRemoval.splice(i, 1); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | draw () |
| 148 | { |
| 149 | if (this.destroyed) |
| 150 | return; |
| 151 | |
| 152 | if (this.displayMode == Scene.DisplayModes.relativeToParent) |
| 153 | { |
| 154 | if (this.parentScene) |
| 155 | { |
| 156 | this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x); |
| 157 | this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y); |
| 158 | |
| 159 | this.real_size.x = this.parentScene.real_size.x * this.size.x; |
| 160 | this.real_size.y = this.parentScene.real_size.y * this.size.y; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | this.real_position.x = window.innerWidth * this.position.x; |
| 165 | this.real_position.y = window.innerHeight * this.position.y; |
| 166 | |
| 167 | this.real_size.x = window.innerWidth * this.size.x; |
| 168 | this.real_size.y = window.innerHeight * this.size.y; |
| 169 | } |
| 170 | } |
| 171 | else if (this.displayMode == Scene.DisplayModes.absolute) |
| 172 | { |
| 173 | this.real_position.x = this.position.x; |
| 174 | this.real_position.y = this.position.y; |
| 175 | |
| 176 | this.real_size.x = this.size.x; |
| 177 | this.real_size.y = this.size.y; |
| 178 | } |
| 179 | |
| 180 | if (this.canvas.width != this.real_size.x) |
| 181 | this.canvas.width = this.real_size.x; |
| 182 | |
| 183 | if (this.canvas.height != this.real_size.y) |
| 184 | this.canvas.height = this.real_size.y; |
| 185 | |
| 186 | if (this.px != this.real_position.x) |
| 187 | { |
| 188 | this.px = this.real_position.x; |
| 189 | this.canvas.style.left = this.real_position.x; |
| 190 | } |
| 191 | |
| 192 | if (this.py != this.real_position.y) |
| 193 | { |
| 194 | this.py = this.real_position.y; |
| 195 | this.canvas.style.top = this.real_position.y; |
| 196 | } |
| 197 | |
| 198 | if (this.renderBackground) |
| 199 | { |
| 200 | this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with |
| 201 | this.context.fillRect(0, 0, this.real_size.x, this.real_size.y); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | this.context.clearRect(0, 0, this.real_size.x, this.real_size.y); |
| 206 | } |
| 207 | |
| 208 | for (var i = 0; i < this.activeObjects.length; i++) |
| 209 | { |
| 210 | if (!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible) |
| 211 | this.activeObjects[i].draw(this.context); |
| 212 | } |
| 213 | } |
| 214 | } |