Skip to content

Commit

Permalink
wip: hello_net
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 3, 2024
1 parent 8900058 commit 5dbf0b0
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
71 changes: 71 additions & 0 deletions crates/beet_ecs/src/lifecycle/actions/insert_on_trigger.rs
Original file line number Diff line number Diff line change
@@ -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<E: GenericActionEvent, T: GenericActionComponent> {
pub value: T,
phantom: PhantomData<E>,
}
impl<E: GenericActionEvent, T: GenericActionComponent> Deref
for InsertOnTrigger<E, T>
{
type Target = T;
fn deref(&self) -> &Self::Target { &self.value }
}
impl<E: GenericActionEvent, T: GenericActionComponent> DerefMut
for InsertOnTrigger<E, T>
{
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.value }
}

impl<E: GenericActionEvent, T: Default + GenericActionComponent> Default
for InsertOnTrigger<E, T>
{
fn default() -> Self {
Self {
value: T::default(),
phantom: Default::default(),
}
}
}

impl<E: GenericActionEvent, T: GenericActionComponent> InsertOnTrigger<E, T> {
pub fn new(value: impl Into<T>) -> Self {
Self {
value: value.into(),
phantom: Default::default(),
}
}
}

impl<E: GenericActionEvent, T: GenericActionComponent> ActionMeta
for InsertOnTrigger<E, T>
{
fn category(&self) -> ActionCategory { ActionCategory::World }
}

impl<E: GenericActionEvent, T: GenericActionComponent> ActionSystems
for InsertOnTrigger<E, T>
{
fn systems() -> SystemConfigs { inset_on_trigger::<E, T>.in_set(TickSet) }
}

fn inset_on_trigger<E: GenericActionEvent, T: GenericActionComponent>(
mut commands: Commands,
mut reader: EventReader<E>,
query: Query<(Entity, &InsertOnTrigger<E, T>)>,
) {
for _ev in reader.read() {
log::info!("EVENT");
for (entity, trigger) in query.iter() {
log::info!("RECEIVED");
commands.entity(entity).insert(trigger.value.clone());
}
}
}
3 changes: 3 additions & 0 deletions crates/beet_ecs/src/lifecycle/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
17 changes: 17 additions & 0 deletions crates/beet_examples/examples/basics/export.rs
Original file line number Diff line number Diff line change
@@ -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();
}
6 changes: 3 additions & 3 deletions crates/beet_examples/examples/basics/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 6 additions & 2 deletions crates/beet_examples/examples/basics/scenes/hello_net.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use beet_examples::prelude::*;
use beet::prelude::*;
use beet_examples::prelude::*;
use bevy::prelude::*;

pub fn hello_net(mut commands: 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::<OnUserMessage, Running>::new(Running),
LogOnRun::new("Recv: Player Message"),
));
}
2 changes: 2 additions & 0 deletions crates/beet_examples/src/net/example_replicate_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ impl Plugin for ExampleReplicatePlugin {
app.add_plugins((ReplicatePlugin, CommonEventsPlugin))
.add_event::<OnUserMessage>()
.replicate_event_incoming::<OnUserMessage>()
.add_plugins(ActionPlugin::<InsertOnTrigger<OnUserMessage,Running>>::default())

.add_event::<AppLoaded>()
.replicate_event_outgoing::<AppLoaded>()
.add_plugins(ActionPlugin::<TriggerOnRun<AppLoaded>>::default());
Expand Down
5 changes: 4 additions & 1 deletion crates/beet_examples/src/plugins/example_plugin.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@
</head>

<body>
<script>
function sendUserMessage(message) {
const detail = JSON.stringify([{
SendEvent: {
reg_id: 0,//the first registered id, OnUserMessage
payload: {
Json: JSON.stringify(message)
}
}
}])
window.dispatchEvent(new CustomEvent('js-message', { detail }))
}

window.addEventListener('wasm-message', (event) => {
const messages = JSON.parse(event.detail)
for (const message of messages) {
console.log(`message received: ${JSON.stringify(message)}`)
if (message?.SendEvent?.payload?.Json) {
const payload = JSON.parse(message.SendEvent.payload.Json)
console.log(`payload received: ${JSON.stringify(payload)}`)
sendUserMessage('loud and clear')
}
}
})
</script>


<script type="module">
import init from './target/static/wasm/basics.js'
init()
Expand Down

0 comments on commit 5dbf0b0

Please sign in to comment.