Skip to content

Commit

Permalink
Merge pull request #92 from johanhelsing/box-game-improvements
Browse files Browse the repository at this point in the history
Box game improvements
  • Loading branch information
gschup authored Nov 13, 2023
2 parents 8fcadd1 + 7195349 commit 97d76be
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
38 changes: 12 additions & 26 deletions examples/box_game/box_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ pub type BoxConfig = GgrsConfig<BoxInput>;

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Pod, Zeroable)]
pub struct BoxInput {
pub inp: u8,
}
pub struct BoxInput(u8);

#[derive(Default, Component)]
pub struct Player {
Expand All @@ -43,12 +41,8 @@ pub struct Player {
// - Copy
// - Reflect
// See `bevy_ggrs::Strategy` for custom alternatives
#[derive(Default, Reflect, Component, Clone)]
pub struct Velocity {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Default, Reflect, Component, Clone, Copy, Deref, DerefMut)]
pub struct Velocity(pub Vec3);

// You can also register resources.
#[derive(Resource, Default, Reflect, Hash, Clone, Copy)]
Expand Down Expand Up @@ -81,7 +75,7 @@ pub fn read_local_inputs(
input |= INPUT_RIGHT;
}

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

commands.insert_resource(LocalInputs::<BoxConfig>(local_inputs));
Expand Down Expand Up @@ -110,6 +104,7 @@ pub fn setup_system(
});

let r = PLANE_SIZE / 4.;
let mesh = meshes.add(Mesh::from(shape::Cube { size: CUBE_SIZE }));

for handle in 0..num_players {
let rot = handle as f32 / num_players as f32 * 2. * std::f32::consts::PI;
Expand All @@ -127,7 +122,7 @@ pub fn setup_system(
.spawn((
// ...add visual information...
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: CUBE_SIZE })),
mesh: mesh.clone(),
material: materials.add(color.into()),
transform,
..default()
Expand Down Expand Up @@ -176,7 +171,7 @@ pub fn move_cube_system(
let dt = time.delta().as_secs_f32();

for (mut t, mut v, p) in query.iter_mut() {
let input = inputs[p.handle].0.inp;
let input = inputs[p.handle].0 .0;
// set velocity through key presses
if input & INPUT_UP != 0 && input & INPUT_DOWN == 0 {
v.z -= ACCELERATION * dt;
Expand All @@ -201,23 +196,14 @@ pub fn move_cube_system(
v.y *= FRICTION.powf(dt);

// constrain velocity
let mag = (v.x * v.x + v.y * v.y + v.z * v.z).sqrt();
if mag > MAX_SPEED {
let factor = MAX_SPEED / mag;
v.x *= factor;
v.y *= factor;
v.z *= factor;
}
**v = v.clamp_length_max(MAX_SPEED);

// apply velocity
t.translation.x += v.x * dt;
t.translation.y += v.y * dt;
t.translation.z += v.z * dt;
t.translation += **v * dt;

// constrain cube to plane
t.translation.x = t.translation.x.max(-1. * (PLANE_SIZE - CUBE_SIZE) * 0.5);
t.translation.x = t.translation.x.min((PLANE_SIZE - CUBE_SIZE) * 0.5);
t.translation.z = t.translation.z.max(-1. * (PLANE_SIZE - CUBE_SIZE) * 0.5);
t.translation.z = t.translation.z.min((PLANE_SIZE - CUBE_SIZE) * 0.5);
let half_width = (PLANE_SIZE - CUBE_SIZE) * 0.5;
t.translation.x = t.translation.x.clamp(-half_width, half_width);
t.translation.z = t.translation.z.clamp(-half_width, half_width);
}
}
5 changes: 3 additions & 2 deletions examples/box_game/box_game_p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Rollback behavior can be customized using a variety of extension methods and plugins:
// The FrameCount resource implements Copy, we can use that to have minimal overhead rollback
.rollback_resource_with_copy::<FrameCount>()
// Transform and Velocity components only implement Clone, so instead we'll use that to snapshot and rollback with
// Same with the Velocity Component
.rollback_component_with_copy::<Velocity>()
// Transform only implement Clone, so instead we'll use that to snapshot and rollback with
.rollback_component_with_clone::<Transform>()
.rollback_component_with_clone::<Velocity>()
.insert_resource(opt)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
Expand Down
5 changes: 3 additions & 2 deletions examples/box_game/box_game_spectator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Rollback behavior can be customized using a variety of extension methods and plugins:
// The FrameCount resource implements Copy, we can use that to have minimal overhead rollback
.rollback_resource_with_copy::<FrameCount>()
// Transform and Velocity components only implement Clone, so instead we'll use that to snapshot and rollback with
// Same with the Velocity Component
.rollback_component_with_copy::<Velocity>()
// Transform only implement Clone, so instead we'll use that to snapshot and rollback with
.rollback_component_with_clone::<Transform>()
.rollback_component_with_clone::<Velocity>()
.insert_resource(opt)
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup_system)
Expand Down
5 changes: 3 additions & 2 deletions examples/box_game/box_game_synctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Rollback behavior can be customized using a variety of extension methods and plugins:
// The FrameCount resource implements Copy, we can use that to have minimal overhead rollback
.rollback_resource_with_copy::<FrameCount>()
// Transform and Velocity components only implement Clone, so instead we'll use that to snapshot and rollback with
// Same with the Velocity Component
.rollback_component_with_copy::<Velocity>()
// Transform only implement Clone, so instead we'll use that to snapshot and rollback with
.rollback_component_with_clone::<Transform>()
.rollback_component_with_clone::<Velocity>()
.add_systems(Startup, setup_system)
// these systems will be executed as part of the advance frame update
.add_systems(GgrsSchedule, (move_cube_system, increase_frame_system))
Expand Down

0 comments on commit 97d76be

Please sign in to comment.