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
+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::*;
use crate::entities::item::{spawn_prefab, MiscPrefab};
@@ -47,9 +48,10 @@ pub fn spawn_pigs(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
config: Res<GameConfig>,
) {
if let Ok(mut rng) = rng_q.single_mut() {
for _ in 0..5 {
for _ in 0..config.spawn_counts.pigs {
let pig = commands
.spawn(Pig::new(
&asset_server,
+3 -1
View File
@@ -1,3 +1,4 @@
use crate::config::GameConfig;
use crate::constants::TILE_SIZE;
use crate::constants::*;
use crate::entities::shared_components::Ambulatory;
@@ -40,9 +41,10 @@ pub fn spawn_rabbits(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
config: Res<GameConfig>,
) {
if let Ok(mut rng) = rng_q.single_mut() {
for _ in 0..15 {
for _ in 0..config.spawn_counts.rabbits {
let rab = commands
.spawn(Rabbit::new(
&asset_server,
+3 -2
View File
@@ -1,3 +1,4 @@
use crate::config::GameConfig;
use crate::constants::TILE_SIZE;
use crate::constants::*;
use crate::entities::shared_components::Ambulatory;
@@ -40,10 +41,10 @@ pub fn spawn_dorfs(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut rng_q: Query<&mut WyRand, With<GlobalRng>>,
config: Res<GameConfig>,
) {
if let Ok(mut rng) = rng_q.single_mut() {
// Spawn a handful of dorfs
for _ in 0..40 {
for _ in 0..config.spawn_counts.dorfs {
let cit = commands
.spawn(Dorf::new(
&asset_server,
+4
View File
@@ -7,6 +7,7 @@ use crate::entities::{
};
mod camera;
mod config;
mod constants;
mod cursor;
mod entities;
@@ -14,7 +15,10 @@ mod game;
mod world;
fn main() {
let game_config = config::GameConfig::load();
App::new()
.insert_resource(game_config)
.insert_resource(game::ZIndex(0.0))
.insert_resource(world::tiles::CurrentWorldSpriteState {
state: world::tiles::TerrainSpriteState::Inactive,
+8 -3
View File
@@ -1,5 +1,6 @@
use crate::{
camera,
config::GameConfig,
world::generation::{
generate_chunk_fauna, generate_chunk_foliage, generate_chunk_forrestry,
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>) {
for x in -15..=15 {
for y in -15..=15 {
fn setup_initial_chunks(
mut event_writer: MessageWriter<GenerateChunkEvent>,
config: Res<GameConfig>,
) {
let radius = config.initial_chunk_radius;
for x in -radius..=radius {
for y in -radius..=radius {
event_writer.write(GenerateChunkEvent {
chunk_position: IVec2::new(x, y),
});