class Scene { static idCount = 0; id = null; activeObjects = []; position = new vector(0, 0); size = new vector(0, 0); cameraPosition = new vector(0, 0); parentScene = null; fullscreen = false; cv_canvas = null; cv_context = null; destroyed = false; constructor(x, y, width, height, fullscreen = false) { Scene.idCount++; this.id = 'canvas_' + Scene.idCount; this.position.x = x; this.position.y = y; this.size.x = width; this.size.y = height; this.fullscreen = fullscreen; let body = document.body; body.insertAdjacentHTML('beforeend', ''); this.cv_canvas = document.getElementById(this.id); this.cv_canvasContext = this.cv_canvas.getContext('2d'); } 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.canvas; } get context() { return this.canvasContext; } 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) } } redraw() { if(this.destroyed) return; if(this.fullscreen) { this.size.width = window.innerWidth - 2; this.size.height = window.innerHeight - 2; } 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.canvasContext.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with this.canvasContext.fillRect(0, 0, this.size.width, this.size.height); for(var i = 0; i < this.activeObjects.length; i++) { if(this.activeObjects[i].active && this.activeObjects[i].renderer.visible) this.activeObjects[i].g_draw(this.canvasContext); } } }