world. objects. chunks.

This commit is contained in:
Joshua Perry 2024-11-08 19:43:15 +00:00
commit c267d00c3c
12 changed files with 266 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

72
Cargo.lock generated Normal file
View File

@ -0,0 +1,72 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anyhow"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775"
[[package]]
name = "proc-macro2"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.214"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.214"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "settlement-engine"
version = "0.1.0"
dependencies = [
"anyhow",
"serde",
]
[[package]]
name = "syn"
version = "2.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "settlement-engine"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.92"
serde = { version = "1.0.214", features = ["derive"] }

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod types;
pub mod world;

3
src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

32
src/types.rs Normal file
View File

@ -0,0 +1,32 @@
use std::time::Duration;
pub trait GameObject {
}
pub trait Harvestable<O: GameObject> {
fn harvest_time(&self) -> Duration;
fn harvest(&self) -> O;
}
pub trait Spawnable {
fn spawn_chance(&self) -> f32;
}
pub trait Smeltable<I: Item> {
fn smelt_time(&self) -> Duration;
fn smelt (&self) -> I;
}
pub trait Stackable {
fn stack_size(&self) -> usize;
}
pub mod blocks;
pub use blocks::Block;
pub mod characters;
pub use characters::Character;
pub mod items;
pub use items::Item;

13
src/types/blocks.rs Normal file
View File

@ -0,0 +1,13 @@
use crate::types;
pub trait Block: types::GameObject {}
pub trait ItemSpace<I: types::Item> {
fn item_in_space(&self) -> I;
}
pub trait Vein {
fn vein_chance(&self) -> f32;
}
pub mod ores;

0
src/types/blocks/ores.rs Normal file
View File

14
src/types/characters.rs Normal file
View File

@ -0,0 +1,14 @@
pub trait Character {
fn profession(&self) -> Profession;
fn automation_unlocked(&self) -> bool;
fn maximum_health(&self) -> f32;
fn current_health(&self) -> f32;
fn is_unique(&self) -> bool;
}
pub enum Profession {
LumberJack,
Carpenter,
Hunter,
Butcher,
}

4
src/types/items.rs Normal file
View File

@ -0,0 +1,4 @@
use crate::types;
pub trait Item: types::GameObject {}

31
src/world.rs Normal file
View File

@ -0,0 +1,31 @@
use crate::types;
pub type WorldObject = Box<dyn types::GameObject>;
pub struct Layer {
grid: Vec<Vec<WorldObject>>,
}
pub mod chunks;
pub use chunks::Chunks;
pub struct World {
name: String,
chunks: Vec<Vec<chunks::ChunkAlias>>,
loaded_chunk: chunks::LoadedChunk,
}
impl World {
fn new(name: String, world_size: usize) -> anyhow::Result<Self> {
let chunks = chunks::generate_new_chunks(&name, world_size)?;
let middle = world_size / 2;
let loaded_chunk = chunks::LoadedChunk::from(&chunks[middle][middle]);
Ok(Self {
name,
chunks,
loaded_chunk,
})
}
}

86
src/world/chunks.rs Normal file
View File

@ -0,0 +1,86 @@
use std::{
fs::{self, File},
path::{Path, PathBuf}
};
pub fn generate_new_chunks(name: &str, world_size: usize) -> anyhow::Result<Chunks> {
let dir = PathBuf::from(name).join("chunks/");
if !dir.is_dir() {
fs::create_dir(&dir)?;
}
create_chunk_files(&dir, world_size)
}
fn create_chunk_files(dir: &Path, world_size: usize) -> anyhow::Result<Chunks> {
Ok(
(0..world_size)
.map(|x| {
let x = coordinate_to_string(&x, world_size);
(0..world_size)
.map(|y| {
let y = coordinate_to_string(&y, world_size);
let file_name = middle_chunk_file(&x, &y);
File::create(dir.join(&file_name)).unwrap();
ChunkAlias::from(dir.join(&file_name))
}).collect()
}).collect()
)
}
fn coordinate_to_string(coordinate: &usize, world_size: usize) -> String {
let digit_len = world_size.to_string().len();
let coordinate = coordinate.to_string();
let delta_len = coordinate.len() % digit_len;
"0".repeat(digit_len - delta_len) + &coordinate
}
fn middle_chunk_file(x: &str, y: &str) -> String {
x.to_string() + "_" + y + ".chunk"
}
pub trait Chunk {
fn generate() -> Self;
}
pub struct LoadedChunk {
stack: Vec<super::Layer>,
neighbours: ChunkNeighbours,
}
impl Chunk for LoadedChunk {
fn generate() -> Self {
todo!("- Generate chunk")
}
}
impl From<&ChunkAlias> for LoadedChunk {
fn from(alias: &ChunkAlias) -> Self {
todo!(
"- Return contents of path as Chunk
- If file is empty, generate new chunk"
);
}
}
#[derive(Clone)]
pub struct ChunkAlias {
file: PathBuf,
}
impl From<PathBuf> for ChunkAlias {
fn from(path: PathBuf) -> Self {
Self {
file: path,
}
}
}
pub struct ChunkNeighbours {
pos_x: ChunkAlias,
pos_y: ChunkAlias,
neg_x: ChunkAlias,
neg_y: ChunkAlias,
}
pub type Chunks = Vec<Vec<ChunkAlias>>;