| 1 | class Scene |
| 2 | { |
| 3 | static idCount = 0; |
| 4 | id = null; |
| 5 | activeObjects = []; |
| 6 | position = null; |
| 7 | size = null; |
| 8 | real_position = null; |
| 9 | real_size = null; |
| 10 | cameraPosition = null; |
| 11 | parentScene = null; |
| 12 | |
| 13 | static DisplayModes = {absolute: 0, relativeToParent: 1} |
| 14 | |
| 15 | displayMode = Scene.DisplayModes.absolute; |
| 16 | |
| 17 | cv_canvas = null; |
| 18 | cv_context = null; |
| 19 | destroyed = false; |
| 20 | |
| 21 | game = null; |
| 22 | |
| 23 | constructor(x, y, width, height, displayMode = 0) |
| 24 | { |
| 25 | Scene.idCount++; |
| 26 | this.id = 'canvas_' + Scene.idCount; |
| 27 | |
| 28 | this.position = new vector(x, y); |
| 29 | this.size = new vector(width, height); |
| 30 | this.real_position = new vector(x, y); |
| 31 | this.real_size = new vector(width, height); |
| 32 | this.cameraPosition = new vector(0, 0) |
| 33 | this.displayMode = displayMode; |
| 34 | |
| 35 | let body = document.body; |
| 36 | |
| 37 | body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>'); |
| 38 | |
| 39 | this.cv_canvas = document.getElementById(this.id); |
| 40 | this.cv_context = this.cv_canvas.getContext('2d'); |
| 41 | } |
| 42 | |
| 43 | onLoad() |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | loadChildScene(scene) |
| 49 | { |
| 50 | scene.parentScene = this; |
| 51 | this.game.loadScene(scene); |
| 52 | } |
| 53 | |
| 54 | destroy() |
| 55 | { |
| 56 | this.destroyed = true |
| 57 | for(var i = 0; i < this.activeObjects.length; i++) |
| 58 | { |
| 59 | this.activeObjects[i].destroy(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | backgroundColor = |
| 64 | { |
| 65 | r: 255, |
| 66 | g: 255, |
| 67 | b: 255 |
| 68 | } |
| 69 | |
| 70 | get canvas() |
| 71 | { |
| 72 | return this.cv_canvas; |
| 73 | } |
| 74 | |
| 75 | get context() |
| 76 | { |
| 77 | return this.cv_context; |
| 78 | } |
| 79 | |
| 80 | addObject(obj) |
| 81 | { |
| 82 | if(this.destroyed) |
| 83 | return; |
| 84 | |
| 85 | this.activeObjects.push(obj); |
| 86 | obj.scene = this; |
| 87 | |
| 88 | obj.addedToScene && obj.addedToScene(); |
| 89 | } |
| 90 | |
| 91 | removeObject(obj) |
| 92 | { |
| 93 | if(this.activeObjects.contains(obj)) |
| 94 | { |
| 95 | this.activeObjects.remove(obj) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | update(deltaTime) |
| 100 | { |
| 101 | for(let i = 0; i < this.activeObjects.length; i++) |
| 102 | { |
| 103 | this.activeObjects[i].update(deltaTime) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | draw() |
| 108 | { |
| 109 | if(this.destroyed) |
| 110 | return; |
| 111 | |
| 112 | if(this.displayMode == Scene.DisplayModes.relativeToParent) |
| 113 | { |
| 114 | if(this.parentScene) |
| 115 | { |
| 116 | this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x); |
| 117 | this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y); |
| 118 | |
| 119 | this.real_size.x = this.parentScene.real_size.x * this.size.x; |
| 120 | this.real_size.y = this.parentScene.real_size.y * this.size.y; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | this.real_position.x = window.innerWidth * this.position.x; |
| 125 | this.real_position.y = window.innerHeight * this.position.y; |
| 126 | |
| 127 | this.real_size.x = window.innerWidth * this.size.x; |
| 128 | this.real_size.y = window.innerHeight * this.size.y; |
| 129 | } |
| 130 | } |
| 131 | else if (this.displayMode == Scene.DisplayModes.absolute) |
| 132 | { |
| 133 | this.real_position.x = this.position.x; |
| 134 | this.real_position.y = this.position.y; |
| 135 | |
| 136 | this.real_size.x = this.size.x; |
| 137 | this.real_size.y = this.size.y; |
| 138 | } |
| 139 | |
| 140 | this.canvas.width = this.real_size.x; |
| 141 | this.canvas.height = this.real_size.y; |
| 142 | |
| 143 | this.canvas.style = 'position: absolute; left: ' + this.real_position.x + 'px; top: ' + this.real_position.y + 'px;'; //todo: only call when changed? |
| 144 | |
| 145 | this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with |
| 146 | this.context.fillRect(0, 0, this.real_size.x, this.real_size.y); |
| 147 | |
| 148 | for(var i = 0; i < this.activeObjects.length; i++) |
| 149 | { |
| 150 | if(!this.activeObjects[i].destroyed && this.activeObjects[i].renderer.visible) |
| 151 | this.activeObjects[i].draw(this.context); |
| 152 | } |
| 153 | } |
| 154 | } |