This commit is contained in:
Mitchell Marino 2024-10-27 19:01:19 -05:00
commit a1ef0a0172
6 changed files with 5000 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

4870
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "crooks"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.14.2"
lightyear = "0.17.1"
serde = { version = "1.0.213", features = ["derive"] }

6
src/main.rs Normal file
View File

@ -0,0 +1,6 @@
pub mod protocol;
pub mod shared_net;
fn main() {
println!("Hello, world!");
}

68
src/protocol.rs Normal file
View File

@ -0,0 +1,68 @@
use bevy::prelude::*;
use client::ComponentSyncMode;
use lightyear::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ChatMessage(pub String);
/// Identifies which player something belongs to.
#[derive(Component, Serialize, Deserialize, Clone, Debug, PartialEq, Reflect)]
pub struct PlayerId(ClientId);
/// A component that will store the transform data of the player.
#[derive(Component, Serialize, Deserialize, Clone, Debug, PartialEq, Reflect)]
pub struct PlayerTransformData {
pos: Vec3,
rot: Quat,
}
impl Linear for PlayerTransformData {
fn lerp(start: &Self, other: &Self, t: f32) -> Self {
Self {
pos: start.pos.lerp(other.pos, t),
rot: start.rot.lerp(other.rot, t),
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Direction {
pub(crate) up: bool,
pub(crate) down: bool,
pub(crate) left: bool,
pub(crate) right: bool,
}
/// The `InputProtocol` needs to be an enum of the various inputs that the client can send to the server.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Inputs {
Direction(Direction),
None,
}
/// A channel for sending chat messages.
#[derive(Channel)]
pub struct ChatChannel;
pub struct ProtocolPlugin;
impl Plugin for ProtocolPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.register_message::<ChatMessage>(ChannelDirection::Bidirectional);
app.add_plugins(InputPlugin::<Inputs>::default());
app.register_component::<PlayerId>(ChannelDirection::ServerToClient)
.add_prediction(ComponentSyncMode::Once)
.add_interpolation(ComponentSyncMode::Once);
app.register_component::<PlayerTransformData>(ChannelDirection::ServerToClient)
.add_prediction(ComponentSyncMode::Full)
.add_interpolation(ComponentSyncMode::Full)
.add_linear_interpolation_fn();
app.add_channel::<ChatChannel>(ChannelSettings {
mode: ChannelMode::OrderedReliable(ReliableSettings::default()),
..default()
})
}
}

46
src/shared_net.rs Normal file
View File

@ -0,0 +1,46 @@
use client::{Authentication, ClientConfig};
use lightyear::prelude::*;
use std::time::Duration;
pub const PRIVIATE_KEY: Key = [
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12,
];
pub const FIXED_TIMESTEP_HZ: f64 = 64.0;
pub const REPLICATION_INTERVAL: Duration = Duration::from_millis(100);
pub fn client_config() -> ClientConfig {
let auth = Authentication::Manual {
server_addr: "127.0.0.1:7000".parse().unwrap(),
client_id: 2,
private_key: PRIVIATE_KEY,
protocol_id: 0,
};
let netcode_config = client::NetcodeConfig::default();
let io_config = client::IoConfig {
transport: client::ClientTransport::UdpSocket("127.0.0.1:7000".parse().unwrap()),
conditioner: None,
compression: CompressionConfig::default(),
};
let net = client::NetConfig::Netcode {
auth,
config: netcode_config,
io: io_config,
};
ClientConfig {
shared: shared_config(Mode::Separate),
net,
..Default::default()
}
}
/// The [`SharedConfig`] must be shared between the `ClientConfig` and `ServerConfig`
pub fn shared_config(mode: Mode) -> SharedConfig {
SharedConfig {
server_replication_send_interval: REPLICATION_INTERVAL,
tick: TickConfig {
tick_duration: Duration::from_secs_f64(1.0 / FIXED_TIMESTEP_HZ),
},
mode,
}
}