Skip to content

Commit

Permalink
feat: hello_net
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 3, 2024
1 parent 5dbf0b0 commit ed280e0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions crates/beet_examples/examples/basics/scenes/hello_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub fn hello_net(mut commands: Commands) {
.spawn((SequenceSelector::default(), Running))
.with_children(|parent| {
parent.spawn((
LogOnRun::new("Send: AppLoaded"),
TriggerOnRun(AppLoaded),
LogOnRun::new("Send: AppReady"),
TriggerOnRun(AppReady),
));
});
commands.spawn((
Expand Down
11 changes: 1 addition & 10 deletions crates/beet_examples/src/net/example_replicate_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ 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());
.add_plugins(ActionPlugin::<InsertOnTrigger<OnUserMessage,Running>>::default());
}
}


/// User messages received either internally or externally
#[derive(
Debug, Clone, Deref, DerefMut, Serialize, Deserialize, Event, Reflect,
)]
pub struct OnUserMessage(pub String);

/// Sent from this bevy app to web ui etc to notify that assets etc have loaded.
#[derive(Debug, Clone, Serialize, Deserialize, Event, Reflect)]
pub struct AppLoaded;
8 changes: 5 additions & 3 deletions crates/beet_net/src/replication/common_events.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use super::AppExtReplicate;
use beet_ecs::prelude::*;
use bevy::prelude::*;
use serde::Deserialize;
use serde::Serialize;


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

pub struct CommonEventsPlugin;

impl Plugin for CommonEventsPlugin {
fn build(&self, app: &mut App) {
app.add_event::<AppReady>()
.replicate_event_outgoing::<AppReady>();
.replicate_event_outgoing::<AppReady>()
.add_plugins(ActionPlugin::<TriggerOnRun<AppReady>>::default());
// .add_systems(Startup, ready);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn setup_fox(
parent
.spawn((
Name::new("Idle Or Fetch"),
TriggerOnRun(AppLoaded),
TriggerOnRun(AppReady),
TargetAgent(agent),
ScoreSelector::default(),
// ScoreSelector::consuming(),
Expand Down
10 changes: 9 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@

<body>
<script>

const regIds = {
// keep in sync with common_events.rs
appReady: 0,
// keep in sync with example_replicate_plugin.rs
onUserMessage: 1
}

function sendUserMessage(message) {
const detail = JSON.stringify([{
SendEvent: {
reg_id: 0,//the first registered id, OnUserMessage
reg_id: regIds.onUserMessage,
payload: {
Json: JSON.stringify(message)
}
Expand Down

0 comments on commit ed280e0

Please sign in to comment.