external config

This commit is contained in:
2026-03-18 23:19:58 +00:00
parent a5154f73dd
commit 15c0d1c7a2
9 changed files with 87 additions and 10 deletions
Generated
+34 -2
View File
@@ -2342,6 +2342,8 @@ dependencies = [
"rand 0.10.0", "rand 0.10.0",
"rayon", "rayon",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"serde",
"toml",
] ]
[[package]] [[package]]
@@ -2650,9 +2652,9 @@ checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]] [[package]]
name = "futures-lite" name = "futures-lite"
version = "2.6.0" version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5edaec856126859abb19ed65f39e90fea3a9574b9707f13539acf4abf7eb532" checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad"
dependencies = [ dependencies = [
"fastrand", "fastrand",
"futures-core", "futures-core",
@@ -4729,6 +4731,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776"
dependencies = [
"serde_core",
]
[[package]] [[package]]
name = "sharded-slab" name = "sharded-slab"
version = "0.1.7" version = "0.1.7"
@@ -5055,6 +5066,21 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "toml"
version = "0.9.12+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf92845e79fc2e2def6a5d828f0801e29a2f8acc037becc5ab08595c7d5e9863"
dependencies = [
"indexmap",
"serde_core",
"serde_spanned",
"toml_datetime 0.7.5+spec-1.1.0",
"toml_parser",
"toml_writer",
"winnow",
]
[[package]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.6.9" version = "0.6.9"
@@ -5102,6 +5128,12 @@ dependencies = [
"winnow", "winnow",
] ]
[[package]]
name = "toml_writer"
version = "1.0.7+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f17aaa1c6e3dc22b1da4b6bba97d066e354c7945cac2f7852d4e4e7ca7a6b56d"
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.44" version = "0.1.44"
+3 -1
View File
@@ -10,11 +10,13 @@ rand = "0.10.0"
bevy_rand = { version = "0.14.2", features = ["wyrand"] } bevy_rand = { version = "0.14.2", features = ["wyrand"] }
image = "0.25.10" image = "0.25.10"
bevy_platform = "0.18.1" bevy_platform = "0.18.1"
serde = { version = "1.0", features = ["derive"] }
toml = "0.9.8"
rayon = "1.11.0" rayon = "1.11.0"
rustc-hash = "2.1.1" rustc-hash = "2.1.1"
ahash = "0.8.12" ahash = "0.8.12"
nohash-hasher = "0.2.0" nohash-hasher = "0.2.0"
futures-lite = "2.6" futures-lite = "2.6.1"
# Enable max optimizations for dependencies, but not for our code: # Enable max optimizations for dependencies, but not for our code:
[profile.dev.package."*"] [profile.dev.package."*"]
+6
View File
@@ -0,0 +1,6 @@
initial_chunk_radius = 7
[spawn_counts]
dorfs = 500
pigs = 5
rabbits = 50
+23
View File
@@ -0,0 +1,23 @@
use bevy::prelude::Resource;
use serde::Deserialize;
use std::fs;
#[derive(Debug, Deserialize, Clone, Resource)]
pub struct GameConfig {
pub initial_chunk_radius: i32,
pub spawn_counts: SpawnCounts,
}
#[derive(Debug, Deserialize, Clone)]
pub struct SpawnCounts {
pub dorfs: u32,
pub pigs: u32,
pub rabbits: u32,
}
impl GameConfig {
pub fn load() -> Self {
let config_str = fs::read_to_string("config.toml").expect("Failed to find config.toml");
toml::from_str(&config_str).expect("Failed to parse config.toml")
}
}
+3 -1
View File
@@ -1,3 +1,4 @@
use crate::config::GameConfig;
use crate::constants::TILE_SIZE; use crate::constants::TILE_SIZE;
use crate::constants::*; use crate::constants::*;
use crate::entities::item::{spawn_prefab, MiscPrefab}; use crate::entities::item::{spawn_prefab, MiscPrefab};
@@ -47,9 +48,10 @@ pub fn spawn_pigs(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut rng_q: Query<&mut WyRand, With<GlobalRng>>, mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
config: Res<GameConfig>,
) { ) {
if let Ok(mut rng) = rng_q.single_mut() { if let Ok(mut rng) = rng_q.single_mut() {
for _ in 0..5 { for _ in 0..config.spawn_counts.pigs {
let pig = commands let pig = commands
.spawn(Pig::new( .spawn(Pig::new(
&asset_server, &asset_server,
+3 -1
View File
@@ -1,3 +1,4 @@
use crate::config::GameConfig;
use crate::constants::TILE_SIZE; use crate::constants::TILE_SIZE;
use crate::constants::*; use crate::constants::*;
use crate::entities::shared_components::Ambulatory; use crate::entities::shared_components::Ambulatory;
@@ -40,9 +41,10 @@ pub fn spawn_rabbits(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut rng_q: Query<&mut WyRand, With<GlobalRng>>, mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
config: Res<GameConfig>,
) { ) {
if let Ok(mut rng) = rng_q.single_mut() { if let Ok(mut rng) = rng_q.single_mut() {
for _ in 0..15 { for _ in 0..config.spawn_counts.rabbits {
let rab = commands let rab = commands
.spawn(Rabbit::new( .spawn(Rabbit::new(
&asset_server, &asset_server,
+3 -2
View File
@@ -1,3 +1,4 @@
use crate::config::GameConfig;
use crate::constants::TILE_SIZE; use crate::constants::TILE_SIZE;
use crate::constants::*; use crate::constants::*;
use crate::entities::shared_components::Ambulatory; use crate::entities::shared_components::Ambulatory;
@@ -40,10 +41,10 @@ pub fn spawn_dorfs(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut rng_q: Query<&mut WyRand, With<GlobalRng>>, mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
config: Res<GameConfig>,
) { ) {
if let Ok(mut rng) = rng_q.single_mut() { if let Ok(mut rng) = rng_q.single_mut() {
// Spawn a handful of dorfs for _ in 0..config.spawn_counts.dorfs {
for _ in 0..40 {
let cit = commands let cit = commands
.spawn(Dorf::new( .spawn(Dorf::new(
&asset_server, &asset_server,
+4
View File
@@ -7,6 +7,7 @@ use crate::entities::{
}; };
mod camera; mod camera;
mod config;
mod constants; mod constants;
mod cursor; mod cursor;
mod entities; mod entities;
@@ -14,7 +15,10 @@ mod game;
mod world; mod world;
fn main() { fn main() {
let game_config = config::GameConfig::load();
App::new() App::new()
.insert_resource(game_config)
.insert_resource(game::ZIndex(0.0)) .insert_resource(game::ZIndex(0.0))
.insert_resource(world::tiles::CurrentWorldSpriteState { .insert_resource(world::tiles::CurrentWorldSpriteState {
state: world::tiles::TerrainSpriteState::Inactive, state: world::tiles::TerrainSpriteState::Inactive,
+8 -3
View File
@@ -1,5 +1,6 @@
use crate::{ use crate::{
camera, camera,
config::GameConfig,
world::generation::{ world::generation::{
generate_chunk_fauna, generate_chunk_foliage, generate_chunk_forrestry, generate_chunk_fauna, generate_chunk_foliage, generate_chunk_forrestry,
generate_chunk_terrain, generate_chunk_weathering_and_precipitation, generate_chunk_terrain, generate_chunk_weathering_and_precipitation,
@@ -69,9 +70,13 @@ impl Plugin for WorldPlugin {
} }
} }
fn setup_initial_chunks(mut event_writer: MessageWriter<GenerateChunkEvent>) { fn setup_initial_chunks(
for x in -15..=15 { mut event_writer: MessageWriter<GenerateChunkEvent>,
for y in -15..=15 { config: Res<GameConfig>,
) {
let radius = config.initial_chunk_radius;
for x in -radius..=radius {
for y in -radius..=radius {
event_writer.write(GenerateChunkEvent { event_writer.write(GenerateChunkEvent {
chunk_position: IVec2::new(x, y), chunk_position: IVec2::new(x, y),
}); });