From b7804bd5475070191d6e307cdeafb03ccd3c0721 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Sat, 24 May 2025 13:22:47 -0400 Subject: [PATCH] Add CLI for networking --- src/game/seed.rs | 7 +++++++ src/game/setup.rs | 8 +++++--- src/lib.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/game/seed.rs b/src/game/seed.rs index 0119b8b..af8ee58 100644 --- a/src/game/seed.rs +++ b/src/game/seed.rs @@ -1,11 +1,18 @@ use std::hash::{DefaultHasher, Hash, Hasher}; use bevy::prelude::*; +use rand::random; /// Value with which to initialize the PRNG #[derive(Clone, Resource)] pub struct Seed(u64); +impl Seed { + pub fn random() -> Self { + Self(random()) + } +} + impl From for Seed { /// Attempt to parse as an integer, fall back to hashing string fn from(value: String) -> Self { diff --git a/src/game/setup.rs b/src/game/setup.rs index 2ca871c..55ed28b 100644 --- a/src/game/setup.rs +++ b/src/game/setup.rs @@ -8,12 +8,13 @@ use bevy::{ }, prelude::*, }; -use bevy_rand::prelude::{GlobalEntropy, WyRand}; -use rand::Rng as _; +use rand::{Rng as _, SeedableRng}; +use wyrand::WyRand; use super::{ objects::{Ball, Player, Radius, Wall}, rng::thread_rng, + seed::Seed, }; const BALL_COUNT: u8 = 32; @@ -25,7 +26,8 @@ const DIMENSION_SIZES: Range = 500.0..2000.0; pub struct PlayableArea(f32, f32); /// Initialize deterministic values -pub fn setup_from_seed(mut commands: Commands, mut rng: GlobalEntropy) { +pub fn setup_from_seed(mut commands: Commands, seed: Res) { + let mut rng = WyRand::from_seed(seed.clone().into()); commands.insert_resource(PlayableArea( rng.random_range(DIMENSION_SIZES), rng.random_range(DIMENSION_SIZES), diff --git a/src/lib.rs b/src/lib.rs index 90a14b2..18a8234 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ +use std::net::SocketAddr; + use avian2d::{math::Vector, prelude::*}; use bevy::{input::common_conditions::input_pressed, prelude::*}; -use bevy_rand::prelude::{EntropyPlugin, WyRand}; -use clap::Parser; +use clap::{Args, Parser}; mod game; use game::{ @@ -15,14 +16,25 @@ use game::{ #[derive(Parser)] #[command(version, about)] pub struct AppSettings { - #[arg(short, long, default_value = ":)")] - pub seed: Seed, + #[command(flatten)] + source: Source, + + #[arg(short, long, default_value = "25565")] + port: u16, +} + +#[derive(Args)] +#[group(required = false, multiple = false)] +struct Source { + #[arg(short, long)] + seed: Option, + #[arg(short, long)] + connect: Option, } impl Plugin for AppSettings { fn build(&self, app: &mut App) { app.insert_resource(Gravity(Vector::ZERO)) - .insert_resource(self.seed.clone()) .add_plugins(( DefaultPlugins.set(WindowPlugin { primary_window: Window { @@ -34,7 +46,6 @@ impl Plugin for AppSettings { ..default() }), PhysicsPlugins::default().with_length_unit(50.0), - EntropyPlugin::::with_seed(self.seed.clone().into()), )) .add_systems( Startup, @@ -53,5 +64,13 @@ impl Plugin for AppSettings { ), ) .add_systems(PostUpdate, move_camera); + if let Some(ref seed) = self.source.seed { + app.insert_resource(seed.clone()); + } else if let Some(ref peer) = self.source.connect { + println!("{peer}"); + todo!("Handle connecting to peer and retrieving seed"); + } else { + app.insert_resource(Seed::random()); + } } }