Skip to content

Commit

Permalink
Merge pull request #114 from johanhelsing/bevy-0.15
Browse files Browse the repository at this point in the history
Bevy 0.15 and latest ggrs main
  • Loading branch information
gschup authored Dec 19, 2024
2 parents 8269891 + 80dd3a5 commit 33cf3ae
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 82 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_ggrs"
version = "0.16.0"
version = "0.17.0"
authors = ["Georg Schuppe <georg.schuppe@gmail.com>"]
edition = "2021"
description = "Bevy plugin for the GGRS P2P rollback networking library"
Expand All @@ -16,16 +16,17 @@ categories = ["network-programming", "game-development"]
wasm-bindgen = ["instant/wasm-bindgen", "ggrs/wasm-bindgen"]

[dependencies]
bevy = { version = "0.14", default-features = false }
bytemuck = { version = "1.7", features = ["derive"] }
bevy = { version = "0.15", default-features = false }
instant = { version = "0.1", optional = true }
log = "0.4"
#ggrs = { version= "0.10.2", features=["sync-send"]}
ggrs = { git = "https://github.com/gschup/ggrs", features = ["sync-send"] }
seahash = "4.1"
disqualified = "1.0.0"
serde = { version = "1", default-features = false }

[dev-dependencies]
bevy = { version = "0.14", default-features = true }
bevy = { version = "0.15", default-features = true }
clap = { version = "4.4", features = ["derive"] }
rand = "0.8.4"
rand_xoshiro = "0.6"
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ bevy_GGRS has a demo app you can try in the browser! It uses [matchbox](https://

## Compatible Versions

|bevy|bevy_ggrs|ggrs|
|---|---|---|
|0.14|main|main|
|0.14|0.16|0.10.2|
|0.13|0.15|0.10.1|
|0.12|0.14|0.10|
|0.11|0.13|0.9.4|
|0.10|0.12|0.9.4|
|0.9|0.11|0.9.3|
|0.8|0.10|0.9|
|0.6|0.9|0.9|
| bevy | bevy_ggrs | ggrs |
| ---- | --------- | ------ |
| 0.15 | main | main |
| 0.14 | 0.16 | 0.10.2 |
| 0.13 | 0.15 | 0.10.1 |
| 0.12 | 0.14 | 0.10 |
| 0.11 | 0.13 | 0.9.4 |
| 0.10 | 0.12 | 0.9.4 |
| 0.9 | 0.11 | 0.9.3 |
| 0.8 | 0.10 | 0.9 |
| 0.6 | 0.9 | 0.9 |

## Thanks

Expand Down
35 changes: 14 additions & 21 deletions examples/box_game/box_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy_ggrs::{
AddRollbackCommandExtension, GgrsConfig, LocalInputs, LocalPlayers, PlayerInputs, Rollback,
Session,
};
use bytemuck::{Pod, Zeroable};
use serde::{Deserialize, Serialize};
use std::hash::Hash;

const BLUE: Color = Color::srgb(0.8, 0.6, 0.2);
Expand All @@ -28,7 +28,7 @@ const CUBE_SIZE: f32 = 0.2;
pub type BoxConfig = GgrsConfig<BoxInput>;

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Pod, Zeroable)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct BoxInput(u8);

#[derive(Default, Component)]
Expand Down Expand Up @@ -94,11 +94,10 @@ pub fn setup_system(
};

// A ground plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(PLANE_SIZE / 2.0))),
material: materials.add(StandardMaterial::from(Color::srgb(0.3, 0.5, 0.3))),
..default()
});
commands.spawn((
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(PLANE_SIZE / 2.0)))),
MeshMaterial3d(materials.add(StandardMaterial::from(Color::srgb(0.3, 0.5, 0.3)))),
));

let r = PLANE_SIZE / 4.;
let mesh = meshes.add(Cuboid::from_length(CUBE_SIZE));
Expand All @@ -118,12 +117,9 @@ pub fn setup_system(
commands
.spawn((
// ...add visual information...
PbrBundle {
mesh: mesh.clone(),
material: materials.add(StandardMaterial::from(color)),
transform,
..default()
},
Mesh3d(mesh.clone()),
MeshMaterial3d(materials.add(StandardMaterial::from(color))),
transform,
// ...flags...
Player { handle },
// ...and components which will be rolled-back...
Expand All @@ -135,15 +131,12 @@ pub fn setup_system(
}

// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
commands.spawn((PointLight::default(), Transform::from_xyz(4.0, 8.0, 4.0)));
// camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 7.5, 0.5).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 7.5, 0.5).looking_at(Vec3::ZERO, Vec3::Y),
));
}

// Example system, manipulating a resource, will be added to the rollback schedule.
Expand Down
1 change: 0 additions & 1 deletion examples/box_game/box_game_p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_num_players(num_players)
.with_desync_detection_mode(ggrs::DesyncDetection::On { interval: 10 }) // (optional) set how often to exchange state checksums
.with_max_prediction_window(12)
.expect("prediction window can't be 0") // (optional) set max prediction window
.with_input_delay(2); // (optional) set input delay for the local player

