Skip to main content

WorldState

Trait WorldState 

Source
pub trait WorldState: Send {
    // Required methods
    fn get_local_id(&self, network_id: NetworkId) -> Option<LocalId>;
    fn get_network_id(&self, local_id: LocalId) -> Option<NetworkId>;
    fn extract_deltas(&mut self) -> Vec<ReplicationEvent>;
    fn apply_updates(&mut self, updates: &[(ClientId, ComponentUpdate)]);
    fn spawn_networked(&mut self) -> NetworkId;
    fn despawn_networked(
        &mut self,
        network_id: NetworkId,
    ) -> Result<(), WorldError>;

    // Provided methods
    fn advance_tick(&mut self) { ... }
    fn simulate(&mut self) { ... }
    fn spawn_networked_for(&mut self, _client_id: ClientId) -> NetworkId { ... }
    fn stress_test(&mut self, _count: u16, _rotate: bool) { ... }
    fn spawn_kind(
        &mut self,
        _kind: u16,
        _x: f32,
        _y: f32,
        _rot: f32,
    ) -> NetworkId { ... }
    fn clear_world(&mut self) { ... }
}
Expand description

The ECS Facade. Translates between the engine’s protocol-level types and the concrete ECS’s internal representation.

§Why this exists

Bevy uses Entity, an opaque 64-bit handle with generation bits. Our network protocol uses NetworkId, a globally unique u64. This trait is the translation layer. The game loop never touches a Bevy Entity directly — it only speaks NetworkId.

§Delta extraction

On every tick, modified components are detected and emitted as ReplicationEvent items. Only changed fields are sent — never the full component. This is the foundation of delta compression.

Required Methods§

Source

fn get_local_id(&self, network_id: NetworkId) -> Option<LocalId>

Maps a protocol-level NetworkId to the ECS’s local entity handle.

Returns None if the entity has been despawned or never existed.

Source

fn get_network_id(&self, local_id: LocalId) -> Option<NetworkId>

Maps a local ECS entity handle back to its protocol-level NetworkId.

Returns None if the entity is not network-replicated.

Source

fn extract_deltas(&mut self) -> Vec<ReplicationEvent>

Extracts replication deltas for all components modified since the last tick.

The returned events contain only the changed fields, not full snapshots. The caller (the game loop) never interprets these events — it passes them directly to the Encoder for serialization.

Source

fn apply_updates(&mut self, updates: &[(ClientId, ComponentUpdate)])

Injects parsed state updates from the network into the ECS.

On the server, these are client inputs (movement commands, actions). On the client, these are authoritative state corrections from the server.

The ClientId in the update pair provides context for ownership verification to prevent unauthorized updates from malicious clients.

Source

fn spawn_networked(&mut self) -> NetworkId

Spawns a new network-replicated entity and returns its NetworkId.

Source

fn despawn_networked(&mut self, network_id: NetworkId) -> Result<(), WorldError>

Despawn a network-replicated entity by its NetworkId.

§Errors

Returns WorldError if the entity with the given network_id does not exist.

Provided Methods§

Source

fn advance_tick(&mut self)

Advances the world change tick at the start of each server tick, before inputs are applied.

Source

fn simulate(&mut self)

Runs a single simulation frame for the ECS.

Source

fn spawn_networked_for(&mut self, _client_id: ClientId) -> NetworkId

Spawns a new network-replicated entity owned by a specific client.

Source

fn stress_test(&mut self, _count: u16, _rotate: bool)

Triggers a bulk spawn of entities for stress testing.

Source

fn spawn_kind(&mut self, _kind: u16, _x: f32, _y: f32, _rot: f32) -> NetworkId

Spawns a new network-replicated entity of a specific kind.

Source

fn clear_world(&mut self)

Despawns all entities from the world.

Implementors§