use nightshade_api::nightshade::ecs::scene::SceneSettings;
use nightshade_api::prelude::*;
struct SaveLoad {
shapes: Vec<Entity>,
saved: Option<Vec<u8>>,
settings: Option<SceneSettings>,
hud: Entity,
}
fn main() {
run(setup, update).unwrap();
}
fn setup(world: &mut World) -> SaveLoad {
set_window_title(world, "Save / Load");
set_background(world, Background::Sunset);
spawn_floor(world, 10.0);
let colors = [RED, GREEN, BLUE, GOLD, PURPLE];
let mut shapes = Vec::new();
for (index, color) in colors.into_iter().enumerate() {
let shape = spawn_object(
world,
Object {
shape: Shape::Cube,
position: vec3(index as f32 * 2.0 - 4.0, 0.6, 0.0),
scale: vec3(1.0, 1.0, 1.0),
color,
..Object::default()
},
);
shapes.push(shape);
}
let hud = spawn_text(
world,
"S save M mutate L load",
ScreenAnchor::BottomLeft,
);
SaveLoad {
shapes,
saved: None,
settings: None,
hud,
}
}
fn update(world: &mut World, state: &mut SaveLoad) {
if key_pressed(world, KeyCode::KeyS) {
match save_scene(world, "demo") {
Ok(bytes) => {
let count = bytes.len();
state.saved = Some(bytes);
state.settings = Some(capture_settings(world));
set_text(world, state.hud, &format!("saved {count} bytes"));
}
Err(error) => set_text(world, state.hud, &format!("save failed: {error}")),
}
}
if key_pressed(world, KeyCode::KeyM) {
for &shape in &state.shapes {
set_color(world, shape, GRAY);
let here = position(world, shape);
set_position(world, shape, vec3(here.x, 2.5, here.z));
}
set_background(world, Background::Space);
set_text(world, state.hud, "mutated");
}
if key_pressed(world, KeyCode::KeyL)
&& let Some(bytes) = state.saved.clone()
{
for &shape in &state.shapes {
despawn(world, shape);
}
match load_scene(world, &bytes) {
Ok(roots) => {
state.shapes = roots;
if let Some(settings) = &state.settings {
apply_settings(world, settings);
}
set_text(world, state.hud, "loaded");
}
Err(error) => set_text(world, state.hud, &format!("load failed: {error}")),
}
}
}