// add players
Expand Down
17 changes: 4 additions & 13 deletions examples/stress_tests/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut session_builder = SessionBuilder::<Config>::new()
.with_num_players(num_players)
.with_desync_detection_mode(desync_mode)
.with_max_prediction_window(args.max_prediction)?
.with_max_prediction_window(args.max_prediction)
.with_input_delay(args.input_delay);

for (i, player_addr) in args.players.iter().enumerate() {
Expand Down Expand Up @@ -170,7 +170,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
app.rollback_component_with_reflect::<Sprite>()
.rollback_component_with_reflect::<Transform>()
.rollback_component_with_reflect::<GlobalTransform>()
.rollback_component_with_reflect::<Handle<Image>>()
.rollback_component_with_reflect::<Visibility>()
.rollback_component_with_reflect::<InheritedVisibility>()
.rollback_component_with_reflect::<ViewVisibility>()
Expand All @@ -188,7 +187,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
app.rollback_component_with_clone::<Sprite>()
.rollback_component_with_clone::<Transform>()
.rollback_component_with_clone::<GlobalTransform>()
.rollback_component_with_clone::<Handle<Image>>()
.rollback_component_with_clone::<Visibility>()
.rollback_component_with_clone::<InheritedVisibility>()
.rollback_component_with_clone::<ViewVisibility>()
Expand Down Expand Up @@ -246,7 +244,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);
}

