class Scene { static idCount = 0; id = null; activeObjects = []; position = null; size = null; real_position = null; px = 0; py = 0; real_size = null; cameraPosition = null; parentScene = null; renderBackground = true; quadTree = null; pendingRemoval = []; g_primary = false; static DisplayModes = { absolute: 0, relativeToParent: 1 }; displayMode = Scene.DisplayModes.absolute; cv_canvas = null; glContext = null; destroyed = false; game = null; get primary () { return this.g_primary; } constructor (x, y, width, height, displayMode = 0) { Scene.idCount++; this.id = 'canvas_' + Scene.idCount; this.position = new vector(x, y); this.size = new vector(width, height); this.real_position = new vector(x, y); this.real_size = new vector(width, height); this.cameraPosition = new vector(0, 0); this.displayMode = displayMode; const body = document.body; body.insertAdjacentHTML('beforeend', ''); this.cv_canvas = document.getElementById(this.id); this.glContext = new GLContext(this.cv_canvas); this.g_fixSize(); this.quadTree = new QuadTree(this.real_size.x, this.real_size.y); } setPrimary () { this.game.setPrimaryScene(this); } onLoad () { } loadChildScene (scene) { scene.parentScene = this; this.game.loadScene(scene); } destroy () { if (this.destroyed) { return; } for (var i = 0; i < this.activeObjects.length; i++) { this.activeObjects[i].destroy(); } this.destroyed = true; if (this.game) { this.game.removeScene(this); } this.removeCanvas(); if (this.primary) { if (this.parentScene && !this.parentScene.destroyed) { this.parentScene.setPrimary(); } else if (this.game) { let promoted = null; for (let i = 0; i < this.game.activeScenes.length; i++) { const candidate = this.game.activeScenes[i]; if (candidate !== this && !candidate.destroyed) { promoted = candidate; break; } } if (promoted) { this.game.setPrimaryScene(promoted); } else { this.g_primary = false; } } else { this.g_primary = false; } } } removeCanvas () { var elem = this.cv_canvas || document.getElementById(this.id); if (elem && elem.parentNode) { elem.parentNode.removeChild(elem); } this.cv_canvas = null; this.glContext = null; } backgroundColor = { r: 255, g: 255, b: 255 }; get canvas () { return this.cv_canvas; } get context () { return this.glContext ? this.glContext.gl : null; } addObject (obj) { if (this.destroyed) { return; } this.activeObjects.push(obj); obj.scene = this; obj.initQuadtree(this.quadTree); obj.checkQuadtree(); obj.addedToScene && obj.addedToScene(); return obj; } removeObject (obj) { if (this.activeObjects.indexOf(obj) >= 0) { if (!this.pendingRemoval.contains(obj)) { this.pendingRemoval.push(obj); } } if (this.quadTree === obj.quadTree) { this.quadTree.remove(obj); } } update (deltaTime) { if (this.destroyed) { return; } for (let i = 0; i < this.activeObjects.length; i++) { this.activeObjects[i].update(deltaTime); } if (this.pendingRemoval.length) { for (let i = 0; i < this.pendingRemoval.length; i++) { const obj = this.pendingRemoval[i]; const index = this.activeObjects.indexOf(obj); if (index >= 0 && obj.destroyed) { this.activeObjects.removeAt(index); } } this.pendingRemoval.length = 0; } this.quadTree.update(deltaTime); } g_fixSize () { if (this.displayMode == Scene.DisplayModes.relativeToParent) { if (this.parentScene) { this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x); this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y); this.real_size.x = this.parentScene.real_size.x * this.size.x; this.real_size.y = this.parentScene.real_size.y * this.size.y; } else { this.real_position.x = window.innerWidth * this.position.x; this.real_position.y = window.innerHeight * this.position.y; this.real_size.x = window.innerWidth * this.size.x; this.real_size.y = window.innerHeight * this.size.y; } } else if (this.displayMode == Scene.DisplayModes.absolute) { this.real_position.x = this.position.x; this.real_position.y = this.position.y; this.real_size.x = this.size.x; this.real_size.y = this.size.y; } const canvasW = Math.max(1, Math.round(this.real_size.x)); const canvasH = Math.max(1, Math.round(this.real_size.y)); let resized = false; if (this.canvas.width != canvasW) { this.canvas.width = canvasW; resized = true; } if (this.canvas.height != canvasH) { this.canvas.height = canvasH; resized = true; } // Changing canvas dimensions resets WebGL — rebuild helper/buffers/programs. if (resized || !this.glContext) { this.glContext = new GLContext(this.cv_canvas); } else { this.glContext.setSize(this.canvas.width, this.canvas.height); } if (this.px != this.real_position.x) { this.px = this.real_position.x; this.canvas.style.left = this.real_position.x + "px"; } if (this.py != this.real_position.y) { this.py = this.real_position.y; this.canvas.style.top = this.real_position.y + "px"; } } draw () { if (this.destroyed || !this.glContext) { return; } this.g_fixSize(); if (this.renderBackground) { this.glContext.clear( this.backgroundColor.r, this.backgroundColor.g, this.backgroundColor.b, 1 ); } else { this.glContext.clearTransparent(); } const sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer); for (var i = 0; i < sortedActiveObjects.length; i++) { if (!sortedActiveObjects[i].destroyed && sortedActiveObjects[i].renderer.visible) { sortedActiveObjects[i].draw(this.context); } } } }