Skip to content

Commit

Permalink
feat: on_spawn_scene
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 3, 2024
1 parent acfca97 commit 406f4d7
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
use super::AppExtReplicate;
use beet_ecs::prelude::*;
use bevy::prelude::*;
use serde::Deserialize;
use serde::Serialize;
use crate::prelude::*;


/// Sent from this app, usually once assets are ready.
#[derive(Debug, Clone, Serialize, Deserialize, Event, Reflect)]
pub struct AppReady;

/**
* This adds common replication events to the app.
* It should be added before any other events in order
* to preserve the registration ids:
*
* - `0`: AppReady
* - `1`: SpawnScene
*/
pub struct CommonEventsPlugin;

impl Plugin for CommonEventsPlugin {
fn build(&self, app: &mut App) {
app.add_event::<AppReady>()
.replicate_event_outgoing::<AppReady>()
.add_plugins(ActionPlugin::<TriggerOnRun<AppReady>>::default());
.add_plugins(ActionPlugin::<TriggerOnRun<AppReady>>::default())
.add_event::<OnSpawnScene>()
.replicate_event_incoming::<OnSpawnScene>()
;
// .add_systems(Startup, ready);
}
}


// fn ready(mut events: EventWriter<AppReady>) { events.send(AppReady); }

/// Sent from this app, usually once assets are ready.
#[derive(Debug, Clone, Serialize, Deserialize, Event, Reflect)]
pub struct AppReady;
6 changes: 6 additions & 0 deletions crates/beet_net/src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod common_events;
#[allow(unused_imports)]
pub use self::common_events::*;
pub mod on_spawn_scene;
#[allow(unused_imports)]
pub use self::on_spawn_scene::*;
93 changes: 93 additions & 0 deletions crates/beet_net/src/events/on_spawn_scene.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use anyhow::Result;
use bevy::ecs::system::SystemState;
use bevy::prelude::*;
use bevy::scene::serde::SceneDeserializer;
use forky_core::ResultTEExt;
use serde::de::DeserializeSeed;
use serde::Deserialize;
use serde::Serialize;
/// Received by this app, containing the raw text of a ron file for
/// deserialization and spawning
#[derive(Debug, Clone, Serialize, Deserialize, Event, Reflect)]
pub struct OnSpawnScene(pub String);

pub fn handle_spawn_scene(
world: &mut World,
events: &mut SystemState<EventReader<OnSpawnScene>>,
) {
events
.get_mut(world)
.read()
.map(|e| e.0.clone())
.collect::<Vec<_>>()
.into_iter()
.map(|scene| spawn(&scene, world))
.collect::<Result<Vec<_>>>()
.ok_or(|e| log::error!("{e}"));
}

fn spawn(ron_str: &str, world: &mut World) -> Result<()> {
let type_registry = world.resource::<AppTypeRegistry>().clone();
let mut deserializer =
bevy::scene::ron::de::Deserializer::from_str(ron_str)?;
let scene_deserializer = SceneDeserializer {
type_registry: &type_registry.read(),
};
let scene = scene_deserializer
.deserialize(&mut deserializer)
.map_err(|e| deserializer.span_error(e))?;
let mut entity_map = Default::default();
scene.write_to_world(world, &mut entity_map)?;
Ok(())
}


#[cfg(test)]
mod test {
use crate::prelude::*;
use anyhow::Result;
use bevy::log::LogPlugin;
use bevy::prelude::*;
use sweet::*;

#[derive(Debug, Component, Reflect, PartialEq)]
#[reflect(Component)]
struct MyStruct(pub u32);


#[test]
fn works() -> Result<()> {
let mut app = App::new();
app.register_type::<MyStruct>();

app.world_mut().spawn(MyStruct(7));
let scene = DynamicScene::from_world(app.world());
let str = scene
.serialize(&app.world().resource::<AppTypeRegistry>().read())?;

let mut app2 = App::new();

app2.add_plugins((LogPlugin::default(), ReplicatePlugin, CommonEventsPlugin))
.add_systems(Update, handle_spawn_scene).register_type::<MyStruct>();

app2.world_mut().send_event(OnSpawnScene(str.into()));

expect(
app2.world_mut()
.query::<&MyStruct>()
.iter(app2.world())
.count(),
)
.to_be(0)?;
app2.update();
expect(
app2.world_mut()
.query::<&MyStruct>()
.iter(app2.world())
.next(),
)
.to_be(Some(&MyStruct(7)))?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions crates/beet_net/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(let_chains)]
pub mod events;
pub mod extensions;
pub mod networking;
pub mod replication;
Expand All @@ -8,6 +9,7 @@ pub mod tokio_client;
pub mod web_transport;

pub mod prelude {
pub use crate::events::*;
pub use crate::extensions::*;
pub use crate::networking::*;
pub use crate::replication::*;
Expand Down
3 changes: 0 additions & 3 deletions crates/beet_net/src/replication/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub mod common_events;
#[allow(unused_imports)]
pub use self::common_events::*;
pub mod incoming;
#[allow(unused_imports)]
pub use self::incoming::*;
Expand Down
12 changes: 1 addition & 11 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test-ecs *args:

test-ml *args:
just watch 'cargo test -p beet_ml --lib -- {{args}}'

test-net *args:
just watch 'cargo test -p beet_net --lib -- {{args}}'

Expand Down Expand Up @@ -126,17 +127,6 @@ publish-all:
just publish beet_ecs
just publish beet

test crate *args:
cargo run -p {{crate}} --example test_{{crate}} -- {{args}}

test-w crate *args:
just watch 'cargo run -p {{crate}} --example test_{{crate}} -- -w {{args}}'

test-ci *args:
cargo run -p beet_core --example test_beet_core -- {{args}}
cargo run -p beet_ecs --example test_beet_ecs -- {{args}}
cargo run -p beet_net --example test_beet_net -- {{args}}

test-wasm crate *args:
sweet -p {{crate}} --example test_{{crate}} --interactive --watch {{args}}

Expand Down

0 comments on commit 406f4d7

Please sign in to comment.