update 1.0.4 (see changelog)

This commit is contained in:
MaxRobinsonTheGreat
2022-09-17 19:48:10 -05:00
parent c13aee254b
commit 734bb7165e
29 changed files with 337 additions and 70 deletions

View File

@@ -2,6 +2,7 @@ const Hyperparams = require("../Hyperparameters");
const Modes = require("./ControlModes");
const StatsPanel = require("../Stats/StatsPanel");
const WorldConfig = require("../WorldConfig");
const LoadController = require("./LoadController");
class ControlPanel {
constructor(engine) {
@@ -24,10 +25,11 @@ class ControlPanel {
this.opacity_change_rate = -0.8;
this.paused=false;
this.setHyperparamDefaults();
LoadController.control_panel = this;
}
defineMinMaxControls(){
this.control_panel_active = true;
this.control_panel_active = false;
this.no_hud = false;
$('#minimize').click ( () => {
$('.control-panel').css('display', 'none');
@@ -100,10 +102,13 @@ class ControlPanel {
};
$('.control-panel').css('display', control_panel_display);
$('.hot-controls').css('display', hot_control_display);
$('.community-section').css('display', 'block');
}
else {
$('.control-panel').css('display', 'none');
$('.hot-controls').css('display', 'none');
$('.community-section').css('display', 'none');
LoadController.close();
}
this.no_hud = !this.no_hud;
break;
@@ -219,7 +224,9 @@ class ControlPanel {
this.setPaused(false);
});
$('#load-env').click(() => {
$('#upload-env').click();
LoadController.loadJson((env)=>{
this.loadEnv(env);
});
});
$('#upload-env').change((e)=>{
let files = e.target.files;
@@ -227,14 +234,8 @@ class ControlPanel {
let reader = new FileReader();
reader.onload = (e) => {
try {
let was_running = this.engine.running;
this.setPaused(true);
let env = JSON.parse(e.target.result);
this.engine.env.loadRaw(env);
if (was_running)
this.setPaused(false);
this.updateHyperparamUIValues();
this.env_controller.resetView();
this.loadEnv(env);
} catch(except) {
console.error(except)
alert('Failed to load world');
@@ -245,6 +246,20 @@ class ControlPanel {
});
}
loadEnv(env) {
if (this.tab_id == 'stats')
this.stats_panel.stopAutoRender();
let was_running = this.engine.running;
this.setPaused(true);
this.engine.env.loadRaw(env);
if (was_running)
this.setPaused(false);
this.updateHyperparamUIValues();
this.env_controller.resetView();
if (this.tab_id == 'stats')
this.stats_panel.startAutoRender();
}
defineHyperparameterControls() {
$('#food-prod-prob').change(function() {
Hyperparams.foodProdProb = $('#food-prod-prob').val();
@@ -262,6 +277,9 @@ class ControlPanel {
$('#look-range').change(function() {
Hyperparams.lookRange = $('#look-range').val();
});
$('#see-through-self').change(function() {
Hyperparams.seeThroughSelf = this.checked;
});
$('#food-drop-rate').change(function() {
Hyperparams.foodDropProb = $('#food-drop-rate').val();
});
@@ -352,6 +370,8 @@ class ControlPanel {
$('#food-drop-rate').val(Hyperparams.foodDropProb);
$('#extra-mover-cost').val(Hyperparams.extraMoverFoodCost);
$('#look-range').val(Hyperparams.lookRange);
$('#see-through-self').prop('checked', Hyperparams.seeThroughSelf);
$('#global-mutation').val(Hyperparams.globalMutability);
if (!Hyperparams.useGlobalMutability) {
$('.global-mutation-in').css('display', 'none');
@@ -436,9 +456,7 @@ class ControlPanel {
}
setPaused(paused) {
if (paused) {
$('.pause-button').find("i").removeClass("fa-pause");
$('.pause-button').find("i").addClass("fa-play");
if (this.engine.running)

View File

@@ -4,6 +4,7 @@ const CellStates = require("../Organism/Cell/CellStates");
const Directions = require("../Organism/Directions");
const Hyperparams = require("../Hyperparameters");
const Species = require("../Stats/Species");
const LoadController = require("./LoadController");
class EditorController extends CanvasController{
constructor(env, canvas) {
@@ -119,36 +120,25 @@ class EditorController extends CanvasController{
downloadEl.click();
});
$('#load-org').click(() => {
$('#upload-org').click();
});
$('#upload-org').change((e)=>{
let files = e.target.files;
if (!files.length) {return;};
let reader = new FileReader();
reader.onload = (e) => {
try {
let org=JSON.parse(e.target.result);
this.env.clear();
this.env.organism.loadRaw(org);
this.refreshDetailsPanel();
this.env.organism.updateGrid();
this.env.renderFull();
this.env.organism.species = new Species(this.env.organism.anatomy, null, 0);
if (org.species_name)
this.env.organism.species.name = org.species_name;
if (this.mode === Modes.Clone)
$('#drop-org').click();
// have to clear the value so change() will be triggered if the same file is uploaded again
$('#upload-org')[0].value = '';
} catch(except) {
console.error(except)
alert('Failed to load organism');
}
};
reader.readAsText(files[0]);
LoadController.loadJson((org)=>{
this.loadOrg(org);
});
});
}
loadOrg(org) {
this.env.clear();
this.env.organism.loadRaw(org);
this.refreshDetailsPanel();
this.env.organism.updateGrid();
this.env.renderFull();
this.env.organism.species = new Species(this.env.organism.anatomy, null, 0);
if (org.species_name)
this.env.organism.species.name = org.species_name;
if (this.mode === Modes.Clone)
$('#drop-org').click();
}
clearDetailsPanel() {
$('#organism-details').css('display', 'none');
$('#edit-organism-details').css('display', 'none');

View File

@@ -0,0 +1,94 @@
const LoadController = {
init() {
$('#close-load-btn').click(()=>{
this.close();
});
$('#load-custom-btn').click(()=>{
$('#upload-json').click();
});
$('#community-creations-btn').click(()=>{
this.open();
});
$('#load-env-btn').click(async ()=>{
let file = $('#worlds-load-dropdown').val();
const base = `./assets/worlds/`;
let resp = await fetch(base+file+'.json');
let json = await resp.json();
this.control_panel.loadEnv(json);
this.close();
});
$('#load-org-btn').click(async ()=>{
let file = $('#organisms-load-dropdown').val();
const base = `./assets/organisms/`;
let resp = await fetch(base+file+'.json');
let json = await resp.json();
this.control_panel.editor_controller.loadOrg(json);
this.close();
$('#maximize').click();
$('#editor').click();
});
this.loadDropdown('worlds');
this.loadDropdown('organisms');
},
async loadDropdown(name) {
const base = `./assets/${name}/`;
let list = [];
try {
let resp = await fetch(base+'_list.json');
list = await resp.json();
} catch(e) {
console.error('Failed to load list: ', e);
}
let id = `#${name}-load-dropdown`
$(id).empty();
for (let opt of list) {
$(id).append(
`<option value="${opt.file}">
${opt.name}
</option>`
);
}
},
async open() {
$('.load-panel').css('display','block');
},
loadJson(callback) {
$('#upload-json').change((e)=>{
let files = e.target.files;
if (!files.length) {return;};
let reader = new FileReader();
reader.onload = (e) => {
try {
let json=JSON.parse(e.target.result);
callback(json);
this.close();
} catch(e) {
console.error(e)
alert('Failed to load');
}
$('#upload-json')[0].value = '';
};
reader.readAsText(files[0]);
});
$('#upload-json').click();
},
close() {
$('.load-panel').css('display','none');
$('#load-selected-btn').off('click');
$('#upload-json').off('change');
}
}
$(document).ready(() => {
LoadController.init();
});
module.exports = LoadController;