From deef995cbb2b861bc9f3df7ca924ec28116cc66d Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sat, 25 Feb 2023 18:13:52 +0100 Subject: [PATCH] Added basic world save and load mechanism --- .gitignore | 1 + Cargo.lock | 49 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/blocks.rs | 3 ++- src/screens/build.rs | 47 +++++++++++++++++++++++++++++++++++--- src/screens/inventory.rs | 3 +++ src/types.rs | 13 ++++++----- 7 files changed, 108 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a5b952f..69c5aff 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ assets/base/ assets/exp/ assets/fonts/ assets/ui/ +local.data diff --git a/Cargo.lock b/Cargo.lock index 5a1eba1..faa3389 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,6 +169,12 @@ dependencies = [ "png", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "lewton" version = "0.9.4" @@ -191,6 +197,8 @@ name = "little_town" version = "0.0.1" dependencies = [ "macroquad", + "nanoserde", + "quad-storage", ] [[package]] @@ -251,6 +259,21 @@ dependencies = [ "adler", ] +[[package]] +name = "nanoserde" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "755e7965536bc54d7c9fba2df5ada5bf835b0443fd613f0a53fa199a301839d3" +dependencies = [ + "nanoserde-derive", +] + +[[package]] +name = "nanoserde-derive" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed7a94da6c6181c35d043fc61c43ac96d3a5d739e7b8027f77650ba41504d6ab" + [[package]] name = "ndk-sys" version = "0.2.2" @@ -351,6 +374,32 @@ dependencies = [ "winapi", ] +[[package]] +name = "quad-storage" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e73edba8c792f3b5a45c68a1086f6f8c1bbda34ab9399a7faabd7dc7d4e14a0e" +dependencies = [ + "lazy_static", + "nanoserde", + "quad-storage-sys", +] + +[[package]] +name = "quad-storage-sys" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1187c77ced2cc6ae79bd6e35cc09e22159164a05c4d867e299bfbf1f801e9157" +dependencies = [ + "sapp-jsutils", +] + +[[package]] +name = "sapp-jsutils" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb8ababa867431fa6c0a178248bfe7e77b5d1de357c9848883ba8e3946bb21d4" + [[package]] name = "smallvec" version = "0.6.14" diff --git a/Cargo.toml b/Cargo.toml index 134c3e8..8aed4c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,5 @@ keywords = ["game", "isometric", "town", "macroquad"] [dependencies] macroquad = "0.3.25" +nanoserde = "0.1.32" +quad-storage = "0.1.3" diff --git a/src/blocks.rs b/src/blocks.rs index 3137be2..77946b7 100644 --- a/src/blocks.rs +++ b/src/blocks.rs @@ -1,9 +1,10 @@ use std::collections::HashMap; use crate::textures::DirectionalTexture; +use nanoserde::{DeJson, SerJson}; static mut CACHE: Option> = None; -#[derive(Eq, Hash, Clone, PartialEq)] +#[derive(Eq, Hash, Clone, PartialEq, SerJson, DeJson)] pub enum Block { Bridge, diff --git a/src/screens/build.rs b/src/screens/build.rs index c59f126..7c7a2fe 100644 --- a/src/screens/build.rs +++ b/src/screens/build.rs @@ -9,11 +9,28 @@ use crate::types::{ }; use crate::blocks::Block; use super::inventory::Inventory; +use nanoserde::{DeJson, SerJson}; + +/// simple Vec2 implementation +/// because I need to derive Serialize and Deserialize +#[derive(Copy, Clone, SerJson, DeJson)] +struct CVec2 { + x: f32, + y: f32 +} +impl CVec2 { + fn new(x: f32, y: f32) -> Self { + Self { + x, y + } + } +} /// the game camera +#[derive(SerJson, DeJson)] struct Camera { /// the centered pixel - center: Vec2, + center: CVec2, /// scales the texture scale: f32 } @@ -21,7 +38,7 @@ impl Camera { /// initialize new camera fn new() -> Self { Self { - center: Vec2::new(0.0, 0.0), + center: CVec2::new(0.0, 0.0), scale: 1.0 } } @@ -40,6 +57,8 @@ const TEXTURE_INNER_HEIGHT: f32 = 108.0; // 110 /// height of top surface const TEXTURE_DEPTH: f32 = 100.0; +/// The screen of the basic build game +#[derive(SerJson, DeJson)] pub struct BuildScreen { grid: HashMap, cam: Camera, @@ -48,6 +67,15 @@ pub struct BuildScreen { } impl BuildScreen { pub fn new() -> Self { + + // load from disk if possible + let storage = &mut quad_storage::STORAGE.lock().unwrap(); + if let Some(dt) = storage.get("build-map") { + if let Ok(bscr) = BuildScreen::deserialize_json(&dt) { + return bscr; + } + } + let mut this = Self { grid: HashMap::new(), cam: Camera::new(), @@ -61,7 +89,7 @@ impl BuildScreen { } } -fn get_screen_coords(pos: &Pos3, scale: f32, center: Vec2) -> (f32, f32) { +fn get_screen_coords(pos: &Pos3, scale: f32, center: CVec2) -> (f32, f32) { let w_i = TEXTURE_INNER_WIDTH * scale; let width = TEXTURE_WIDTH * scale; let height = TEXTURE_HEIGHT * scale; @@ -394,6 +422,19 @@ impl GameComponent for BuildScreen { self.inv.direction = Direction::West; } + // save the world + if is_key_pressed(KeyCode::N) || is_key_pressed(KeyCode::Q) { + // save game + let storage = &mut quad_storage::STORAGE.lock().unwrap(); + let json = BuildScreen::serialize_json(&self); + storage.set("build-map", &json); + + if is_key_pressed(KeyCode::Q) { + // close game + return GameEvent::Quit; + } + } + GameEvent::None } } diff --git a/src/screens/inventory.rs b/src/screens/inventory.rs index d4a31cc..2a18dc4 100644 --- a/src/screens/inventory.rs +++ b/src/screens/inventory.rs @@ -13,7 +13,10 @@ use crate::textures::AssetStore; use crate::blocks::Block; use crate::draw::draw_centered_text; use std::collections::HashMap; +use nanoserde::{DeJson, SerJson}; +/// The players inventory +#[derive(SerJson, DeJson)] pub struct Inventory { /// when set to true, the player has an infinite amount of items infinite_items: bool, diff --git a/src/types.rs b/src/types.rs index e955cdf..618ff98 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,5 +1,6 @@ use crate::textures::AssetStore; use crate::Screen; +use nanoserde::{DeJson, SerJson}; /// three dimensional coordinate /// z @@ -8,14 +9,14 @@ use crate::Screen; /// / /// / /// x -#[derive(Debug,Eq,Hash,PartialEq)] +#[derive(Debug,Eq,Hash,PartialEq, DeJson, SerJson)] pub struct Pos3 { - pub x: isize, - pub y: isize, - pub z: isize + pub x: i64, + pub y: i64, + pub z: i64 } impl Pos3 { - pub fn new(x: isize, y: isize, z: isize) -> Self { + pub fn new(x: i64, y: i64, z: i64) -> Self { Self { x, y, @@ -25,7 +26,7 @@ impl Pos3 { } /// Directions -#[derive(Clone)] +#[derive(Clone, DeJson, SerJson)] pub enum Direction { /// Left /// (left edge of screen) -- 2.38.5