Skip to content

Commit

Permalink
Make GgrsPlugin a regular Bevy plugin
Browse files Browse the repository at this point in the history
- `GgrsPlugin` is now just a regular bevy plugin, and the builder is gone.
- Adds a `ReadInputs` schedule, where inputs should be inserted into a `LocalInputs` resource.
- Registering rollback types are now done on the app instead.
- Setting the FPS is now done through a command extension on App

Co-authored-by: Georg Friedrich Schuppe <georg.schuppe@gmail.com>
  • Loading branch information
johanhelsing and gschup committed Sep 28, 2023
1 parent 7516867 commit 57ceab3
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 354 deletions.
45 changes: 29 additions & 16 deletions examples/box_game/box_game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_ggrs::{AddRollbackCommandExtension, PlayerInputs, Rollback, Session};
use bevy::{prelude::*, utils::HashMap};
use bevy_ggrs::{AddRollbackCommandExtension, LocalInputs, PlayerInputs, Rollback, Session};
use bytemuck::{Pod, Zeroable};
use ggrs::{Config, PlayerHandle};
use std::{hash::Hash, net::SocketAddr};
Expand Down Expand Up @@ -31,6 +31,9 @@ impl Config for GgrsConfig {
type Address = SocketAddr;
}

#[derive(Resource)]
pub struct LocalPlayers(pub Vec<PlayerHandle>);

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Pod, Zeroable)]
pub struct BoxInput {
Expand All @@ -57,23 +60,33 @@ pub struct FrameCount {
pub frame: u32,
}

pub fn input(_handle: In<PlayerHandle>, keyboard_input: Res<Input<KeyCode>>) -> BoxInput {
let mut input: u8 = 0;
pub fn read_local_inputs(
mut commands: Commands,
keyboard_input: Res<Input<KeyCode>>,
local_players: Res<LocalPlayers>,
) {
let mut local_inputs = HashMap::new();

if keyboard_input.pressed(KeyCode::W) {
input |= INPUT_UP;
}
if keyboard_input.pressed(KeyCode::A) {
input |= INPUT_LEFT;
}
if keyboard_input.pressed(KeyCode::S) {
input |= INPUT_DOWN;
}
if keyboard_input.pressed(KeyCode::D) {
input |= INPUT_RIGHT;
for handle in &local_players.0 {
let mut input: u8 = 0;

if keyboard_input.pressed(KeyCode::W) {
input |= INPUT_UP;
}
if keyboard_input.pressed(KeyCode::A) {
input |= INPUT_LEFT;
}
if keyboard_input.pressed(KeyCode::S) {
input |= INPUT_DOWN;
}
if keyboard_input.pressed(KeyCode::D) {
input |= INPUT_RIGHT;
}

local_inputs.insert(*handle, BoxInput { inp: input });
}

BoxInput { inp: input }
commands.insert_resource(LocalInputs::<GgrsConfig>(local_inputs));
}

pub fn setup_system(
Expand Down
23 changes: 11 additions & 12 deletions examples/box_game/box_game_p2p.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::net::SocketAddr;

use bevy::{prelude::*, window::WindowResolution};
use bevy_ggrs::{GgrsAppExtension, GgrsPlugin, GgrsSchedule, Session};
use bevy_ggrs::{GgrsApp, GgrsPlugin, GgrsSchedule, ReadInputs, Session};
use ggrs::{GGRSEvent as GgrsEvent, PlayerType, SessionBuilder, UdpNonBlockingSocket};

use structopt::StructOpt;
Expand Down Expand Up @@ -60,17 +60,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let sess = sess_build.start_p2p_session(socket)?;

App::new()
.add_ggrs_plugin(
GgrsPlugin::<GgrsConfig>::new()
// define frequency of rollback game logic update
.with_update_frequency(FPS)
// define system that returns inputs given a player handle, so GGRS can send the inputs around
.with_input_system(input)
// register types of components AND resources you want to be rolled back
.register_rollback_component::<Transform>()
.register_rollback_component::<Velocity>()
.register_rollback_resource::<FrameCount>(),
)
.insert_resource(LocalPlayers(sess.local_player_handles()))
.add_plugins(GgrsPlugin::<GgrsConfig>::default())
// define frequency of rollback game logic update
.set_rollback_schedule_fps(FPS)
// this system will be executed as part of input reading
.add_systems(ReadInputs, read_local_inputs)
// register types of components AND resources you want to be rolled back
.register_rollback_component::<Transform>()
.register_rollback_component::<Velocity>()
.register_rollback_resource::<FrameCount>()
.insert_resource(opt)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
Expand Down
23 changes: 11 additions & 12 deletions examples/box_game/box_game_spectator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::net::SocketAddr;

use bevy::prelude::*;
use bevy_ggrs::{GgrsAppExtension, GgrsPlugin, GgrsSchedule, Session};
use bevy_ggrs::{GgrsApp, GgrsPlugin, GgrsSchedule, ReadInputs, Session};
use ggrs::{SessionBuilder, UdpNonBlockingSocket};
use structopt::StructOpt;

Expand Down Expand Up @@ -37,17 +37,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.start_spectator_session(opt.host, socket);

App::new()
.add_ggrs_plugin(
GgrsPlugin::<GgrsConfig>::new()
// define frequency of rollback game logic update
.with_update_frequency(FPS)
// define system that returns inputs given a player handle, so GGRS can send the inputs around
.with_input_system(input)
// register types of components AND resources you want to be rolled back
.register_rollback_component::<Transform>()
.register_rollback_component::<Velocity>()
.register_rollback_resource::<FrameCount>(),
)
.insert_resource(LocalPlayers((0..opt.num_players).collect()))
.add_plugins(GgrsPlugin::<GgrsConfig>::default())
// define frequency of rollback game logic update
.set_rollback_schedule_fps(FPS)
// this system will be executed as part of input reading
.add_systems(ReadInputs, read_local_inputs)
// register types of components AND resources you want to be rolled back
.register_rollback_component::<Transform>()
.register_rollback_component::<Velocity>()
.register_rollback_resource::<FrameCount>()
.insert_resource(opt)
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup_system)
Expand Down
22 changes: 10 additions & 12 deletions examples/box_game/box_game_synctest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_ggrs::{GgrsAppExtension, GgrsPlugin, GgrsSchedule, Session};
use bevy_ggrs::{GgrsApp, GgrsPlugin, GgrsSchedule, ReadInputs, Session};
use ggrs::{PlayerType, SessionBuilder};
use structopt::StructOpt;

Expand Down Expand Up @@ -37,17 +37,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let sess = sess_build.start_synctest_session()?;

App::new()
.add_ggrs_plugin(
GgrsPlugin::<GgrsConfig>::new()
// define frequency of rollback game logic update
.with_update_frequency(FPS)
// define system that returns inputs given a player handle, so GGRS can send the inputs around
.with_input_system(input)
// register types of components AND resources you want to be rolled back
.register_rollback_component::<Transform>()
.register_rollback_component::<Velocity>()
.register_rollback_resource::<FrameCount>(),
)
.add_plugins(GgrsPlugin::<GgrsConfig>::default())
// define frequency of rollback game logic update
.set_rollback_schedule_fps(FPS)
// this system will be executed as part of input reading
.add_systems(ReadInputs, read_local_inputs)
// register types of components AND resources you want to be rolled back
.register_rollback_component::<Transform>()
.register_rollback_component::<Velocity>()
.register_rollback_resource::<FrameCount>()
.insert_resource(opt)
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup_system)
Expand Down
Loading

0 comments on commit 57ceab3

Please sign in to comment.