Skip to content

Commit

Permalink
wip: TriggerOnRun
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 3, 2024
1 parent 232f074 commit a568552
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 39 deletions.
27 changes: 0 additions & 27 deletions crates/beet_ecs/src/lifecycle/actions/call_on_run.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/beet_ecs/src/lifecycle/actions/insert_on_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;


/// Inserts the given component when this behavior starts running.
#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Component, Reflect)]
#[reflect(Component, ActionMeta)]
pub struct InsertOnRun<T: GenericActionComponent>(pub T);
Expand Down
14 changes: 12 additions & 2 deletions crates/beet_ecs/src/lifecycle/actions/lifecycle_actions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
use bevy::prelude::*;
use bevy::reflect::GetTypeRegistration;

/// Minimal traits generally required for an action component.
pub trait GenericActionComponent:
Clone + Component + FromReflect + GetTypeRegistration
Clone + Component + FromReflect + GetTypeRegistration
{
}
impl<T: Clone + Component + FromReflect + GetTypeRegistration>
GenericActionComponent for T
GenericActionComponent for T
{
}
/// Minimal traits generally required for an action event.
pub trait GenericActionEvent:
Clone + Event + FromReflect + GetTypeRegistration
{
}
impl<T: Clone + Event + FromReflect + GetTypeRegistration>
GenericActionEvent for T
{
}
6 changes: 3 additions & 3 deletions crates/beet_ecs/src/lifecycle/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub mod call_on_run;
#[allow(unused_imports)]
pub use self::call_on_run::*;
pub mod empty_action;
#[allow(unused_imports)]
pub use self::empty_action::*;
Expand Down Expand Up @@ -34,3 +31,6 @@ pub use self::set_on_run::*;
pub mod set_on_spawn;
#[allow(unused_imports)]
pub use self::set_on_spawn::*;
pub mod trigger_on_run;
#[allow(unused_imports)]
pub use self::trigger_on_run::*;
33 changes: 33 additions & 0 deletions crates/beet_ecs/src/lifecycle/actions/trigger_on_run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Triggers the given event when this behavior starts running.
#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Component, Reflect)]
#[reflect(Component, ActionMeta)]
pub struct TriggerOnRun<T: GenericActionEvent>(pub T);

impl<T: Default + GenericActionEvent> Default for TriggerOnRun<T> {
fn default() -> Self { Self(T::default()) }
}

impl<T: GenericActionEvent> TriggerOnRun<T> {
pub fn new(value: impl Into<T>) -> Self { Self(value.into()) }
}

impl<T: GenericActionEvent> ActionMeta for TriggerOnRun<T> {
fn category(&self) -> ActionCategory { ActionCategory::World }
}

impl<T: GenericActionEvent> ActionSystems for TriggerOnRun<T> {
fn systems() -> SystemConfigs { trigger_on_run::<T>.in_set(TickSet) }
}

fn trigger_on_run<T: GenericActionEvent>(
mut writer: EventWriter<T>,
query: Query<&TriggerOnRun<T>, Added<Running>>,
) {
for trigger in query.iter() {
writer.send(trigger.0.clone());
}
}
14 changes: 12 additions & 2 deletions crates/beet_examples/src/net/example_replicate_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ impl Plugin for ExampleReplicatePlugin {
fn build(&self, app: &mut App) {
app.add_plugins((ReplicatePlugin, CommonEventsPlugin))
.add_event::<OnUserMessage>()
.replicate_event_incoming::<OnUserMessage>();
.replicate_event_incoming::<OnUserMessage>()
.add_event::<AppLoaded>()
.replicate_event_outgoing::<AppLoaded>()
.add_plugins(ActionPlugin::<TriggerOnRun<AppLoaded>>::default());
}
}


#[derive(Event, Deref, DerefMut, Serialize, Deserialize)]
/// 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;
2 changes: 2 additions & 0 deletions crates/beet_examples/src/wasm/postmessage_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use wasm_bindgen::JsCast;
use web_sys::window;
use web_sys::MessageEvent;


#[deprecated("use beet_net instead")]
pub struct PostmessageInputPlugin;

impl Plugin for PostmessageInputPlugin {
Expand Down
5 changes: 1 addition & 4 deletions examples/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ fn main() {
(set_player_sentence, rotate_items, ready_on_bert_load),
);

#[cfg(target_arch = "wasm32")]
app.add_plugins(PostmessageInputPlugin);

app.run();
}

Expand Down Expand Up @@ -113,7 +110,7 @@ fn setup_fox(
parent
.spawn((
Name::new("Idle Or Fetch"),
CallOnRun::new(beet_finished_loading),
TriggerOnRun(AppLoaded),
TargetAgent(agent),
ScoreSelector::default(),
// ScoreSelector::consuming(),
Expand Down

0 comments on commit a568552

Please sign in to comment.