Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
add note about replicon integration
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Oct 6, 2023
1 parent dbd339c commit 66e461a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions REPLICON_INTEGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Integrating bevy_timewarp with bevy_replicon

I'm using [bevy_replicon](https://crates.io/crates/bevy_replicon) in my game, alongside bevy_timewarp.
You can use custom deserializers with replicon to write updates into the `ServerSnapshot` buffer like this:

### Custom timewarp deserializer example

This is for [bevy_xpbd](https://crates.io/crates/bevy_xpbd_2d)'s `Rotation`` component:

```rust
pub fn deserialize_rotation_timewarp(
entity: &mut EntityMut,
_entity_map: &mut NetworkEntityMap,
mut cursor: &mut Cursor<Bytes>,
tick: RepliconTick,
) -> Result<(), bincode::Error> {
let sin: f32 = bincode::deserialize_from(&mut cursor)?;
let cos: f32 = bincode::deserialize_from(&mut cursor)?;
let comp = Rotation::from_sin_cos(sin, cos);
if let Some(mut ss) = entity.get_mut::<ServerSnapshot<Rotation>>() {
ss.insert(tick.get(), comp);
} else {
entity.insert(comp);
}
if let Some(mut sync_status) = entity.get_mut::<SyncStatus>() {
sync_status.last_snapshot_rot = Some(comp);
sync_status.last_update_frame = tick.get();
}
Ok(())
}

pub fn serialize_rotation(
component: Ptr,
mut cursor: &mut Cursor<Vec<u8>>,
) -> Result<(), bincode::Error> {
// SAFETY: Function called for registered `ComponentId`.
let comp: &Rotation = unsafe { component.deref() };
bincode::serialize_into(&mut cursor, &comp.sin())?;
bincode::serialize_into(&mut cursor, &comp.cos())
}
```

0 comments on commit 66e461a

Please sign in to comment.