save on exit
Some checks failed
Some checks failed
This commit is contained in:
81
src/main.rs
81
src/main.rs
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::window::{ClosingWindow, WindowResolution};
|
||||||
use bevy::window::WindowResolution;
|
use bevy::{prelude::*, window::WindowCloseRequested};
|
||||||
use helpers::grid::{Flag, GridPlugin, Revealed, Tile, TileClickEvent, TileOffset, TileType};
|
use helpers::grid::{Flag, GridPlugin, Revealed, Tile, TileClickEvent, TileOffset, TileType};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -249,49 +249,49 @@ fn load_game(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_and_exit(
|
fn save_on_exit(
|
||||||
input: Res<ButtonInput<KeyCode>>,
|
|
||||||
world: &World,
|
world: &World,
|
||||||
type_registry: Res<AppTypeRegistry>,
|
type_registry: Res<AppTypeRegistry>,
|
||||||
|
mut close_request: EventReader<WindowCloseRequested>,
|
||||||
) {
|
) {
|
||||||
if !input.just_pressed(KeyCode::Escape) {
|
for _ in close_request.read() {
|
||||||
return;
|
println!("Saving");
|
||||||
}
|
|
||||||
|
|
||||||
let type_registry = type_registry.read();
|
let type_registry = type_registry.read();
|
||||||
|
|
||||||
let dscene = DynamicSceneBuilder::from_world(world)
|
let dscene = DynamicSceneBuilder::from_world(world)
|
||||||
.deny_all()
|
.deny_all()
|
||||||
.deny_all_resources()
|
.deny_all_resources()
|
||||||
.allow::<Tile>()
|
.allow::<Tile>()
|
||||||
.allow::<Flag>()
|
.allow::<Flag>()
|
||||||
.allow::<Revealed>()
|
.allow::<Revealed>()
|
||||||
.allow::<TileType>()
|
.allow::<TileType>()
|
||||||
.allow::<Transform>()
|
.allow::<Transform>()
|
||||||
.allow::<TileOffset>()
|
.allow::<TileOffset>()
|
||||||
.allow::<OrthographicProjection>()
|
.allow::<OrthographicProjection>()
|
||||||
.allow_resource::<Score>()
|
.allow_resource::<Score>()
|
||||||
.extract_entities(world.iter_entities().filter_map(|entity| {
|
.extract_entities(world.iter_entities().filter_map(|entity| {
|
||||||
world.entity(entity.id()).get::<Tile>()?;
|
world.entity(entity.id()).get::<Tile>()?;
|
||||||
Some(entity.id())
|
Some(entity.id())
|
||||||
}))
|
}))
|
||||||
// .extract_entities(world.iter_entities().filter_map(|entity| {
|
// .extract_entities(world.iter_entities().filter_map(|entity| {
|
||||||
// world.entity(entity.id()).get::<TileOffset>()?;
|
// world.entity(entity.id()).get::<TileOffset>()?;
|
||||||
// Some(entity.id())
|
// Some(entity.id())
|
||||||
// }))
|
// }))
|
||||||
.extract_resources()
|
.extract_resources()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let serialized = match dscene.serialize(&type_registry) {
|
let serialized = match dscene.serialize(&type_registry) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Could not serialize scene: {:?}", e);
|
eprintln!("Could not serialize scene: {:?}", e);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = std::fs::write("./assets/save.sav", serialized) {
|
||||||
|
eprintln!("Unable to write save file: {:?}", e);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(e) = std::fs::write("./assets/save.sav", serialized) {
|
|
||||||
eprintln!("Unable to write save file: {:?}", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,7 +338,8 @@ fn main() {
|
|||||||
resolution: WindowResolution::new(1280.0, 720.0),
|
resolution: WindowResolution::new(1280.0, 720.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
..default()
|
exit_condition: bevy::window::ExitCondition::OnAllClosed,
|
||||||
|
close_when_requested: true,
|
||||||
}),
|
}),
|
||||||
// bevy_inspector_egui::quick::WorldInspectorPlugin::new(),
|
// bevy_inspector_egui::quick::WorldInspectorPlugin::new(),
|
||||||
))
|
))
|
||||||
@@ -349,6 +350,6 @@ fn main() {
|
|||||||
.init_resource::<FontHandle>()
|
.init_resource::<FontHandle>()
|
||||||
.add_plugins(GridPlugin)
|
.add_plugins(GridPlugin)
|
||||||
.add_systems(Startup, (load_game, setup_camera))
|
.add_systems(Startup, (load_game, setup_camera))
|
||||||
.add_systems(Update, (tile_clicked, save_and_exit))
|
.add_systems(Update, (tile_clicked, save_on_exit))
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user