crude save/load system
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2646,6 +2646,7 @@ dependencies = [
|
|||||||
"noise",
|
"noise",
|
||||||
"rand",
|
"rand",
|
||||||
"rand_chacha",
|
"rand_chacha",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@@ -31,3 +31,4 @@ imageproc = "0.25.0"
|
|||||||
noise = { version = "0.9.0", features = ["images"] }
|
noise = { version = "0.9.0", features = ["images"] }
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
rand_chacha = "0.3.1"
|
rand_chacha = "0.3.1"
|
||||||
|
serde = { version = "1.0.204", features = ["derive"] }
|
||||||
|
10757
assets/save.sav
Normal file
10757
assets/save.sav
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,26 +1,27 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_ecs_tilemap::prelude::*;
|
use bevy_ecs_tilemap::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::camera;
|
use super::camera;
|
||||||
|
|
||||||
pub struct GridPlugin;
|
pub struct GridPlugin;
|
||||||
|
|
||||||
#[derive(Debug, Component, Reflect)]
|
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
pub x: isize,
|
pub x: isize,
|
||||||
pub y: isize,
|
pub y: isize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Component, Reflect)]
|
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Flag;
|
pub struct Flag;
|
||||||
|
|
||||||
#[derive(Debug, Component, Reflect)]
|
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Revealed;
|
pub struct Revealed;
|
||||||
|
|
||||||
#[derive(Debug, Component, Reflect, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Component, Reflect, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub enum TileType {
|
pub enum TileType {
|
||||||
Empty,
|
Empty,
|
||||||
@@ -51,7 +52,8 @@ impl From<i32> for TileType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Debug, Component, Reflect, Serialize, Deserialize)]
|
||||||
|
#[reflect(Component)]
|
||||||
pub struct TileOffset {
|
pub struct TileOffset {
|
||||||
pub translation: Vec3,
|
pub translation: Vec3,
|
||||||
pub x: isize,
|
pub x: isize,
|
||||||
|
67
src/main.rs
67
src/main.rs
@@ -6,13 +6,15 @@ mod utils;
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::window::WindowResolution;
|
use bevy::window::WindowResolution;
|
||||||
use helpers::grid::{Flag, GridPlugin, Revealed, Tile, TileClickEvent, TileOffset, TileType};
|
use helpers::grid::{Flag, GridPlugin, Revealed, Tile, TileClickEvent, TileOffset, TileType};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
mod helpers;
|
mod helpers;
|
||||||
|
|
||||||
#[derive(Deref, Resource)]
|
#[derive(Deref, Resource)]
|
||||||
pub struct PlayerAlive(bool);
|
pub struct PlayerAlive(bool);
|
||||||
|
|
||||||
#[derive(Deref, Resource)]
|
#[derive(Deref, Resource, Reflect, Serialize, Deserialize)]
|
||||||
|
#[reflect(Resource)]
|
||||||
pub struct Score(usize);
|
pub struct Score(usize);
|
||||||
|
|
||||||
impl FromWorld for PlayerAlive {
|
impl FromWorld for PlayerAlive {
|
||||||
@@ -33,7 +35,6 @@ impl FromWorld for FontHandle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn setup_camera(mut commands: Commands) {
|
fn setup_camera(mut commands: Commands) {
|
||||||
// 0x0 = Center
|
|
||||||
let translation = Vec3 {
|
let translation = Vec3 {
|
||||||
x: -(1280.0 / 2.0),
|
x: -(1280.0 / 2.0),
|
||||||
y: -(720.0 / 2.0),
|
y: -(720.0 / 2.0),
|
||||||
@@ -238,6 +239,62 @@ fn get_tile(x: isize, y: isize) -> TileType {
|
|||||||
surrounding.into()
|
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() {
|
fn main() {
|
||||||
// use imageproc::drawing::Canvas;
|
// use imageproc::drawing::Canvas;
|
||||||
// // prepare an image
|
// // prepare an image
|
||||||
@@ -286,10 +343,12 @@ fn main() {
|
|||||||
// bevy_inspector_egui::quick::WorldInspectorPlugin::new(),
|
// bevy_inspector_egui::quick::WorldInspectorPlugin::new(),
|
||||||
))
|
))
|
||||||
.insert_resource(PlayerAlive(true))
|
.insert_resource(PlayerAlive(true))
|
||||||
|
.register_type::<Score>()
|
||||||
|
.register_type::<TileOffset>()
|
||||||
.insert_resource(Score(0))
|
.insert_resource(Score(0))
|
||||||
.init_resource::<FontHandle>()
|
.init_resource::<FontHandle>()
|
||||||
.add_plugins(GridPlugin)
|
.add_plugins(GridPlugin)
|
||||||
.add_systems(Startup, setup_camera)
|
.add_systems(Startup, (load_game, setup_camera))
|
||||||
.add_systems(Update, tile_clicked)
|
.add_systems(Update, (tile_clicked, save_and_exit))
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user