From f8b5107c92abede0f0d5d4e4ce8e345100982128 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Mon, 9 Feb 2026 15:35:37 -0600 Subject: [PATCH] shells --- Cargo.lock | 127 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/cli.rs | 27 ++++++++++ src/main.rs | 74 +++++++++++++++++++++----- src/net/client.rs | 18 +++++++ src/net/protocol.rs | 30 +++++++++++ src/net/server.rs | 13 +++++ 7 files changed, 278 insertions(+), 13 deletions(-) create mode 100644 src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index 1f3fcb4..0012485 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "approx" version = "0.5.1" @@ -1850,6 +1900,46 @@ dependencies = [ "libloading", ] +[[package]] +name = "clap" +version = "4.5.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75ca66430e33a14957acc24c5077b503e7d374151b2b4b3a10c83b4ceb4be0e" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793207c7fa6300a0608d1080b858e5fdbe713cdc1c8db9fb17777d8a13e63df0" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + [[package]] name = "codespan-reporting" version = "0.12.0" @@ -1861,6 +1951,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "combine" version = "4.6.7" @@ -2919,6 +3015,12 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.5.2" @@ -3002,6 +3104,12 @@ dependencies = [ "rustversion", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itertools" version = "0.13.0" @@ -3843,6 +3951,12 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "orbclient" version = "0.3.50" @@ -4719,6 +4833,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "svg_fmt" version = "0.4.5" @@ -4846,6 +4966,7 @@ version = "0.1.0" dependencies = [ "avian2d", "bevy", + "clap", ] [[package]] @@ -5077,6 +5198,12 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.19.0" diff --git a/Cargo.toml b/Cargo.toml index 539cc76..0d1cade 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,10 +4,12 @@ version = "0.1.0" edition = "2024" [features] +default = ["client", "server"] client = [] server = [] [dependencies] avian2d = "0.5.0" bevy = "0.18.0" +clap = { version = "4.5.56", features = ["derive"] } diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..c0abf4b --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,27 @@ +use clap::{Parser, Subcommand}; + +#[derive(Parser, Debug)] +#[command(author, version, about)] +pub struct Cli { + #[command(subcommand)] + pub mode: Option, +} + +#[derive(Subcommand, Debug, Default)] +pub enum Mode { + /// Run a headless server (no rendering) + #[cfg(feature = "server")] + #[cfg_attr(all(feature = "server", not(feature = "client")), default)] + Headless, + /// Run a server that renders + #[cfg(feature = "server")] + Server, + /// Run the client application + #[cfg(feature = "client")] + #[cfg_attr(all(feature = "client", not(feature = "server")), default)] + Client, + /// Run both client and server in one process + #[cfg(all(feature = "client", feature = "server"))] + #[cfg_attr(all(feature = "client", feature = "server"), default)] + Host, +} diff --git a/src/main.rs b/src/main.rs index 8b56cbd..097e713 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,75 @@ -use crate::avian::{CharacterControllerBundle, CharacterControllerPlugin}; +use crate::{ + avian::{CharacterControllerBundle, CharacterControllerPlugin}, + cli::Cli, +}; use avian2d::{ PhysicsPlugins, math::Vector, prelude::{Collider, Gravity, RigidBody}, }; use bevy::{camera::ScalingMode, color::palettes::css::GREEN, prelude::*}; +use clap::Parser; pub mod avian; +pub mod cli; pub mod net; fn main() { - App::new() - .add_plugins(( - DefaultPlugins, - // Add physics plugins and specify a units-per-meter scaling factor, 1 meter = 20 pixels. - // The unit allows the engine to tune its parameters for the scale of the world, improving stability. - PhysicsPlugins::default().with_length_unit(20.0), - CharacterControllerPlugin, - )) - .add_systems(Startup, setup) - .add_systems(Update, debug_border) - .insert_resource(Gravity(Vector::ZERO)) - .run(); + let cli = match Cli::try_parse() { + Ok(cli) => cli, + Err(err) => { + err.print().expect("failed to print clap error"); + std::process::exit(1); + } + }; + + #[cfg(all(feature = "client", not(feature = "server")))] + let mode = cli.mode.unwrap_or(cli::Mode::Client); + #[cfg(all(feature = "server", not(feature = "client")))] + let mode = cli.mode.unwrap_or(cli::Mode::Server); + #[cfg(all(feature = "client", feature = "server"))] + let mode = cli.mode.unwrap_or(cli::Mode::Host); + #[cfg(all(not(feature = "client"), not(feature = "server")))] + panic!("You must enable either the client or server feature"); + + let mut app = App::new(); + + match mode { + #[cfg(feature = "server")] + cli::Mode::Headless => { + app.add_plugins((MinimalPlugins, net::server::GameServerPlugin)); + } + + #[cfg(feature = "server")] + cli::Mode::Server => { + app.add_plugins((DefaultPlugins, net::server::GameServerPlugin)); + } + + #[cfg(feature = "client")] + cli::Mode::Client => { + app.add_plugins((DefaultPlugins, net::client::GameClientPlugin)); + } + + #[cfg(all(feature = "client", feature = "server"))] + cli::Mode::Host => { + app.add_plugins(( + DefaultPlugins, + net::server::GameServerPlugin, + net::client::GameClientPlugin, + )); + } + } + + app.add_plugins(( + // Add physics plugins and specify a units-per-meter scaling factor, 1 meter = 20 pixels. + // The unit allows the engine to tune its parameters for the scale of the world, improving stability. + PhysicsPlugins::default().with_length_unit(20.0), + CharacterControllerPlugin, + )) + .add_systems(Startup, setup) + .add_systems(Update, debug_border) + .insert_resource(Gravity(Vector::ZERO)) + .run(); } fn setup(mut commands: Commands, asset_server: Res) { diff --git a/src/net/client.rs b/src/net/client.rs index e69de29..6754417 100644 --- a/src/net/client.rs +++ b/src/net/client.rs @@ -0,0 +1,18 @@ +use bevy::prelude::*; + +pub struct GameClientPlugin; + +impl Plugin for GameClientPlugin { + fn build(&self, app: &mut App) { + // app.add_systems( + // FixedPreUpdate, + // // Inputs have to be buffered in the WriteClientInputs set + // buffer_input.in_set(InputSystems::WriteClientInputs), + // ); + // app.add_systems(FixedUpdate, player_movement); + + // app.add_systems(Update, receive_message1); + // app.add_observer(handle_predicted_spawn); + // app.add_observer(handle_interpolated_spawn); + } +} diff --git a/src/net/protocol.rs b/src/net/protocol.rs index e69de29..8e3fa92 100644 --- a/src/net/protocol.rs +++ b/src/net/protocol.rs @@ -0,0 +1,30 @@ +use bevy::prelude::*; + +#[derive(Clone)] +pub struct ProtocolPlugin; + +impl Plugin for ProtocolPlugin { + fn build(&self, app: &mut App) { + // messages + app.register_message::() + .add_direction(NetworkDirection::ServerToClient); + + // inputs + app.add_plugins(input::native::InputPlugin::::default()); + // components + app.register_component::(); + + app.register_component::() + .add_prediction() + .add_linear_interpolation(); + + app.register_component::(); + + // channels + app.add_channel::(ChannelSettings { + mode: ChannelMode::OrderedReliable(ReliableSettings::default()), + ..default() + }) + .add_direction(NetworkDirection::ServerToClient); + } +} diff --git a/src/net/server.rs b/src/net/server.rs index e69de29..42adf55 100644 --- a/src/net/server.rs +++ b/src/net/server.rs @@ -0,0 +1,13 @@ +use bevy::prelude::*; + +pub struct GameServerPlugin; + +impl Plugin for GameServerPlugin { + fn build(&self, app: &mut App) { + // // the physics/FixedUpdates systems that consume inputs should be run in this set. + // app.add_systems(FixedUpdate, movement); + // app.add_observer(handle_new_client); + // app.add_observer(handle_connected); + // app.add_systems(Update, send_message); + } +}