Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow local input to be disabled on SessionRunner. #411

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions framework_crates/bones_framework/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ pub struct GgrsSessionRunner<'a, InputTypes: NetworkInputConfig<'a>> {
/// Session runner's input collector.
pub input_collector: InputTypes::InputCollector,

/// Is local input disabled? (No input will be used if set)
pub local_input_disabled: bool,

/// Players who have been reported disconnected by ggrs
disconnected_players: Vec<usize>,

Expand Down Expand Up @@ -353,6 +356,7 @@ where
input_collector: InputTypes::InputCollector::default(),
socket: info.socket.clone(),
local_input_delay,
local_input_disabled: false,
}
}
}
Expand Down Expand Up @@ -455,9 +459,17 @@ where
if self.accumulator >= step {
self.accumulator -= step;

self.session
.add_local_input(self.local_player_idx, self.last_player_input)
.unwrap();
if !self.local_input_disabled {
self.session
.add_local_input(self.local_player_idx, self.last_player_input)
.unwrap();
} else {
// If local input is disabled, we still submit a default value representing no-inputs.
// This way if input is disabled current inputs will not be held down indefinitely.
self.session
.add_local_input(self.local_player_idx, InputTypes::Dense::default())
.unwrap();
}

let current_frame = self.session.current_frame();
let confirmed_frame = self.session.confirmed_frame();
Expand Down Expand Up @@ -503,6 +515,11 @@ where
// Input has been consumed, signal that we are in new input frame
self.input_collector.advance_frame();

// TODO: Make sure NetworkInfo is initialized immediately when session is created,
// even before a frame has advanced.
//
// The existance of this resource may be used to determine if in an online match, and there could
// be race if expected it to exist but testing before first frame advance.
world.insert_resource(NetworkInfo {
current_frame: self.session.current_frame(),
last_confirmed_frame: self.session.confirmed_frame(),
Expand Down Expand Up @@ -599,4 +616,8 @@ where
};
*self = GgrsSessionRunner::new(self.original_fps as f32, runner_info);
}

fn disable_local_input(&mut self, input_disabled: bool) {
self.local_input_disabled = input_disabled;
}
}
7 changes: 7 additions & 0 deletions framework_crates/bones_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub trait SessionRunner: Sync + Send + 'static {
/// stages.run(world);
/// }
/// fn restart_session(&mut self) {}
/// fn disable_local_input(&mut self, disable_input: bool) {}
/// # }
/// ```
fn step(&mut self, now: Instant, world: &mut World, stages: &mut SystemStages);
Expand All @@ -149,6 +150,9 @@ pub trait SessionRunner: Sync + Send + 'static {
/// The expectation is that current players using it may continue to, so something like a network
/// socket or player info should persist.
fn restart_session(&mut self);

/// Disable the capture of local input by this session.
fn disable_local_input(&mut self, input_disabled: bool);
}

/// The default [`SessionRunner`], which just runs the systems once every time it is run.
Expand All @@ -165,6 +169,9 @@ impl SessionRunner for DefaultSessionRunner {
fn restart_session(&mut self) {
*self = DefaultSessionRunner::default();
}

// `DefaultSessionRunner` does not collect input so this impl is not relevant.
fn disable_local_input(&mut self, _input_disabled: bool) {}
}

/// The [`Game`] encompasses a complete bones game's logic, independent of the renderer and IO
Expand Down
Loading