Extract Seed and AppSettings to utils
This commit is contained in:
parent
9be7b102d7
commit
b7780b8862
5 changed files with 43 additions and 35 deletions
40
src/lib.rs
40
src/lib.rs
|
@ -1,8 +1,4 @@
|
|||
use std::{
|
||||
f32::consts::PI,
|
||||
hash::{DefaultHasher, Hash, Hasher},
|
||||
ops::Range,
|
||||
};
|
||||
use std::{f32::consts::PI, ops::Range};
|
||||
|
||||
use avian2d::{math::Vector, prelude::*};
|
||||
use bevy::{
|
||||
|
@ -14,40 +10,17 @@ use bevy::{
|
|||
prelude::*,
|
||||
};
|
||||
use bevy_rand::prelude::{EntropyPlugin, GlobalEntropy, WyRand};
|
||||
use clap::Parser;
|
||||
use rand::Rng;
|
||||
use wyrand::WyRand as LocalRng;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about)]
|
||||
pub struct AppSettings {
|
||||
#[arg(short, long, default_value = "")]
|
||||
seed: String,
|
||||
}
|
||||
pub mod utils;
|
||||
|
||||
use utils::AppSettings;
|
||||
|
||||
const BALL_COUNT: u8 = 32;
|
||||
const BALL_SIZES: Range<f32> = 10.0..25.0;
|
||||
const DIMENSION_SIZES: Range<f32> = 500.0..2000.0;
|
||||
|
||||
#[derive(Clone, Resource)]
|
||||
struct Seed(u64);
|
||||
|
||||
impl From<String> for Seed {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value.parse::<u64>().unwrap_or_else(|_| {
|
||||
let mut state = DefaultHasher::new();
|
||||
value.hash(&mut state);
|
||||
state.finish()
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Seed> for [u8; 8] {
|
||||
fn from(value: Seed) -> Self {
|
||||
value.0.to_le_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct PlayableArea(f32, f32);
|
||||
|
||||
|
@ -55,10 +28,9 @@ struct PlayableArea(f32, f32);
|
|||
struct PlayerSize(f32);
|
||||
|
||||
pub fn run_app(settings: AppSettings) -> AppExit {
|
||||
let seed = Seed::from(settings.seed);
|
||||
App::new()
|
||||
.insert_resource(Gravity(Vector::ZERO))
|
||||
.insert_resource(seed.clone())
|
||||
.insert_resource(settings.seed.clone())
|
||||
.add_plugins((
|
||||
DefaultPlugins.set(WindowPlugin {
|
||||
primary_window: Window {
|
||||
|
@ -70,7 +42,7 @@ pub fn run_app(settings: AppSettings) -> AppExit {
|
|||
..default()
|
||||
}),
|
||||
PhysicsPlugins::default().with_length_unit(50.0),
|
||||
EntropyPlugin::<WyRand>::with_seed(seed.into()),
|
||||
EntropyPlugin::<WyRand>::with_seed(settings.seed.into()),
|
||||
))
|
||||
.add_systems(
|
||||
Startup,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use bevy::prelude::AppExit;
|
||||
use clap::Parser;
|
||||
|
||||
use distributed_physics_test::{AppSettings, run_app};
|
||||
use distributed_physics_test::{run_app, utils::AppSettings};
|
||||
|
||||
fn main() -> AppExit {
|
||||
run_app(AppSettings::parse())
|
||||
|
|
10
src/utils/app_settings.rs
Normal file
10
src/utils/app_settings.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use clap::Parser;
|
||||
|
||||
use super::Seed;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about)]
|
||||
pub struct AppSettings {
|
||||
#[arg(short, long, default_value = "")]
|
||||
pub seed: Seed,
|
||||
}
|
4
src/utils/mod.rs
Normal file
4
src/utils/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
mod seed;
|
||||
pub use seed::Seed;
|
||||
mod app_settings;
|
||||
pub use app_settings::AppSettings;
|
22
src/utils/seed.rs
Normal file
22
src/utils/seed.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Clone, Resource)]
|
||||
pub struct Seed(u64);
|
||||
|
||||
impl From<String> for Seed {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value.parse::<u64>().unwrap_or_else(|_| {
|
||||
let mut state = DefaultHasher::new();
|
||||
value.hash(&mut state);
|
||||
state.finish()
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Seed> for [u8; 8] {
|
||||
fn from(value: Seed) -> Self {
|
||||
value.0.to_le_bytes()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue