diff options
author | Fbenas <philbeansburton@gmail.com> | 2020-09-20 22:08:03 +0100 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2020-09-20 22:08:03 +0100 |
commit | 9be73c2d0f21a539bc46974139e8c36c12845ca0 (patch) | |
tree | 3442d2254797983e7e0294c9aee00e90aa643109 /resources/js/scene.js |
Initial commit
Diffstat (limited to 'resources/js/scene.js')
-rw-r--r-- | resources/js/scene.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/resources/js/scene.js b/resources/js/scene.js new file mode 100644 index 0000000..194a77b --- /dev/null +++ b/resources/js/scene.js @@ -0,0 +1,70 @@ +class Scene { + + constructor(component_size, no_of_components) { + this.component_size = component_size; + this.no_of_components = no_of_components; + this.components = new Array; + this.gameArea = {}; + this.initGameArea(); + this.initComponents(); + this.started = false; + } + + initComponents() { + for (let i = 0; i < this.no_of_components; i++) { + let new_component = new Component( + this.component_size, + "black", + Math.random() * this.gameArea.canvas.width, + Math.random() * this.gameArea.canvas.height, + Math.random() * 360 + ); + this.components.push(new_component); + } + } + + initGameArea() { + this.gameArea = new GameArea(600, 600); + this.gameArea.init() + } + + start() { + if (this.started) { + return; + } + + // Kinda annoying, setInterval is a piece of shite and is always run from global scope + // Therefore "this.update" will be undefined unless we bind it + const updateMe = this.update.bind(this); + this.interval = setInterval(updateMe, 5); + this.started = true; + } + + update() { + this.gameArea.clear(); + for (let i = 0; i < this.no_of_components; i++) { + this.components[i].move(this.gameArea.context); + } + } + + stop() { + if (!this.started) { + return; + } + + clearInterval(this.interval); + this.interval = null; + this.started = false; + } + + reset() { + if (this.started) { + this.stop(); + } + this.components = new Array; + this.initComponents(); + this.gameArea.clear(); + this.started = false; + } + +} |