Skip to content

Commit

Permalink
restructure!: Move command logic into console
Browse files Browse the repository at this point in the history
  • Loading branch information
PraxTube committed Oct 27, 2023
1 parent fad4d0c commit a644a4f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
74 changes: 74 additions & 0 deletions src/console/mod.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
queue: Vec<String>,
}

pub fn push_command(
mut socket: ResMut<AceSocket>,
mut command_queque: ResMut<CommandQueue>,
keys: Res<Input<KeyCode>>,
) {
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<AceSocket>, mut command_queque: ResMut<CommandQueue>) {
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<CommandQueue>, 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::<CommandQueue>()
.add_systems(OnEnter(RollbackState::InRound), apply_commands);
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy_roll_safe::prelude::*;

mod assets;
mod audio;
mod console;
mod game_logic;
mod input;
mod map;
Expand Down Expand Up @@ -97,6 +98,7 @@ fn main() {
game_logic::GameLogicPlugin,
network::AceNetworkPlugin,
ui::AceUiPlugin,
console::AceConsolePlugin,
map::MapPlugin,
player::PlayerPlugin,
debug::AceDebugPlugin,
Expand Down
6 changes: 3 additions & 3 deletions src/network/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 18 additions & 3 deletions src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -47,6 +59,8 @@ pub struct Player {
pub heat: u32,
pub overheated: bool,
pub dodging: bool,

pub stats: PlayerStats,
}

impl Player {
Expand All @@ -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
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/player/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerInputs<GgrsConfig>>,
Expand Down Expand Up @@ -42,7 +42,9 @@ pub fn accelerate_players(inputs: Res<PlayerInputs<GgrsConfig>>, 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);
}
}

Expand Down

0 comments on commit a644a4f

Please sign in to comment.