Started working on world

This commit is contained in:
Marlon Klaus 2025-04-06 14:45:25 +02:00
parent 7466ae03d0
commit 1700427531
4 changed files with 74 additions and 1 deletions

BIN
Content/Levels/InGame.umap (Stored with Git LFS)

Binary file not shown.

View File

@ -4,6 +4,10 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
dirs = "6.0.0"
rmp-serde = "1.3.0"
serde = { version = "1.0.217", features = ["derive"] }
thiserror = "2.0.11"
[build-dependencies] [build-dependencies]
bindgen = "0.71.1" bindgen = "0.71.1"

View File

@ -1,3 +1,5 @@
mod world;
pub fn add(left: u64, right: u64) -> u64 { pub fn add(left: u64, right: u64) -> u64 {
left + right left + right
} }

View File

@ -0,0 +1,67 @@
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("{0}")]
Custom(String),
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
RmpEncode(#[from] rmp_serde::encode::Error),
#[error("{0}")]
RmpDecode(#[from] rmp_serde::decode::Error),
}
fn get_save_dir() -> Result<PathBuf, Error> {
let path = dirs::home_dir()
.ok_or(Error::Custom("Failed to get home dir".into()))?
.join("land_of_barl");
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
Ok(path)
}
#[derive(Serialize, Deserialize)]
pub struct SerializedWorld {
name: String,
day: u32,
time: f32,
}
impl SerializedWorld {
pub fn new(name: String) -> Self {
Self {
name,
day: 0,
time: 7.0,
}
}
pub fn save(&self) -> Result<(), Error> {
let path = Self::dir(&self.name)?;
if !path.exists() {
fs::create_dir_all(&path)?;
}
let content = rmp_serde::to_vec(self)?;
fs::write(path.join("world.dat"), content)?;
Ok(())
}
pub fn load(name: &str) -> Result<Self, Error> {
let path = Self::dir(name)?;
let content = fs::read(path.join("world.dat"))?;
Ok(rmp_serde::from_slice(&content)?)
}
fn dir(name: &str) -> Result<PathBuf, Error> {
Ok(get_save_dir()?.join(name))
}
}