@@ -316,8 +316,7 @@ class ControlPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
changeEngineSpeed(change_val) {
|
changeEngineSpeed(change_val) {
|
||||||
this.engine.stop();
|
this.engine.restart(change_val)
|
||||||
this.engine.start(change_val)
|
|
||||||
this.fps = this.engine.fps;
|
this.fps = this.engine.fps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ const ControlPanel = require('./Controllers/ControlPanel');
|
|||||||
const OrganismEditor = require('./Environments/OrganismEditor');
|
const OrganismEditor = require('./Environments/OrganismEditor');
|
||||||
const ColorScheme = require('./Rendering/ColorScheme');
|
const ColorScheme = require('./Rendering/ColorScheme');
|
||||||
|
|
||||||
const render_speed = 60;
|
// If the simulation speed is below this value, a new interval will be created to handle ui rendering
|
||||||
|
// at a reasonable speed. If it is above, the simulation interval will be used to update the ui.
|
||||||
|
const min_render_speed = 60;
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
constructor(){
|
constructor(){
|
||||||
@@ -14,8 +16,13 @@ class Engine {
|
|||||||
this.colorscheme = new ColorScheme(this.env, this.organism_editor);
|
this.colorscheme = new ColorScheme(this.env, this.organism_editor);
|
||||||
this.colorscheme.loadColorScheme();
|
this.colorscheme.loadColorScheme();
|
||||||
this.env.OriginOfLife();
|
this.env.OriginOfLife();
|
||||||
this.last_update = Date.now();
|
|
||||||
this.delta_time = 0;
|
this.sim_last_update = Date.now();
|
||||||
|
this.sim_delta_time = 0;
|
||||||
|
|
||||||
|
this.ui_last_update = Date.now();
|
||||||
|
this.ui_delta_time = 0;
|
||||||
|
|
||||||
this.actual_fps = 0;
|
this.actual_fps = 0;
|
||||||
this.running = false;
|
this.running = false;
|
||||||
}
|
}
|
||||||
@@ -24,40 +31,57 @@ class Engine {
|
|||||||
if (fps <= 0)
|
if (fps <= 0)
|
||||||
fps = 1;
|
fps = 1;
|
||||||
this.fps = fps;
|
this.fps = fps;
|
||||||
this.game_loop = setInterval(function(){this.updateDeltaTime();this.environmentUpdate();}.bind(this), 1000/fps);
|
this.sim_loop = setInterval(()=>{
|
||||||
|
this.updateSimDeltaTime();
|
||||||
|
this.environmentUpdate();
|
||||||
|
}, 1000/fps);
|
||||||
this.running = true;
|
this.running = true;
|
||||||
if (this.fps >= render_speed) {
|
if (this.fps >= min_render_speed) {
|
||||||
if (this.render_loop != null) {
|
if (this.ui_loop != null) {
|
||||||
clearInterval(this.render_loop);
|
clearInterval(this.ui_loop);
|
||||||
this.render_loop = null;
|
this.ui_loop = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
this.setRenderLoop();
|
this.setUiLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
clearInterval(this.game_loop);
|
clearInterval(this.sim_loop);
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.setRenderLoop();
|
this.setUiLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
setRenderLoop() {
|
restart(fps) {
|
||||||
if (this.render_loop == null) {
|
clearInterval(this.sim_loop);
|
||||||
this.render_loop = setInterval(function(){this.updateDeltaTime();this.necessaryUpdate();}.bind(this), 1000/render_speed);
|
this.start(fps);
|
||||||
|
}
|
||||||
|
|
||||||
|
setUiLoop() {
|
||||||
|
if (!this.ui_loop) {
|
||||||
|
this.ui_loop = setInterval(()=> {
|
||||||
|
this.updateUIDeltaTime();
|
||||||
|
this.necessaryUpdate();
|
||||||
|
}, 1000/min_render_speed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateDeltaTime() {
|
updateSimDeltaTime() {
|
||||||
this.delta_time = Date.now() - this.last_update;
|
this.sim_delta_time = Date.now() - this.sim_last_update;
|
||||||
this.last_update = Date.now();
|
this.sim_last_update = Date.now();
|
||||||
|
if (!this.ui_loop) // if the ui loop isn't running, use the sim delta time
|
||||||
|
this.ui_delta_time = this.sim_delta_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateUIDeltaTime() {
|
||||||
|
this.ui_delta_time = Date.now() - this.ui_last_update;
|
||||||
|
this.ui_last_update = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
environmentUpdate() {
|
environmentUpdate() {
|
||||||
this.env.update(this.delta_time);
|
this.actual_fps = (1000/this.sim_delta_time);
|
||||||
this.actual_fps = 1/this.delta_time*1000;
|
this.env.update(this.sim_delta_time);
|
||||||
if(this.render_loop == null){
|
if(this.ui_loop == null) {
|
||||||
this.necessaryUpdate();
|
this.necessaryUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +89,7 @@ class Engine {
|
|||||||
|
|
||||||
necessaryUpdate() {
|
necessaryUpdate() {
|
||||||
this.env.render();
|
this.env.render();
|
||||||
this.controlpanel.update(this.delta_time);
|
this.controlpanel.update(this.ui_delta_time);
|
||||||
this.organism_editor.update();
|
this.organism_editor.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user