| 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 | quadTree = null; |
| 15 | |
| 16 | pendingRemoval = []; |
| 17 | |
| 18 | g_primary = false; |
| 19 | |
| 20 | static DisplayModes = { absolute: 0, relativeToParent: 1 }; |
| 21 | |
| 22 | displayMode = Scene.DisplayModes.absolute; |
| 23 | |
| 24 | cv_canvas = null; |
| 25 | cv_context = null; |
| 26 | destroyed = false; |
| 27 | |
| 28 | game = null; |
| 29 | |
| 30 | get primary () |
| 31 | { |
| 32 | return this.g_primary; |
| 33 | } |
| 34 | |
| 35 | constructor (x, y, width, height, displayMode = 0) |
| 36 | { |
| 37 | Scene.idCount++; |
| 38 | this.id = 'canvas_' + Scene.idCount; |
| 39 | |
| 40 | this.position = new vector(x, y); |
| 41 | this.size = new vector(width, height); |
| 42 | this.real_position = new vector(x, y); |
| 43 | this.real_size = new vector(width, height); |
| 44 | this.cameraPosition = new vector(0, 0); |
| 45 | this.displayMode = displayMode; |
| 46 | |
| 47 | |
| 48 | const body = document.body; |
| 49 | |
| 50 | body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>'); |
| 51 | |
| 52 | this.cv_canvas = document.getElementById(this.id); |
| 53 | this.cv_context = this.cv_canvas.getContext('2d'); |
| 54 | |
| 55 | this.g_fixSize(); |
| 56 | this.quadTree = new QuadTree(this.real_size.x, this.real_size.y); |
| 57 | } |
| 58 | |
| 59 | setPrimary () |
| 60 | { |
| 61 | this.game.setPrimaryScene(this); |
| 62 | } |
| 63 | |
| 64 | onLoad () |
| 65 | { |
| 66 | |
| 67 | } |
| 68 | |
| 69 | loadChildScene (scene) |
| 70 | { |
| 71 | scene.parentScene = this; |
| 72 | this.game.loadScene(scene); |
| 73 | } |
| 74 | |
| 75 | destroy () |
| 76 | { |
| 77 | if (this.destroyed) |
| 78 | { return; } |
| 79 | |
| 80 | for (var i = 0; i < this.activeObjects.length; i++) |
| 81 | { |
| 82 | this.activeObjects[i].destroy(); |
| 83 | } |
| 84 | |
| 85 | this.destroyed = true; |
| 86 | |
| 87 | if (this.game) |
| 88 | { this.game.removeScene(this); } |
| 89 | |
| 90 | this.removeCanvas(); |
| 91 | |
| 92 | if (this.primary) |
| 93 | { |
| 94 | if (this.parentScene && !this.parentScene.destroyed) |
| 95 | { |
| 96 | this.parentScene.setPrimary(); |
| 97 | } |
| 98 | else if (this.game) |
| 99 | { |
| 100 | let promoted = null; |
| 101 | for (let i = 0; i < this.game.activeScenes.length; i++) |
| 102 | { |
| 103 | const candidate = this.game.activeScenes[i]; |
| 104 | if (candidate !== this && !candidate.destroyed) |
| 105 | { |
| 106 | promoted = candidate; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (promoted) |
| 112 | { this.game.setPrimaryScene(promoted); } |
| 113 | else |
| 114 | { this.g_primary = false; } |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | this.g_primary = false; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | removeCanvas () |
| 124 | { |
| 125 | var elem = this.cv_canvas || document.getElementById(this.id); |
| 126 | if (elem && elem.parentNode) |
| 127 | { |
| 128 | elem.parentNode.removeChild(elem); |
| 129 | } |
| 130 | |
| 131 | this.cv_canvas = null; |
| 132 | this.cv_context = null; |
| 133 | } |
| 134 | |
| 135 | backgroundColor = |
| 136 | { |
| 137 | r: 255, |
| 138 | g: 255, |
| 139 | b: 255 |
| 140 | }; |
| 141 | |
| 142 | get canvas () |
| 143 | { |
| 144 | return this.cv_canvas; |
| 145 | } |
| 146 | |
| 147 | get context () |
| 148 | { |
| 149 | return this.cv_context; |
| 150 | } |
| 151 | |
| 152 | addObject (obj) |
| 153 | { |
| 154 | if (this.destroyed) |
| 155 | { return; } |
| 156 | |
| 157 | this.activeObjects.push(obj); |
| 158 | obj.scene = this; |
| 159 | obj.initQuadtree(this.quadTree); |
| 160 | obj.checkQuadtree(); |
| 161 | |
| 162 | obj.addedToScene && obj.addedToScene(); |
| 163 | |
| 164 | return obj; |
| 165 | } |
| 166 | |
| 167 | removeObject (obj) |
| 168 | { |
| 169 | if (this.activeObjects.indexOf(obj) >= 0) |
| 170 | { |
| 171 | if (!this.pendingRemoval.contains(obj)) |
| 172 | { this.pendingRemoval.push(obj); } |
| 173 | } |
| 174 | |
| 175 | if (this.quadTree === obj.quadTree) |
| 176 | { this.quadTree.remove(obj); } |
| 177 | } |
| 178 | |
| 179 | update (deltaTime) |
| 180 | { |
| 181 | if (this.destroyed) |
| 182 | { return; } |
| 183 | |
| 184 | for (let i = 0; i < this.activeObjects.length; i++) |
| 185 | { |
| 186 | this.activeObjects[i].update(deltaTime); |
| 187 | } |
| 188 | |
| 189 | if (this.pendingRemoval.length) |
| 190 | { |
| 191 | for (let i = 0; i < this.pendingRemoval.length; i++) |
| 192 | { |
| 193 | const obj = this.pendingRemoval[i]; |
| 194 | const index = this.activeObjects.indexOf(obj); |
| 195 | if (index >= 0 && obj.destroyed) |
| 196 | { this.activeObjects.removeAt(index); } |
| 197 | } |
| 198 | |
| 199 | this.pendingRemoval.length = 0; |
| 200 | } |
| 201 | |
| 202 | this.quadTree.update(deltaTime); |
| 203 | } |
| 204 | |
| 205 | g_fixSize () |
| 206 | { |
| 207 | if (this.displayMode == Scene.DisplayModes.relativeToParent) |
| 208 | { |
| 209 | if (this.parentScene) |
| 210 | { |
| 211 | this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x); |
| 212 | this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y); |
| 213 | |
| 214 | this.real_size.x = this.parentScene.real_size.x * this.size.x; |
| 215 | this.real_size.y = this.parentScene.real_size.y * this.size.y; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | this.real_position.x = window.innerWidth * this.position.x; |
| 220 | this.real_position.y = window.innerHeight * this.position.y; |
| 221 | |
| 222 | this.real_size.x = window.innerWidth * this.size.x; |
| 223 | this.real_size.y = window.innerHeight * this.size.y; |
| 224 | } |
| 225 | } |
| 226 | else if (this.displayMode == Scene.DisplayModes.absolute) |
| 227 | { |
| 228 | this.real_position.x = this.position.x; |
| 229 | this.real_position.y = this.position.y; |
| 230 | |
| 231 | this.real_size.x = this.size.x; |
| 232 | this.real_size.y = this.size.y; |
| 233 | } |
| 234 | |
| 235 | if (this.canvas.width != this.real_size.x) |
| 236 | { this.canvas.width = this.real_size.x; } |
| 237 | |
| 238 | if (this.canvas.height != this.real_size.y) |
| 239 | { this.canvas.height = this.real_size.y; } |
| 240 | |
| 241 | if (this.px != this.real_position.x) |
| 242 | { |
| 243 | this.px = this.real_position.x; |
| 244 | this.canvas.style.left = this.real_position.x + "px"; |
| 245 | } |
| 246 | |
| 247 | if (this.py != this.real_position.y) |
| 248 | { |
| 249 | this.py = this.real_position.y; |
| 250 | this.canvas.style.top = this.real_position.y + "px"; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | draw () |
| 255 | { |
| 256 | if (this.destroyed) |
| 257 | { return; } |
| 258 | |
| 259 | this.g_fixSize(); |
| 260 | |
| 261 | if (this.renderBackground) |
| 262 | { |
| 263 | this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with |
| 264 | this.context.fillRect(0, 0, this.real_size.x, this.real_size.y); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | this.context.clearRect(0, 0, this.real_size.x, this.real_size.y); |
| 269 | } |
| 270 | |
| 271 | const sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer); |
| 272 | |
| 273 | for (var i = 0; i < sortedActiveObjects.length; i++) |
| 274 | { |
| 275 | if (!sortedActiveObjects[i].destroyed && sortedActiveObjects[i].renderer.visible) |
| 276 | { sortedActiveObjects[i].draw(this.context); } |
| 277 | } |
| 278 | } |
| 279 | } |