pub trait WorldState: Send {
Show 20 methods
// 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 extract_reliable_events(&mut self) -> Vec<(Option<ClientId>, WireEvent)> { ... }
fn advance_tick(&mut self) { ... }
fn simulate(&mut self) { ... }
fn post_extract(&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 spawn_kind_for(
&mut self,
kind: u16,
x: f32,
y: f32,
rot: f32,
_client_id: ClientId,
) -> NetworkId { ... }
fn spawn_session_ship(
&mut self,
kind: u16,
x: f32,
y: f32,
rot: f32,
client_id: ClientId,
) -> NetworkId { ... }
fn clear_world(&mut self) { ... }
fn queue_reliable_event(
&mut self,
_client_id: Option<ClientId>,
_event: GameEvent,
) { ... }
fn setup_world(&mut self) { ... }
fn get_entity_room(&self, _network_id: NetworkId) -> Option<NetworkId> { ... }
fn get_client_room(&self, _client_id: ClientId) -> Option<NetworkId> { ... }
}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§
Sourcefn get_local_id(&self, network_id: NetworkId) -> Option<LocalId>
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.
Sourcefn get_network_id(&self, local_id: LocalId) -> Option<NetworkId>
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.
Sourcefn extract_deltas(&mut self) -> Vec<ReplicationEvent>
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.
Sourcefn apply_updates(&mut self, updates: &[(ClientId, ComponentUpdate)])
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.
Sourcefn spawn_networked(&mut self) -> NetworkId
fn spawn_networked(&mut self) -> NetworkId
Spawns a new network-replicated entity and returns its NetworkId.
Sourcefn despawn_networked(&mut self, network_id: NetworkId) -> Result<(), WorldError>
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§
Sourcefn extract_reliable_events(&mut self) -> Vec<(Option<ClientId>, WireEvent)>
fn extract_reliable_events(&mut self) -> Vec<(Option<ClientId>, WireEvent)>
Extracts discrete game events that should be sent reliably.
Returns a list of (Target ClientId, WireEvent).
If ClientId is None, the event should be broadcast to all authenticated clients.
Sourcefn advance_tick(&mut self)
fn advance_tick(&mut self)
Advances the world change tick at the start of each server tick, before inputs are applied.
Sourcefn post_extract(&mut self)
fn post_extract(&mut self)
Called once per tick after extract_deltas, before the next advance_tick.
Implementations should use this to clear ECS change-detection trackers so that only mutations from the next simulation step appear as changed in the following extraction. The default no-op is safe for non-Bevy adapters.
Sourcefn spawn_networked_for(&mut self, _client_id: ClientId) -> NetworkId
fn spawn_networked_for(&mut self, _client_id: ClientId) -> NetworkId
Spawns a new network-replicated entity owned by a specific client.
Sourcefn stress_test(&mut self, _count: u16, _rotate: bool)
fn stress_test(&mut self, _count: u16, _rotate: bool)
Triggers a bulk spawn of entities for stress testing.
Sourcefn spawn_kind(&mut self, _kind: u16, _x: f32, _y: f32, _rot: f32) -> NetworkId
fn spawn_kind(&mut self, _kind: u16, _x: f32, _y: f32, _rot: f32) -> NetworkId
Spawns a new network-replicated entity of a specific kind.
Sourcefn spawn_kind_for(
&mut self,
kind: u16,
x: f32,
y: f32,
rot: f32,
_client_id: ClientId,
) -> NetworkId
fn spawn_kind_for( &mut self, kind: u16, x: f32, y: f32, rot: f32, _client_id: ClientId, ) -> NetworkId
Spawns a new network-replicated entity of a specific kind for a specific client.
Sourcefn spawn_session_ship(
&mut self,
kind: u16,
x: f32,
y: f32,
rot: f32,
client_id: ClientId,
) -> NetworkId
fn spawn_session_ship( &mut self, kind: u16, x: f32, y: f32, rot: f32, client_id: ClientId, ) -> NetworkId
Spawns the authoritative session ship for a client and marks it as the possession target.
The default implementation delegates to spawn_kind_for. Adapters
that support a SessionShip marker component should override this to
attach that marker so the input pipeline can gate InputCommand
processing exclusively to the session ship.
Sourcefn clear_world(&mut self)
fn clear_world(&mut self)
Despawns all entities from the world.
Sourcefn queue_reliable_event(
&mut self,
_client_id: Option<ClientId>,
_event: GameEvent,
)
fn queue_reliable_event( &mut self, _client_id: Option<ClientId>, _event: GameEvent, )
Queues a reliable game event to be sent to a specific client (or all if None).
Sourcefn setup_world(&mut self)
fn setup_world(&mut self)
Setups the initial world state (e.g. Master Room).
Sourcefn get_entity_room(&self, _network_id: NetworkId) -> Option<NetworkId>
fn get_entity_room(&self, _network_id: NetworkId) -> Option<NetworkId>
Returns the Room network_id for a given entity, if any.
Sourcefn get_client_room(&self, _client_id: ClientId) -> Option<NetworkId>
fn get_client_room(&self, _client_id: ClientId) -> Option<NetworkId>
Returns the Room network_id for a client’s session ship, if any.