| 1 | class Scene |
| 2 | { |
| 3 | static idCount = 0; |
| 4 | id = null; |
| 5 | activeObjects = []; |
| 6 | position = new vector(0, 0); |
| 7 | size = new vector(0, 0); |
| 8 | cameraPosition = new vector(0, 0); |
| 9 | parentScene = null; |
| 10 | fullscreen = false; |
| 11 | |
| 12 | cv_canvas = null; |
| 13 | cv_context = null; |
| 14 | destroyed = false; |
| 15 | |
| 16 | constructor(x, y, width, height, fullscreen = false) |
| 17 | { |
| 18 | Scene.idCount++; |
| 19 | this.id = 'canvas_' + Scene.idCount; |
| 20 | |
| 21 | this.position.x = x; |
| 22 | this.position.y = y; |
| 23 | this.size.x = width; |
| 24 | this.size.y = height; |
| 25 | this.fullscreen = fullscreen; |
| 26 | |
| 27 | let body = document.body; |
| 28 | |
| 29 | body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>'); |
| 30 | |
| 31 | this.cv_canvas = document.getElementById(this.id); |
| 32 | this.cv_canvasContext = this.cv_canvas.getContext('2d'); |
| 33 | } |
| 34 | |
| 35 | destroy() |
| 36 | { |
| 37 | this.destroyed = true |
| 38 | for(var i = 0; i < this.activeObjects.length; i++) |
| 39 | { |
| 40 | this.activeObjects[i].destroy(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | backgroundColor = |
| 45 | { |
| 46 | r: 255, |
| 47 | g: 255, |
| 48 | b: 255 |
| 49 | } |
| 50 | |
| 51 | get canvas() |
| 52 | { |
| 53 | return this.canvas; |
| 54 | } |
| 55 | |
| 56 | get context() |
| 57 | { |
| 58 | return this.canvasContext; |
| 59 | } |
| 60 | |
| 61 | addObject(obj) |
| 62 | { |
| 63 | if(this.destroyed) |
| 64 | return; |
| 65 | |
| 66 | this.activeObjects.push(obj); |
| 67 | obj.scene = this; |
| 68 | } |
| 69 | |
| 70 | removeObject(obj) |
| 71 | { |
| 72 | if(this.activeObjects.contains(obj)) |
| 73 | { |
| 74 | this.activeObjects.remove(obj) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | update(deltaTime) |
| 79 | { |
| 80 | for(let i = 0; i < this.activeObjects.length; i++) |
| 81 | { |
| 82 | this.activeObjects[i].update(deltaTime) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | redraw() |
| 87 | { |
| 88 | if(this.destroyed) |
| 89 | return; |
| 90 | |
| 91 | if(this.fullscreen) |
| 92 | { |
| 93 | this.size.width = window.innerWidth - 2; |
| 94 | this.size.height = window.innerHeight - 2; |
| 95 | } |
| 96 | |
| 97 | this.canvas.width = this.size.width; |
| 98 | this.canvas.height = this.size.height; |
| 99 | |
| 100 | this.canvas.style = 'position: absolute; left: ' + this.position.x + 'px; top: ' + this.position.y + 'px;'; //todo: only call when changed? |
| 101 | |
| 102 | this.canvasContext.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with |
| 103 | this.canvasContext.fillRect(0, 0, this.size.width, this.size.height); |
| 104 | |
| 105 | for(var i = 0; i < this.activeObjects.length; i++) |
| 106 | { |
| 107 | if(this.activeObjects[i].active && this.activeObjects[i].renderer.visible) |
| 108 | this.activeObjects[i].g_draw(this.canvasContext); |
| 109 | } |
| 110 | } |
| 111 | } |