From a644a4fcf84684b16ad28cb8da3e4769e455ebfb Mon Sep 17 00:00:00 2001 From: PraxTube Date: Fri, 27 Oct 2023 17:28:03 +0200 Subject: [PATCH] restructure!: Move command logic into `console` --- src/console/mod.rs | 74 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 ++ src/network/session.rs | 6 ++-- src/network/socket.rs | 4 +-- src/player/mod.rs | 21 ++++++++++-- src/player/movement.rs | 6 ++-- 6 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 src/console/mod.rs diff --git a/src/console/mod.rs b/src/console/mod.rs new file mode 100644 index 0000000..e442b0d --- /dev/null +++ b/src/console/mod.rs @@ -0,0 +1,74 @@ +use bevy::prelude::*; +use bevy_ggrs::ggrs::PlayerType; + +use crate::{network::socket::AceSocket, player::Player, GameState, RollbackState}; + +#[derive(Resource, Default)] +pub struct CommandQueue { + early_queue: Vec, + queue: Vec, +} + +pub fn push_command( + mut socket: ResMut, + mut command_queque: ResMut, + keys: Res>, +) { + if !keys.just_pressed(KeyCode::U) { + return; + } + + for p in socket.players() { + if let PlayerType::Remote(peer_id) = p { + info!("sending cmd message, adding to own queue"); + socket.send_tcp_message(peer_id, "lower max_speed"); + command_queque.queue.push("lower max_speed".to_string()); + }; + } +} + +pub fn receive_command(mut socket: ResMut, mut command_queque: ResMut) { + let received_messages = socket.receive_tcp_message(); + + if received_messages.is_empty() { + return; + } + + for message in received_messages { + if message.1 == "lower max_speed".to_string() { + info!("received command messsage, adding to queue"); + command_queque.queue.push("lower max_speed".to_string()); + } + } +} + +pub fn apply_commands(mut command_queque: ResMut, mut players: Query<&mut Player>) { + for command in command_queque.early_queue.drain(..) { + if &command == "lower max_speed" { + for mut player in &mut players { + if player.handle == 0 { + info!("setting max speed..."); + player.stats.max_speed = 0.0; + } + } + } + } + + command_queque.early_queue = command_queque.queue.clone(); + command_queque.queue = Vec::default(); +} + +pub struct AceConsolePlugin; + +impl Plugin for AceConsolePlugin { + fn build(&self, app: &mut App) { + app.add_systems( + Update, + (push_command, receive_command) + .chain() + .run_if(in_state(GameState::InRollbackGame)), + ) + .init_resource::() + .add_systems(OnEnter(RollbackState::InRound), apply_commands); + } +} diff --git a/src/main.rs b/src/main.rs index a087ed0..e150eb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use bevy_roll_safe::prelude::*; mod assets; mod audio; +mod console; mod game_logic; mod input; mod map; @@ -97,6 +98,7 @@ fn main() { game_logic::GameLogicPlugin, network::AceNetworkPlugin, ui::AceUiPlugin, + console::AceConsolePlugin, map::MapPlugin, player::PlayerPlugin, debug::AceDebugPlugin, diff --git a/src/network/session.rs b/src/network/session.rs index c1c5d98..c21fe51 100644 --- a/src/network/session.rs +++ b/src/network/session.rs @@ -72,7 +72,7 @@ pub fn wait_for_players( match player { PlayerType::Remote(peer_id) => { - socket.send_tcp_seed(peer_id, &seed.0[0].seed.to_string()); + socket.send_tcp_message(peer_id, &seed.0[0].seed.to_string()); } PlayerType::Local => { commands.insert_resource(LocalPlayerHandle(i)); @@ -105,7 +105,7 @@ pub fn wait_for_seed( return; } - let received_seeds = socket.receive_tcp_seed(); + let received_seeds = socket.receive_tcp_message(); if received_seeds.is_empty() { return; @@ -130,7 +130,7 @@ pub fn wait_for_seed( // The 0 seed should never be possible as a normal seed. for player in socket.players() { if let PlayerType::Remote(peer_id) = player { - socket.send_tcp_seed(peer_id, "ready"); + socket.send_tcp_message(peer_id, "ready"); }; } info!("we are ready, received peer seed and sent ready message"); diff --git a/src/network/socket.rs b/src/network/socket.rs index 75f913f..b32858a 100644 --- a/src/network/socket.rs +++ b/src/network/socket.rs @@ -46,14 +46,14 @@ impl AceSocket { pub const GGRS_CHANNEL: usize = 0; pub const RELIABLE_CHANNEL: usize = 1; - pub fn send_tcp_seed(&mut self, peer: PeerId, message: &str) { + pub fn send_tcp_message(&mut self, peer: PeerId, message: &str) { let bytes = serialize(message).expect("failed to serialize string"); self.inner_mut() .channel(Self::RELIABLE_CHANNEL) .send(bytes.clone().into(), peer); } - pub fn receive_tcp_seed(&mut self) -> Vec<(PeerId, String)> { + pub fn receive_tcp_message(&mut self) -> Vec<(PeerId, String)> { self.inner_mut() .channel(Self::RELIABLE_CHANNEL) .receive() diff --git a/src/player/mod.rs b/src/player/mod.rs index 0a15ebc..68190ee 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -16,7 +16,6 @@ use crate::network::GgrsConfig; use crate::RollbackState; // Movement -pub const MAX_SPEED: f32 = 400.0 / 60.0; pub const MIN_SPEED: f32 = 000.0 / 60.0; pub const DELTA_SPEED: f32 = 75.0 / 60.0 / 100.0; pub const DELTA_STEERING: f32 = 3.5 / 60.0; @@ -37,6 +36,19 @@ pub const P2_COLOR: Color = Color::rgb( 0xD9 as f32 / 255.0, ); +#[derive(Reflect)] +pub struct PlayerStats { + pub max_speed: f32, +} + +impl Default for PlayerStats { + fn default() -> Self { + Self { + max_speed: 400.0 / 60.0, + } + } +} + #[derive(Component, Reflect, Default)] #[reflect(Hash)] pub struct Player { @@ -47,6 +59,8 @@ pub struct Player { pub heat: u32, pub overheated: bool, pub dodging: bool, + + pub stats: PlayerStats, } impl Player { @@ -58,12 +72,13 @@ impl Player { heat: 0, overheated: false, dodging: false, + stats: PlayerStats::default(), } } pub fn speed_ratio(&self) -> u32 { - ((self.current_speed - MIN_SPEED).max(0.0) / (MAX_SPEED - MIN_SPEED).max(0.0) * 100.0) - as u32 + ((self.current_speed - MIN_SPEED).max(0.0) / (self.stats.max_speed - MIN_SPEED).max(0.0) + * 100.0) as u32 } } diff --git a/src/player/movement.rs b/src/player/movement.rs index c6dc27a..d7913e4 100644 --- a/src/player/movement.rs +++ b/src/player/movement.rs @@ -4,7 +4,7 @@ use bevy_ggrs::*; use crate::debug::DebugTransform; use crate::input; use crate::network::GgrsConfig; -use crate::player::{Player, DELTA_SPEED, DELTA_STEERING, MAX_SPEED, MIN_SPEED}; +use crate::player::{Player, DELTA_SPEED, DELTA_STEERING, MIN_SPEED}; pub fn steer_players( inputs: Res>, @@ -42,7 +42,9 @@ pub fn accelerate_players(inputs: Res>, mut players: Qu }; player.current_speed += acceleration; - player.current_speed = player.current_speed.clamp(MIN_SPEED, MAX_SPEED); + player.current_speed = player + .current_speed + .clamp(MIN_SPEED, player.stats.max_speed); } }