From 5dbf0b09fed4bf14355c7a3fc11c79378b182121 Mon Sep 17 00:00:00 2001 From: Peter Hayman Date: Wed, 3 Jul 2024 13:47:37 +1000 Subject: [PATCH] wip: hello_net --- .vscode/settings.json | 2 +- Cargo.lock | 1 - Cargo.toml | 2 +- .../lifecycle/actions/insert_on_trigger.rs | 71 +++++++++++++++++++ crates/beet_ecs/src/lifecycle/actions/mod.rs | 3 + .../beet_examples/examples/basics/export.rs | 17 +++++ crates/beet_examples/examples/basics/main.rs | 6 +- .../examples/basics/scenes/hello_net.rs | 8 ++- .../src/net/example_replicate_plugin.rs | 2 + .../src/plugins/example_plugin.rs | 5 +- crates/beet_web/Cargo.toml | 2 +- index.html | 27 +++++++ 12 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 crates/beet_ecs/src/lifecycle/actions/insert_on_trigger.rs create mode 100644 crates/beet_examples/examples/basics/export.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 5f5d63a0..b0a805aa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -28,7 +28,7 @@ "rust-analyzer.files.excludeDirs": [ "crates/beet_esp" ], - // "rust-analyzer.cargo.target": "wasm32-unknown-unknown", + "rust-analyzer.cargo.target": "wasm32-unknown-unknown", "search.exclude": { "docs/deps/mermaid-init.js": true, "docs/deps/mermaid.min.js": true, diff --git a/Cargo.lock b/Cargo.lock index 19a32131..d227d464 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -726,7 +726,6 @@ dependencies = [ "futures", "js-sys", "log", - "parking_lot", "ron", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 524299b2..c86e649d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -155,7 +155,7 @@ strum_macros = "0.26.0" extend = "1.1.2" # channels -parking_lot = "0.12" # fast alternative std::sync::Mutex/RwLock +# parking_lot = {version = "0.12", features = ["wasm-bindgen"] } # fast alternative std::sync::Mutex/RwLock # flume = { version = "0.11", default-features = false, features = ["async"] } flume = "0.11" diff --git a/crates/beet_ecs/src/lifecycle/actions/insert_on_trigger.rs b/crates/beet_ecs/src/lifecycle/actions/insert_on_trigger.rs new file mode 100644 index 00000000..abfb03a7 --- /dev/null +++ b/crates/beet_ecs/src/lifecycle/actions/insert_on_trigger.rs @@ -0,0 +1,71 @@ +use crate::prelude::*; +use bevy::ecs::schedule::SystemConfigs; +use bevy::prelude::*; +use std::marker::PhantomData; +use std::ops::Deref; +use std::ops::DerefMut; + +/// Triggers the given event when this behavior starts Insertning. +#[derive(Debug, Clone, PartialEq, Component, Reflect)] +#[reflect(Component, ActionMeta)] +pub struct InsertOnTrigger { + pub value: T, + phantom: PhantomData, +} +impl Deref + for InsertOnTrigger +{ + type Target = T; + fn deref(&self) -> &Self::Target { &self.value } +} +impl DerefMut + for InsertOnTrigger +{ + fn deref_mut(&mut self) -> &mut Self::Target { &mut self.value } +} + +impl Default + for InsertOnTrigger +{ + fn default() -> Self { + Self { + value: T::default(), + phantom: Default::default(), + } + } +} + +impl InsertOnTrigger { + pub fn new(value: impl Into) -> Self { + Self { + value: value.into(), + phantom: Default::default(), + } + } +} + +impl ActionMeta + for InsertOnTrigger +{ + fn category(&self) -> ActionCategory { ActionCategory::World } +} + +impl ActionSystems + for InsertOnTrigger +{ + fn systems() -> SystemConfigs { inset_on_trigger::.in_set(TickSet) } +} + +fn inset_on_trigger( + mut commands: Commands, + mut reader: EventReader, + query: Query<(Entity, &InsertOnTrigger)>, +) { + for _ev in reader.read() { + log::info!("EVENT"); + for (entity, trigger) in query.iter() { + log::info!("RECEIVED"); + commands.entity(entity).insert(trigger.value.clone()); + } + } +} diff --git a/crates/beet_ecs/src/lifecycle/actions/mod.rs b/crates/beet_ecs/src/lifecycle/actions/mod.rs index 1d6fb9a4..989211d7 100644 --- a/crates/beet_ecs/src/lifecycle/actions/mod.rs +++ b/crates/beet_ecs/src/lifecycle/actions/mod.rs @@ -22,6 +22,9 @@ pub use self::remove_agent_on_run::*; pub mod repeat; #[allow(unused_imports)] pub use self::repeat::*; +pub mod insert_on_trigger; +#[allow(unused_imports)] +pub use self::insert_on_trigger::*; pub mod set_agent_on_run; #[allow(unused_imports)] pub use self::set_agent_on_run::*; diff --git a/crates/beet_examples/examples/basics/export.rs b/crates/beet_examples/examples/basics/export.rs new file mode 100644 index 00000000..1e7a9f6c --- /dev/null +++ b/crates/beet_examples/examples/basics/export.rs @@ -0,0 +1,17 @@ +use beet::prelude::*; +use beet_examples::prelude::*; +use bevy::prelude::*; +mod scenes; + +fn main() { + let mut app = App::new(); + + app.add_plugins(( + ExamplePluginText::default(), + DefaultBeetPlugins::default(), + )) + // .add_systems(Startup, scenes::hello_world) + .add_systems(Startup, scenes::hello_net) + // .add_systems(PostStartup, save_scene("target/scenes/hello_world.ron")) + .run(); +} \ No newline at end of file diff --git a/crates/beet_examples/examples/basics/main.rs b/crates/beet_examples/examples/basics/main.rs index b752f822..ea189622 100644 --- a/crates/beet_examples/examples/basics/main.rs +++ b/crates/beet_examples/examples/basics/main.rs @@ -12,8 +12,8 @@ fn main() { ExamplePluginText::default(), DefaultBeetPlugins::default(), )) - .add_systems(Startup, scenes::hello_world) - // .add_systems(Startup, scenes::hello_net) - .add_systems(PostStartup, save_scene("target/scenes/hello_world.ron")) + // .add_systems(Startup, scenes::hello_world) + .add_systems(Startup, scenes::hello_net) + // .add_systems(PostStartup, save_scene("target/scenes/hello_world.ron")) .run(); } diff --git a/crates/beet_examples/examples/basics/scenes/hello_net.rs b/crates/beet_examples/examples/basics/scenes/hello_net.rs index 38bcb8d6..bf452a52 100644 --- a/crates/beet_examples/examples/basics/scenes/hello_net.rs +++ b/crates/beet_examples/examples/basics/scenes/hello_net.rs @@ -1,5 +1,5 @@ -use beet_examples::prelude::*; use beet::prelude::*; +use beet_examples::prelude::*; use bevy::prelude::*; pub fn hello_net(mut commands: Commands) { @@ -7,8 +7,12 @@ pub fn hello_net(mut commands: Commands) { .spawn((SequenceSelector::default(), Running)) .with_children(|parent| { parent.spawn(( - LogOnRun("Message Sent: AppLoaded".into()), + LogOnRun::new("Send: AppLoaded"), TriggerOnRun(AppLoaded), )); }); + commands.spawn(( + InsertOnTrigger::::new(Running), + LogOnRun::new("Recv: Player Message"), + )); } diff --git a/crates/beet_examples/src/net/example_replicate_plugin.rs b/crates/beet_examples/src/net/example_replicate_plugin.rs index 321bea81..4bdfd648 100644 --- a/crates/beet_examples/src/net/example_replicate_plugin.rs +++ b/crates/beet_examples/src/net/example_replicate_plugin.rs @@ -10,6 +10,8 @@ impl Plugin for ExampleReplicatePlugin { app.add_plugins((ReplicatePlugin, CommonEventsPlugin)) .add_event::() .replicate_event_incoming::() + .add_plugins(ActionPlugin::>::default()) + .add_event::() .replicate_event_outgoing::() .add_plugins(ActionPlugin::>::default()); diff --git a/crates/beet_examples/src/plugins/example_plugin.rs b/crates/beet_examples/src/plugins/example_plugin.rs index 10fe80e1..74d8e69f 100644 --- a/crates/beet_examples/src/plugins/example_plugin.rs +++ b/crates/beet_examples/src/plugins/example_plugin.rs @@ -1,4 +1,6 @@ use crate::prelude::*; +#[cfg(any(target_arch = "wasm32", feature = "tokio"))] +use beet::prelude::*; use bevy::asset::AssetMetaCheck; use bevy::prelude::*; use forky_bevy::systems::close_on_esc; @@ -20,7 +22,8 @@ impl Plugin for ExamplePlugin { #[cfg(feature = "tokio")] app.add_transport(NativeWsClient::new(DEFAULT_SOCKET_URL).unwrap()); - app.add_plugins(ExampleReplicatePlugin) + app + .add_plugins(ExampleReplicatePlugin) .add_plugins( DefaultPlugins .set(WindowPlugin { diff --git a/crates/beet_web/Cargo.toml b/crates/beet_web/Cargo.toml index 8dbc89bb..9b6d03ba 100644 --- a/crates/beet_web/Cargo.toml +++ b/crates/beet_web/Cargo.toml @@ -31,7 +31,7 @@ futures.workspace = true bincode.workspace = true serde.workspace = true serde_json.workspace = true -parking_lot.workspace = true +# parking_lot.workspace = true ron.workspace = true bevy.workspace = true diff --git a/index.html b/index.html index b8a6ffd8..145e3bd5 100644 --- a/index.html +++ b/index.html @@ -19,6 +19,33 @@ + + +