| 1 | class Scene |
| 2 | { |
| 3 | static idCount = 0; |
| 4 | id = null; |
| 5 | activeObjects = []; |
| 6 | position = null; |
| 7 | size = null; |
| 8 | real_position = null; |
| 9 | px = 0; py = 0; |
| 10 | real_size = null; |
| 11 | cameraPosition = null; |
| 12 | parentScene = null; |
| 13 | renderBackground = true; |
| 14 | quadTree = null; |
| 15 | |
| 16 | pendingRemoval = []; |
| 17 | |
| 18 | g_primary = false; |
| 19 | |
| 20 | static DisplayModes = { absolute: 0, relativeToParent: 1 }; |
| 21 | |
| 22 | displayMode = Scene.DisplayModes.absolute; |
| 23 | |
| 24 | cv_canvas = null; |
| 25 | cv_context = null; |
| 26 | destroyed = false; |
| 27 | |
| 28 | game = null; |
| 29 | |
| 30 | get primary () |
| 31 | { |
| 32 | return this.g_primary; |
| 33 | } |
| 34 | |
| 35 | constructor (x, y, width, height, displayMode = 0) |
| 36 | { |
| 37 | Scene.idCount++; |
| 38 | this.id = 'canvas_' + Scene.idCount; |
| 39 | |
| 40 | this.position = new vector(x, y); |
| 41 | this.size = new vector(width, height); |
| 42 | this.real_position = new vector(x, y); |
| 43 | this.real_size = new vector(width, height); |
| 44 | this.cameraPosition = new vector(0, 0); |
| 45 | this.displayMode = displayMode; |
| 46 | |
| 47 | |
| 48 | let body = document.body; |
| 49 | |
| 50 | body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>'); |
| 51 | |
| 52 | this.cv_canvas = document.getElementById(this.id); |
| 53 | this.cv_context = this.cv_canvas.getContext('2d'); |
| 54 | |
| 55 | this.g_fixSize(); |
| 56 | this.quadTree = new QuadTree(this.real_size.x, this.real_size.y); |
| 57 | } |
| 58 | |
| 59 | setPrimary () |
| 60 | { |
| 61 | this.game.setPrimaryScene(this); |
| 62 | } |
| 63 | |
| 64 | onLoad () |
| 65 | { |
| 66 | |
| 67 | } |
| 68 | |
| 69 | loadChildScene (scene) |
| 70 | { |
| 71 | scene.parentScene = this; |
| 72 | this.game.loadScene(scene); |
| 73 | } |
| 74 | |
| 75 | destroy () |
| 76 | { |
| 77 | if (!this.destroyed) |
| 78 | { |
| 79 | for (var i = 0; i < this.activeObjects.length; i++) |
| 80 | { |
| 81 | this.activeObjects[i].destroy(); |
| 82 | } |
| 83 | this.game.removeScene(this); |
| 84 | |
| 85 | //this.context.clearRect(0, 0, this.real_size.x, this.real_size.y); |
| 86 | |
| 87 | this.removeCanvas(); |
| 88 | |
| 89 | if (this.primary && this.parentScene) |
| 90 | { |
| 91 | this.parentScene.setPrimary(); |
| 92 | } |
| 93 | |
| 94 | this.destroyed = true; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | removeCanvas () |
| 99 | { |
| 100 | var elem = document.getElementById(this.id); |
| 101 | return elem.parentNode.removeChild(elem); |
| 102 | } |
| 103 | |
| 104 | backgroundColor = |
| 105 | { |
| 106 | r: 255, |
| 107 | g: 255, |
| 108 | b: 255 |
| 109 | }; |
| 110 | |
| 111 | get canvas () |
| 112 | { |
| 113 | return this.cv_canvas; |
| 114 | } |
| 115 | |
| 116 | get context () |
| 117 | { |
| 118 | return this.cv_context; |
| 119 | } |
| 120 | |
| 121 | addObject (obj) |
| 122 | { |
| 123 | if (this.destroyed) |
| 124 | return; |
| 125 | |
| 126 | this.activeObjects.push(obj); |
| 127 | obj.scene = this; |
| 128 | obj.initQuadtree(this.quadTree); |
| 129 | obj.checkQuadtree(); |
| 130 | |
| 131 | obj.addedToScene && obj.addedToScene(); |
| 132 | |
| 133 | return obj; |
| 134 | } |
| 135 | |
| 136 | removeObject (obj) |
| 137 | { |
| 138 | let index = this.activeObjects.indexOf(obj); |
| 139 | if (index >= 0 && !obj.destroyed) |
| 140 | { |
| 141 | if (!this.pendingRemoval.contains(index)) |
| 142 | this.pendingRemoval.push(index); |
| 143 | } |
| 144 | |
| 145 | if (this.quadTree === obj.quadTree) |
| 146 | this.quadTree.remove(obj); |
| 147 | } |
| 148 | |
| 149 | update (deltaTime) |
| 150 | { |
| 151 | for (let i = 0; i < this.activeObjects.length; i++) |
| 152 | { |
| 153 | this.activeObjects[i].update(deltaTime); |
| 154 | } |
| 155 | |
| 156 | if (this.pendingRemoval.length) |
| 157 | { |
| 158 | this.pendingRemoval.sort((a, b) => a - b); |
| 159 | |
| 160 | for (let i = this.pendingRemoval.length - 1; i >= 0; i--) |
| 161 | { |
| 162 | if (this.activeObjects[this.pendingRemoval[i]].destroyed) |
| 163 | this.activeObjects.removeAt(this.pendingRemoval[i]); |
| 164 | |
| 165 | this.pendingRemoval.splice(i, 1); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | this.quadTree.update(deltaTime); |
| 170 | } |
| 171 | |
| 172 | g_fixSize () |
| 173 | { |
| 174 | if (this.displayMode == Scene.DisplayModes.relativeToParent) |
| 175 | { |
| 176 | if (this.parentScene) |
| 177 | { |
| 178 | this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x); |
| 179 | this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y); |
| 180 | |
| 181 | this.real_size.x = this.parentScene.real_size.x * this.size.x; |
| 182 | this.real_size.y = this.parentScene.real_size.y * this.size.y; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | this.real_position.x = window.innerWidth * this.position.x; |
| 187 | this.real_position.y = window.innerHeight * this.position.y; |
| 188 | |
| 189 | this.real_size.x = window.innerWidth * this.size.x; |
| 190 | this.real_size.y = window.innerHeight * this.size.y; |
| 191 | } |
| 192 | } |
| 193 | else if (this.displayMode == Scene.DisplayModes.absolute) |
| 194 | { |
| 195 | this.real_position.x = this.position.x; |
| 196 | this.real_position.y = this.position.y; |
| 197 | |
| 198 | this.real_size.x = this.size.x; |
| 199 | this.real_size.y = this.size.y; |
| 200 | } |
| 201 | |
| 202 | if (this.canvas.width != this.real_size.x) |
| 203 | this.canvas.width = this.real_size.x; |
| 204 | |
| 205 | if (this.canvas.height != this.real_size.y) |
| 206 | this.canvas.height = this.real_size.y; |
| 207 | |
| 208 | if (this.px != this.real_position.x) |
| 209 | { |
| 210 | this.px = this.real_position.x; |
| 211 | this.canvas.style.left = this.real_position.x; |
| 212 | } |
| 213 | |
| 214 | if (this.py != this.real_position.y) |
| 215 | { |
| 216 | this.py = this.real_position.y; |
| 217 | this.canvas.style.top = this.real_position.y; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | draw () |
| 222 | { |
| 223 | if (this.destroyed) |
| 224 | return; |
| 225 | |
| 226 | this.g_fixSize(); |
| 227 | |
| 228 | if (this.renderBackground) |
| 229 | { |
| 230 | this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with |
| 231 | this.context.fillRect(0, 0, this.real_size.x, this.real_size.y); |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | this.context.clearRect(0, 0, this.real_size.x, this.real_size.y); |
| 236 | } |
| 237 | |
| 238 | let sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer); |
| 239 | |
| 240 | for (var i = 0; i < sortedActiveObjects.length; i++) |
| 241 | { |
| 242 | if (!sortedActiveObjects[i].destroyed && sortedActiveObjects[i].renderer.visible) |
| 243 | sortedActiveObjects[i].draw(this.context); |
| 244 | } |
| 245 | } |
| 246 | } |