class Scene { static idCount = 0; id = null; activeObjects = []; position = null; size = null; cameraPosition = null; parentScene = null; fullscreen = false; cv_canvas = null; cv_context = null; destroyed = false; game = null; constructor(x, y, width, height, fullscreen = false) { Scene.idCount++; this.id = 'canvas_' + Scene.idCount; this.position = new vector(x, y); this.size = new vector(width, height); this.cameraPosition = new vector(0, 0) this.fullscreen = fullscreen; if(this.fullscreen) { this.size.x = window.innerWidth; this.size.y = window.innerHeight; } let body = document.body; body.insertAdjacentHTML('beforeend', ''); this.cv_canvas = document.getElementById(this.id); this.cv_context = this.cv_canvas.getContext('2d'); } loadChildScene(scene) { scene.parentScene = this; this.game.loadScene(scene); } destroy() { this.destroyed = true for(var i = 0; i < this.activeObjects.length; i++) { this.activeObjects[i].destroy(); } } backgroundColor = { r: 255, g: 255, b: 255 } get canvas() { return this.cv_canvas; } get context() { return this.cv_context; } addObject(obj) { if(this.destroyed) return; this.activeObjects.push(obj); obj.scene = this; } removeObject(obj) { if(this.activeObjects.contains(obj)) { this.activeObjects.remove(obj) } } update(deltaTime) { for(let i = 0; i < this.activeObjects.length; i++) { this.activeObjects[i].update(deltaTime) } } draw() { if(this.destroyed) return; if(this.fullscreen) { this.size.width = window.innerWidth; this.size.height = window.innerHeight; } this.canvas.width = this.size.width; this.canvas.height = this.size.height; this.canvas.style = 'position: absolute; left: ' + this.position.x + 'px; top: ' + this.position.y + 'px;'; //todo: only call when changed? this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with this.context.fillRect(0, 0, this.size.width, this.size.height); for(var i = 0; i < this.activeObjects.length; i++) { if(!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible) this.activeObjects[i].renderer.draw(this.context); } } }