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