fn spawn_pressed(inputs: Res<PlayerInputs<Config>>) -> bool {
Expand All @@ -260,14 +258,7 @@ fn spawn_particles(mut commands: Commands, args: Res<Args>, mut rng: ResMut<Part
for _ in 0..args.rate {
commands
.spawn((
SpriteBundle {
sprite: Sprite {
color: ORANGE.into(),
custom_size: Some(Vec2::splat(5.0)),
..default()
},
..default()
},
Sprite::from_color(ORANGE, Vec2::splat(5.0)),
Velocity(vec3(rng.gen_range(-s..s), rng.gen_range(-s..s), 0.0)),
Ttl(ttl),
))
Expand All @@ -276,7 +267,7 @@ fn spawn_particles(mut commands: Commands, args: Res<Args>, mut rng: ResMut<Part
}

fn update_particles(mut particles: Query<(&mut Transform, &mut Velocity)>, time: Res<Time>) {
let time_step = time.delta_seconds();
let time_step = time.delta_secs();
let gravity = Vec3::NEG_Y * 200.0;

for (mut transform, mut velocity) in &mut particles {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bevy::{
utils::{Duration, HashMap},
};
use ggrs::{Config, InputStatus, P2PSession, PlayerHandle, SpectatorSession, SyncTestSession};
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, hash::Hash, marker::PhantomData, net::SocketAddr};

pub use ggrs;
Expand Down Expand Up @@ -47,7 +48,7 @@ pub struct GgrsConfig<Input, Address = SocketAddr, State = u8> {
impl<Input, Address, State> Config for GgrsConfig<Input, Address, State>
where
Self: 'static,
Input: Send + Sync + PartialEq + bytemuck::Pod,
Input: Send + Sync + PartialEq + Serialize + for<'a> Deserialize<'a> + Default + Copy,
Address: Send + Sync + Debug + Hash + Eq + Clone,
State: Send + Sync + Clone,
{
Expand Down
6 changes: 3 additions & 3 deletions src/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ pub trait AddRollbackCommandExtension: private::AddRollbackCommandExtensionSeal
fn add_rollback(&mut self) -> &mut Self;
}

impl<'a> private::AddRollbackCommandExtensionSeal for EntityCommands<'a> {}
impl private::AddRollbackCommandExtensionSeal for EntityCommands<'_> {}

impl<'a> AddRollbackCommandExtension for EntityCommands<'a> {
impl AddRollbackCommandExtension for EntityCommands<'_> {
fn add_rollback(&mut self) -> &mut Self {
self.add(AddRollbackCommand);
self.queue(AddRollbackCommand);
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot/component_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ where

trace!(
"Component {} has checksum {:X}",
bevy::utils::get_short_name(std::any::type_name::<C>()),
disqualified::ShortName::of::<C>(),
result.0
);

Expand Down
5 changes: 1 addition & 4 deletions src/snapshot/component_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ where
}
}

trace!(
"Mapped {}",
bevy::utils::get_short_name(std::any::type_name::<C>())
);
trace!("Mapped {}", disqualified::ShortName::of::<C>());
}

impl<C> Plugin for ComponentMapEntitiesPlugin<C>
Expand Down
4 changes: 2 additions & 2 deletions src/snapshot/component_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
trace!(
"Snapshot {} {} component(s)",
snapshot.iter().count(),
bevy::utils::get_short_name(std::any::type_name::<S::Target>())
disqualified::ShortName::of::<S::Target>()
);

snapshots.push(frame.0, snapshot);
Expand Down Expand Up @@ -98,7 +98,7 @@ where
trace!(
"Rolled back {} {} component(s)",
snapshot.iter().count(),
bevy::utils::get_short_name(std::any::type_name::<S::Target>())
disqualified::ShortName::of::<S::Target>()
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot/resource_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where

trace!(
"Resource {} has checksum {:X}",
bevy::utils::get_short_name(std::any::type_name::<R>()),
disqualified::ShortName::of::<R>(),
result.0
);

Expand Down
5 changes: 1 addition & 4 deletions src/snapshot/resource_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ where
resource.map_entities(&mut map.as_ref());
}

trace!(
"Mapped {}",
bevy::utils::get_short_name(std::any::type_name::<R>())
);
trace!("Mapped {}", disqualified::ShortName::of::<R>());
}

impl<R> Plugin for ResourceMapEntitiesPlugin<R>
Expand Down
10 changes: 2 additions & 8 deletions src/snapshot/resource_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ where
) {
snapshots.push(frame.0, resource.map(|res| S::store(res.as_ref())));

trace!(
"Snapshot {}",
bevy::utils::get_short_name(std::any::type_name::<S::Target>())
);
trace!("Snapshot {}", disqualified::ShortName::of::<S::Target>());
}

pub fn load(
Expand All @@ -83,10 +80,7 @@ where
(None, None) => {}
}

trace!(
"Rolled back {}",
bevy::utils::get_short_name(std::any::type_name::<S::Target>())
);
trace!("Rolled back {}", disqualified::ShortName::of::<S::Target>());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/snapshot/rollback_entity_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl RollbackEntityMap {
}
}

impl<'a> EntityMapper for &'a RollbackEntityMap {
impl EntityMapper for &RollbackEntityMap {
/// Map the provided [`Entity`], or return it unmodified if it does not need to be mapped.
fn map_entity(&mut self, entity: Entity) -> Entity {
self.get(entity).unwrap_or(entity)
Expand Down
4 changes: 2 additions & 2 deletions src/snapshot/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::marker::PhantomData;

use bevy::{
prelude::{FromWorld, World},
reflect::Reflect,
reflect::{PartialReflect, Reflect},
};

/// Describes how to efficiently transform a [`Target`](`Strategy::Target`) into a
Expand Down Expand Up @@ -77,7 +77,7 @@ pub struct ReflectStrategy<T: Reflect + FromWorld>(PhantomData<T>);
impl<T: Reflect + FromWorld> Strategy for ReflectStrategy<T> {
type Target = T;

type Stored = Box<dyn Reflect>;
type Stored = Box<dyn PartialReflect>;

#[inline(always)]
fn store(target: &Self::Target) -> Self::Stored {
Expand Down
4 changes: 2 additions & 2 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl Default for RollbackFrameRate {
/// # app.add_systems(ReadInputs, read_local_inputs);
/// # app.insert_resource(session);
/// fn get_in_game_time(real_time: Res<Time<Real>>, game_time: Res<Time<GgrsTime>>) {
/// info!("Real Time: {}", real_time.elapsed_seconds());
/// info!("Game Time: {}", game_time.elapsed_seconds());
/// info!("Real Time: {}", real_time.elapsed_secs());
/// info!("Game Time: {}", game_time.elapsed_secs());
/// }
/// # app.add_systems(Update, get_in_game_time);
/// # }
Expand Down
6 changes: 3 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use bevy_ggrs::{
AddRollbackCommandExtension, GgrsConfig, GgrsPlugin, GgrsSchedule, LocalInputs, LocalPlayers,
PlayerInputs, ReadInputs, Rollback, Session,
};
use bytemuck::{Pod, Zeroable};
use ggrs::{Config, P2PSession, PlayerHandle, PlayerType, SessionBuilder, UdpNonBlockingSocket};
use serde::{Deserialize, Serialize};
use serial_test::serial;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

Expand Down Expand Up @@ -91,7 +91,7 @@ fn create_app<T: Config>(session: P2PSession<T>) -> App {
type TestConfig = GgrsConfig<BoxInput>;

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Pod, Zeroable)]
#[derive(Copy, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct BoxInput {
pub inp: u8,
}
Expand Down Expand Up @@ -125,7 +125,6 @@ fn start_session(
let mut session_builder = SessionBuilder::<TestConfig>::new()
.with_num_players(2)
.with_max_prediction_window(12)
.expect("prediction window can't be 0") // (optional) set max prediction window
.with_input_delay(2); // (optional) set input delay for the local player
session_builder = session_builder.add_player(PlayerType::Local, local_player.handle)?;
session_builder = session_builder.add_player(
Expand Down Expand Up @@ -167,6 +166,7 @@ fn press_key(app: &mut App, key: KeyCode) {
key_code: key,
state: ButtonState::Pressed,
window: Entity::PLACEHOLDER,
repeat: false,
});
}

Expand Down

0 comments on commit 33cf3ae

Please sign in to comment.