use bevy::{ecs::component::ComponentId, prelude::*};
use crate::{prelude::*, shared::server_entity_map::ServerEntityMap};
#[non_exhaustive]
pub struct SerializeCtx<'a> {
pub component_id: ComponentId,
pub server_tick: RepliconTick,
pub type_registry: &'a AppTypeRegistry,
}
#[non_exhaustive]
pub struct WriteCtx<'a, 'w> {
pub entity_map: &'a mut ServerEntityMap,
pub type_registry: &'a AppTypeRegistry,
pub component_id: ComponentId,
pub message_tick: RepliconTick,
pub(crate) spawner: &'a mut EntitySpawner<'w>,
pub(crate) ignore_mapping: bool,
}
impl EntityMapper for WriteCtx<'_, '_> {
fn get_mapped(&mut self, server_entity: Entity) -> Entity {
if self.ignore_mapping {
return server_entity;
}
self.entity_map
.server_entry(server_entity)
.or_insert_with(|| self.spawner.spawn_empty())
}
fn set_mapped(&mut self, _source: Entity, _target: Entity) {
unimplemented!()
}
}
#[non_exhaustive]
pub struct RemoveCtx {
pub component_id: ComponentId,
pub message_tick: RepliconTick,
}
#[non_exhaustive]
pub struct DespawnCtx {
pub message_tick: RepliconTick,
}
pub(crate) struct EntitySpawner<'a> {
world: &'a mut World,
}
impl<'a> EntitySpawner<'a> {
pub(crate) fn new(world: &'a mut World) -> Self {
Self { world }
}
fn spawn_empty(&mut self) -> Entity {
self.world.spawn_empty().id()
}
}