~comcloudway/little_town

deef995cbb2b861bc9f3df7ca924ec28116cc66d — Jakob Meier 1 year, 8 months ago 8ac7794
Added basic world save and load mechanism
7 files changed, 108 insertions(+), 10 deletions(-)

M .gitignore
M Cargo.lock
M Cargo.toml
M src/blocks.rs
M src/screens/build.rs
M src/screens/inventory.rs
M src/types.rs
M .gitignore => .gitignore +1 -0
@@ 3,3 3,4 @@ assets/base/
assets/exp/
assets/fonts/
assets/ui/
local.data

M Cargo.lock => Cargo.lock +49 -0
@@ 170,6 170,12 @@ dependencies = [
]

[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 191,6 197,8 @@ name = "little_town"
version = "0.0.1"
dependencies = [
 "macroquad",
 "nanoserde",
 "quad-storage",
]

[[package]]


@@ 252,6 260,21 @@ dependencies = [
]

[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 352,6 375,32 @@ dependencies = [
]

[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +2 -0
@@ 12,3 12,5 @@ keywords = ["game", "isometric", "town", "macroquad"]

[dependencies]
macroquad = "0.3.25"
nanoserde = "0.1.32"
quad-storage = "0.1.3"

M src/blocks.rs => src/blocks.rs +2 -1
@@ 1,9 1,10 @@
use std::collections::HashMap;
use crate::textures::DirectionalTexture;
use nanoserde::{DeJson, SerJson};

static mut CACHE: Option<HashMap<&str, DirectionalTexture>> = None;

#[derive(Eq, Hash, Clone, PartialEq)]
#[derive(Eq, Hash, Clone, PartialEq, SerJson, DeJson)]
pub enum Block {
    Bridge,


M src/screens/build.rs => src/screens/build.rs +44 -3
@@ 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<Pos3, (Block, Direction)>,
    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
    }
}

M src/screens/inventory.rs => src/screens/inventory.rs +3 -0
@@ 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,

M src/types.rs => src/types.rs +7 -6
@@ 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)