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