Compare commits

...

2 Commits

Author SHA1 Message Date
f8b5107c92 shells 2026-02-09 15:35:37 -06:00
dbb87c3930 networking shell 2026-02-02 15:31:39 -06:00
9 changed files with 291 additions and 13 deletions

127
Cargo.lock generated
View File

@ -173,6 +173,56 @@ dependencies = [
"libc", "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]] [[package]]
name = "approx" name = "approx"
version = "0.5.1" version = "0.5.1"
@ -1850,6 +1900,46 @@ dependencies = [
"libloading", "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]] [[package]]
name = "codespan-reporting" name = "codespan-reporting"
version = "0.12.0" version = "0.12.0"
@ -1861,6 +1951,12 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]] [[package]]
name = "combine" name = "combine"
version = "4.6.7" version = "4.6.7"
@ -2919,6 +3015,12 @@ dependencies = [
"stable_deref_trait", "stable_deref_trait",
] ]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.5.2" version = "0.5.2"
@ -3002,6 +3104,12 @@ dependencies = [
"rustversion", "rustversion",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.13.0" version = "0.13.0"
@ -3843,6 +3951,12 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]] [[package]]
name = "orbclient" name = "orbclient"
version = "0.3.50" version = "0.3.50"
@ -4719,6 +4833,12 @@ 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 = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]] [[package]]
name = "svg_fmt" name = "svg_fmt"
version = "0.4.5" version = "0.4.5"
@ -4846,6 +4966,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"avian2d", "avian2d",
"bevy", "bevy",
"clap",
] ]
[[package]] [[package]]
@ -5077,6 +5198,12 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.19.0" version = "1.19.0"

View File

@ -3,6 +3,13 @@ name = "time_travel"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
[features]
default = ["client", "server"]
client = []
server = []
[dependencies] [dependencies]
avian2d = "0.5.0" avian2d = "0.5.0"
bevy = "0.18.0" bevy = "0.18.0"
clap = { version = "4.5.56", features = ["derive"] }

27
src/cli.rs Normal file
View File

@ -0,0 +1,27 @@
use clap::{Parser, Subcommand};
#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct Cli {
#[command(subcommand)]
pub mode: Option<Mode>,
}
#[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,
}

View File

@ -1,26 +1,75 @@
use crate::avian::{CharacterControllerBundle, CharacterControllerPlugin}; use crate::{
avian::{CharacterControllerBundle, CharacterControllerPlugin},
cli::Cli,
};
use avian2d::{ use avian2d::{
PhysicsPlugins, PhysicsPlugins,
math::Vector, math::Vector,
prelude::{Collider, Gravity, RigidBody}, prelude::{Collider, Gravity, RigidBody},
}; };
use bevy::{camera::ScalingMode, color::palettes::css::GREEN, prelude::*}; use bevy::{camera::ScalingMode, color::palettes::css::GREEN, prelude::*};
use clap::Parser;
pub mod avian; pub mod avian;
pub mod cli;
pub mod net;
fn main() { fn main() {
App::new() let cli = match Cli::try_parse() {
.add_plugins(( Ok(cli) => cli,
DefaultPlugins, Err(err) => {
// Add physics plugins and specify a units-per-meter scaling factor, 1 meter = 20 pixels. err.print().expect("failed to print clap error");
// The unit allows the engine to tune its parameters for the scale of the world, improving stability. std::process::exit(1);
PhysicsPlugins::default().with_length_unit(20.0), }
CharacterControllerPlugin, };
))
.add_systems(Startup, setup) #[cfg(all(feature = "client", not(feature = "server")))]
.add_systems(Update, debug_border) let mode = cli.mode.unwrap_or(cli::Mode::Client);
.insert_resource(Gravity(Vector::ZERO)) #[cfg(all(feature = "server", not(feature = "client")))]
.run(); 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<AssetServer>) { fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

18
src/net/client.rs Normal file
View File

@ -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);
}
}

6
src/net/mod.rs Normal file
View File

@ -0,0 +1,6 @@
#[cfg(feature = "client")]
pub mod client;
pub mod protocol;
#[cfg(feature = "server")]
pub mod server;
pub mod shared;

30
src/net/protocol.rs Normal file
View File

@ -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::<Message1>()
.add_direction(NetworkDirection::ServerToClient);
// inputs
app.add_plugins(input::native::InputPlugin::<Inputs>::default());
// components
app.register_component::<PlayerId>();
app.register_component::<PlayerPosition>()
.add_prediction()
.add_linear_interpolation();
app.register_component::<PlayerColor>();
// channels
app.add_channel::<Channel1>(ChannelSettings {
mode: ChannelMode::OrderedReliable(ReliableSettings::default()),
..default()
})
.add_direction(NetworkDirection::ServerToClient);
}
}

13
src/net/server.rs Normal file
View File

@ -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);
}
}

1
src/net/shared.rs Normal file
View File

@ -0,0 +1 @@