pub mod prelude { pub(crate) use bevy_app::prelude::*; pub(crate) use bevy_ecs::{prelude::*, system::EntityCommands}; pub(crate) use bevy_replicon::prelude::*; pub use bevy_replicon_spawn_macros::SpawnContext; pub use crate::{context::SpawnContext, RepliconSpawnAppExt, SpawnEvent}; } use crate::prelude::*; mod context; pub trait RepliconSpawnAppExt { fn replicate_spawn(&mut self, spawn: fn(EntityCommands, &T)) -> &mut Self where T: Component + Clone; } impl RepliconSpawnAppExt for App { fn replicate_spawn(&mut self, spawn: fn(EntityCommands, &T)) -> &mut Self where T: Component + Clone, { self.add_event::>(); self.insert_resource(SpawnEventResource:: { spawn }); self.add_systems(Update, system::); self } } fn system( mut commands: Commands, resource: Res>, query: Query<(Entity, &T), (Added, Added)>, mut events: EventWriter>, ) { for (entity, event) in &query { (resource.spawn)(commands.entity(entity), event); events.send(SpawnEvent { entity, data: event.clone(), }); } } #[derive(Resource)] struct SpawnEventResource { pub spawn: fn(EntityCommands, &T), } #[derive(Event)] pub struct SpawnEvent { pub entity: Entity, pub data: T, }