crude save/load system

This commit is contained in:
2024-07-12 16:33:46 +02:00
parent bedc46f901
commit 222c7e9f96
5 changed files with 10829 additions and 9 deletions

1
Cargo.lock generated
View File

@@ -2646,6 +2646,7 @@ dependencies = [
"noise",
"rand",
"rand_chacha",
"serde",
]
[[package]]

View File

@@ -31,3 +31,4 @@ imageproc = "0.25.0"
noise = { version = "0.9.0", features = ["images"] }
rand = "0.8.5"
rand_chacha = "0.3.1"
serde = { version = "1.0.204", features = ["derive"] }

10757
assets/save.sav Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,26 +1,27 @@
use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use serde::{Deserialize, Serialize};
use super::camera;
pub struct GridPlugin;
#[derive(Debug, Component, Reflect)]
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
#[reflect(Component)]
pub struct Tile {
pub x: isize,
pub y: isize,
}
#[derive(Debug, Component, Reflect)]
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
#[reflect(Component)]
pub struct Flag;
#[derive(Debug, Component, Reflect)]
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
#[reflect(Component)]
pub struct Revealed;
#[derive(Debug, Component, Reflect, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Component, Reflect, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)]
#[reflect(Component)]
pub enum TileType {
Empty,
@@ -51,7 +52,8 @@ impl From<i32> for TileType {
}
}
#[derive(Component)]
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
#[reflect(Component)]
pub struct TileOffset {
pub translation: Vec3,
pub x: isize,

View File

@@ -6,13 +6,15 @@ mod utils;
use bevy::prelude::*;
use bevy::window::WindowResolution;
use helpers::grid::{Flag, GridPlugin, Revealed, Tile, TileClickEvent, TileOffset, TileType};
use serde::{Deserialize, Serialize};
mod helpers;
#[derive(Deref, Resource)]
pub struct PlayerAlive(bool);
#[derive(Deref, Resource)]
#[derive(Deref, Resource, Reflect, Serialize, Deserialize)]
#[reflect(Resource)]
pub struct Score(usize);
impl FromWorld for PlayerAlive {
@@ -33,7 +35,6 @@ impl FromWorld for FontHandle {
}
fn setup_camera(mut commands: Commands) {
// 0x0 = Center
let translation = Vec3 {
x: -(1280.0 / 2.0),
y: -(720.0 / 2.0),
@@ -238,6 +239,62 @@ fn get_tile(x: isize, y: isize) -> TileType {
surrounding.into()
}
fn load_game(mut commands: Commands, asset_server: Res<AssetServer>) {
// "Spawning" a scene bundle creates a new entity and spawns new instances
// of the given scene's entities as children of that entity.
commands.spawn(DynamicSceneBundle {
// Scenes are loaded just like any other asset.
scene: asset_server.load("save.sav"),
..default()
});
}
fn save_and_exit(
input: Res<ButtonInput<KeyCode>>,
world: &World,
type_registry: Res<AppTypeRegistry>,
) {
if !input.just_pressed(KeyCode::Escape) {
return;
}
let type_registry = type_registry.read();
let dscene = DynamicSceneBuilder::from_world(world)
.deny_all()
.deny_all_resources()
.allow::<Tile>()
.allow::<Flag>()
.allow::<Revealed>()
.allow::<TileType>()
.allow::<Transform>()
.allow::<TileOffset>()
.allow::<OrthographicProjection>()
.allow_resource::<Score>()
.extract_entities(world.iter_entities().filter_map(|entity| {
world.entity(entity.id()).get::<Tile>()?;
Some(entity.id())
}))
// .extract_entities(world.iter_entities().filter_map(|entity| {
// world.entity(entity.id()).get::<TileOffset>()?;
// Some(entity.id())
// }))
.extract_resources()
.build();
let serialized = match dscene.serialize(&type_registry) {
Ok(s) => s,
Err(e) => {
eprintln!("Could not serialize scene: {:?}", e);
return;
}
};
if let Err(e) = std::fs::write("./assets/save.sav", serialized) {
eprintln!("Unable to write save file: {:?}", e);
}
}
fn main() {
// use imageproc::drawing::Canvas;
// // prepare an image
@@ -286,10 +343,12 @@ fn main() {
// bevy_inspector_egui::quick::WorldInspectorPlugin::new(),
))
.insert_resource(PlayerAlive(true))
.register_type::<Score>()
.register_type::<TileOffset>()
.insert_resource(Score(0))
.init_resource::<FontHandle>()
.add_plugins(GridPlugin)
.add_systems(Startup, setup_camera)
.add_systems(Update, tile_clicked)
.add_systems(Startup, (load_game, setup_camera))
.add_systems(Update, (tile_clicked, save_and_exit))
.run();
}