diff --git a/.vscode/launch.json b/.vscode/launch.json index 076d413..f4dd30c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "cargo": { "args": ["build"] }, - "args": ["--seed=:)"], + "args": ["--seed=gargamel"], "cwd": "${workspaceFolder}", "env": { "CARGO_MANIFEST_DIR": "${workspaceFolder}", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index b346e26..779bf54 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -35,7 +35,7 @@ "problemMatcher": ["$rustc"], "group": { "kind": "build", - "isDefault": false + "isDefault": true } } ] diff --git a/Cargo.lock b/Cargo.lock index 3dc023e..1af9a77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1921,7 +1921,6 @@ dependencies = [ "bevy", "bevy_rand", "clap", - "crossbeam-channel", "log", "rand 0.9.1", "wyrand", diff --git a/Cargo.toml b/Cargo.toml index a0f83e5..ba7dbfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ bevy = { version = "0.16.0", default-features = false, features = [ ] } bevy_rand = { version = "0.11.0", features = ["wyrand", "std"] } clap = { version = "4.5.32", features = ["derive"] } -crossbeam-channel = "0.5.15" log = { version = "*", features = [ "max_level_debug", "release_max_level_warn", diff --git a/src/game/objects.rs b/src/game/objects.rs index 1ede2d5..da4f096 100644 --- a/src/game/objects.rs +++ b/src/game/objects.rs @@ -1,11 +1,9 @@ use avian2d::prelude::*; use bevy::prelude::*; -use crate::AppState; - /// Basic implementation of a physics object #[derive(Component, Default)] -#[require(Collider, Mesh2d, MeshMaterial2d, Restitution = Restitution::new(1.0), RigidBody, TransformInterpolation, Transform, StateScoped = StateScoped(AppState::InGame))] +#[require(Collider, Mesh2d, MeshMaterial2d, Restitution = Restitution::new(1.0), RigidBody, TransformInterpolation, Transform)] struct GameObject; /// Radius of a ball diff --git a/src/game/seed.rs b/src/game/seed.rs index 5fabafa..af8ee58 100644 --- a/src/game/seed.rs +++ b/src/game/seed.rs @@ -8,7 +8,6 @@ use rand::random; pub struct Seed(u64); impl Seed { - /// Use a random integer as the seed pub fn random() -> Self { Self(random()) } @@ -31,15 +30,3 @@ impl From for [u8; 8] { value.0.to_le_bytes() } } - -impl From for Seed { - fn from(value: u64) -> Self { - Seed(value) - } -} - -impl From for u64 { - fn from(value: Seed) -> Self { - value.0 - } -} diff --git a/src/lib.rs b/src/lib.rs index cb4d69c..113b8b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,8 +14,6 @@ use game::{ setup::{check_for_seed, setup_balls, setup_from_seed, setup_player, setup_ui, setup_walls}, }; -mod net; - /// The initial configuration passed to the game's setup functions. /// Also functions as a Bevy plugin to pass the configuration into the app. #[derive(Parser)] @@ -42,7 +40,6 @@ struct Source { } #[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)] -#[states(scoped_entities)] enum AppState { #[default] Loading, @@ -65,7 +62,6 @@ impl Plugin for AppSettings { PhysicsPlugins::default().with_length_unit(50.0), #[cfg(feature = "dev")] dev::dev_tools, - net::NetIOPlugin::new(self.port, self.source.connect), )) .init_state::() .add_systems(Startup, setup_ui) @@ -76,14 +72,11 @@ impl Plugin for AppSettings { (setup_player, setup_balls, setup_walls).after(setup_from_seed), ), ) - .add_systems( - FixedUpdate, - check_for_seed.run_if(in_state(AppState::Loading)), - ) .add_systems( Update, ( - ((move_player, move_camera).chain(), zoom_camera) + check_for_seed.run_if(in_state(AppState::Loading)), + (move_player, move_camera.after(move_player), zoom_camera) .run_if(in_state(AppState::InGame)), quit.run_if(input_pressed(KeyCode::KeyQ)), ), @@ -92,7 +85,8 @@ impl Plugin for AppSettings { if let Some(ref seed) = self.source.seed { app.insert_resource(seed.clone()); } else if let Some(ref peer) = self.source.connect { - info!("Will retrieve seed from peer => {peer}"); + info!("Got peer: {peer}"); + todo!("Handle connecting to peer and retrieving seed"); } else { app.insert_resource(Seed::random()); } diff --git a/src/net.rs b/src/net.rs deleted file mode 100644 index 1055e92..0000000 --- a/src/net.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::{ - net::{Ipv6Addr, SocketAddr, UdpSocket}, - thread, -}; - -use bevy::prelude::*; -use crossbeam_channel::{Receiver, Sender, unbounded}; - -use crate::game::seed::Seed; - -#[derive(Resource)] -pub struct NetworkSend(Sender<(u64, SocketAddr)>); - -#[derive(Resource)] -pub struct NetworkReceive(Receiver<(u64, SocketAddr)>); - -fn handle_network_io( - receive: Res, - send: Res, - seed: Option>, - mut commands: Commands, -) -> Result { - let Ok((message, address)) = receive.0.try_recv() else { - return Ok(()); - }; - if let Some(value) = seed { - send.0.try_send((value.clone().into(), address))?; - } else { - commands.insert_resource::(message.into()); - } - Ok(()) -} - -pub struct NetIOPlugin { - listen: u16, - peer: Option, -} - -impl NetIOPlugin { - pub fn new(listen: u16, peer: Option) -> Self { - Self { listen, peer } - } -} - -impl Plugin for NetIOPlugin { - fn build(&self, app: &mut App) { - app.add_systems(FixedUpdate, handle_network_io); - - let (send, receive) = match UdpSocket::bind((Ipv6Addr::LOCALHOST, self.listen)) { - Ok(socket) => { - socket.set_read_timeout(None).unwrap(); - socket.set_write_timeout(None).unwrap(); - let (send_outbound, receive_outbound) = unbounded::<(u64, SocketAddr)>(); - let send_socket = socket.try_clone().unwrap(); - thread::spawn(move || { - loop { - match receive_outbound.recv() { - Ok((message, address)) => send_socket - .send_to(&message.to_le_bytes(), address) - .unwrap(), - Err(err) => { - error!("{err}"); - break; - } - }; - } - }); - let (send_inbound, receive_inbound) = unbounded::<(u64, SocketAddr)>(); - thread::spawn(move || { - loop { - let mut message = [0u8; 8]; - let (len, address) = socket.recv_from(&mut message).unwrap(); - info!("Received {len} bytes"); - send_inbound - .try_send((u64::from_le_bytes(message), address)) - .unwrap(); - } - }); - (send_outbound, receive_inbound) - } - Err(err) => { - error!("Could not bind socket: {err}"); - todo!("bounded(0) is apparently meaningful so find another solution") - } - }; - - if let Some(socket) = self.peer { - send.try_send((0, socket)).unwrap(); - } - - app.insert_resource(NetworkSend(send)); - app.insert_resource(NetworkReceive(receive)); - } -}