Added arbitrary names for scenes

master
Mark 2024-02-03 11:22:22 -08:00
parent ae89f0e987
commit a6a0884737
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
2 changed files with 32 additions and 30 deletions

View File

@ -39,6 +39,8 @@ zoom_max = 2000.0
# TODO: move to user config file
ui_scale = 2
ui_landed_scene = "ui/landed.rhai"
ui_flying_scene = "ui/flying.rhai"
ui_outfitter_scene = "ui/outfitter.rhai"
start_ui_scene = "landed"
ui_scene.landed = "ui/landed.rhai"
ui_scene.flying = "ui/flying.rhai"
ui_scene.outfitter = "ui/outfitter.rhai"

View File

@ -1,4 +1,4 @@
use std::{num::NonZeroU32, path::PathBuf};
use std::{collections::HashMap, num::NonZeroU32, path::PathBuf};
use rhai::AST;
@ -7,7 +7,10 @@ pub(crate) mod syntax {
use galactica_packer::SpriteAtlas;
use rhai::Engine;
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
// Raw serde syntax structs.
// These are never seen by code outside this crate.
@ -20,9 +23,8 @@ pub(crate) mod syntax {
pub zoom_min: f32,
pub zoom_max: f32,
pub ui_scale: f32,
pub ui_flying_scene: PathBuf,
pub ui_landed_scene: PathBuf,
pub ui_outfitter_scene: PathBuf,
pub ui_scene: HashMap<String, PathBuf>,
pub start_ui_scene: String,
}
impl Config {
@ -59,18 +61,19 @@ pub(crate) mod syntax {
};
let engine = Engine::new();
let ui_landed_scene = engine
.compile_file(content_root.join(self.ui_landed_scene))
.with_context(|| format!("while loading `landed` scene"))
.with_context(|| format!("while loading config"))?;
let ui_outfitter_scene = engine
.compile_file(content_root.join(self.ui_outfitter_scene))
.with_context(|| format!("while loading `outfitter` scene"))
.with_context(|| format!("while loading config"))?;
let ui_flying_scene = engine
.compile_file(content_root.join(self.ui_flying_scene))
.with_context(|| format!("while loading `flying` scene"))
.with_context(|| format!("while loading config"))?;
let mut ui_scenes = HashMap::new();
for (n, p) in self.ui_scene {
ui_scenes.insert(
n.clone(),
engine
.compile_file(content_root.join(p))
.with_context(|| format!("while loading scene script `{}`", n))?,
);
}
if !ui_scenes.contains_key(&self.start_ui_scene) {
bail!("starting ui scene `{}` doesn't exist", self.start_ui_scene)
}
return Ok(super::Config {
sprite_root: asset_root.join(self.sprite_root),
@ -98,9 +101,8 @@ pub(crate) mod syntax {
zoom_max: self.zoom_max,
zoom_min: self.zoom_min,
ui_scale: self.ui_scale,
ui_landed_scene,
ui_flying_scene,
ui_outfitter_scene,
ui_scenes,
start_ui_scene: self.start_ui_scene,
});
}
}
@ -187,12 +189,10 @@ pub struct Config {
/// Ui scale factor
pub ui_scale: f32,
/// Ui landed scene script
pub ui_landed_scene: AST,
/// Ui scene scripts
pub ui_scenes: HashMap<String, AST>,
/// Ui flying scene script
pub ui_flying_scene: AST,
/// Ui outfitter scene script
pub ui_outfitter_scene: AST,
/// The UI scene we start in.
/// This is guaranteed to be a key in ui_scenes.
pub start_ui_scene: String,
}