Skip to content

Commit

Permalink
feat: trigger_in_duration
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 12, 2024
1 parent c55e905 commit 8eaf29f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 38 deletions.
4 changes: 2 additions & 2 deletions crates/beet_core/src/steer/forage_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn forage_behavior(world: &mut World) -> Entity {
Score::default(),
SetOnSpawn(Score::Weight(0.5)),
Wander::default(),
InsertInDuration::<RunResult>::default(),
TriggerInDuration::<OnRunResult>::default(),
));
parent
.spawn((
Expand All @@ -36,7 +36,7 @@ pub fn forage_behavior(world: &mut World) -> Entity {
parent.spawn((
Name::new("Wait 1 second"),
SetAgentOnRun(Velocity(Vec3::ZERO)),
InsertInDuration::<RunResult>::default(),
TriggerInDuration::<OnRunResult>::default(),
));
parent.spawn((
Name::new("Collect flower"),
Expand Down
4 changes: 2 additions & 2 deletions crates/beet_ecs/src/actions/on_trigger/on_global_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ mod test {
#[test]
fn works() -> Result<()> {
let mut app = App::new();
app.add_plugins((ActionPlugin::<
app.add_plugins(ActionPlugin::<
TriggerOnGlobalTrigger<OnRun, OnRunResult>,
>::default(),));
>::default());
let world = app.world_mut();
let func = observe_run_results(world);

Expand Down
4 changes: 2 additions & 2 deletions crates/beet_ecs/src/lifecycle/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub use self::empty_action::*;
pub mod insert_agent_on_run;
#[allow(unused_imports)]
pub use self::insert_agent_on_run::*;
pub mod insert_in_duration;
pub mod trigger_in_duration;
#[allow(unused_imports)]
pub use self::insert_in_duration::*;
pub use self::trigger_in_duration::*;
pub mod insert_on_run;
#[allow(unused_imports)]
pub use self::insert_on_run::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ use crate::prelude::*;
use bevy::prelude::*;
use std::time::Duration;

/// Inserts the given component after running for a given duration. Has no effect if
/// Triggers the given event after running for a given duration. Has no effect if
/// the action completes before the duration.
/// # Requires
/// - [`RunTimer`]
#[derive(Debug, Clone, Component, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(insert_in_duration::<T>.in_set(TickSet))]
pub struct InsertInDuration<T: GenericActionComponent> {
#[systems(trigger_in_duration::<T>.in_set(TickSet))]
pub struct TriggerInDuration<T: GenericActionEvent> {
pub duration: Duration,
pub value: T,
}

impl<T: Default + GenericActionComponent> Default for InsertInDuration<T> {
impl<T: Default + GenericActionEvent> Default for TriggerInDuration<T> {
fn default() -> Self { Self::new(T::default(), Duration::from_secs(1)) }
}

impl<T: GenericActionComponent> InsertInDuration<T> {
impl<T: GenericActionEvent> TriggerInDuration<T> {
pub fn new(value: T, duration: Duration) -> Self {
Self { value, duration }
}
Expand All @@ -37,15 +37,16 @@ impl<T: GenericActionComponent> InsertInDuration<T> {
}
}

pub fn insert_in_duration<T: GenericActionComponent>(
pub fn trigger_in_duration<T: GenericActionEvent>(
mut commands: Commands,
mut query: Query<(Entity, &RunTimer, &InsertInDuration<T>), With<Running>>,
mut query: Query<(Entity, &RunTimer, &TriggerInDuration<T>), With<Running>>,
) {
for (entity, timer, insert_in_duration) in query.iter_mut() {
println!("here!{:?}", timer.last_started.elapsed());
if timer.last_started.elapsed() >= insert_in_duration.duration {
commands
.entity(entity)
.insert(insert_in_duration.value.clone());
.trigger(insert_in_duration.value.clone());
}
}
}
Expand All @@ -62,30 +63,35 @@ mod test {
#[test]
fn works() -> Result<()> {
let mut app = App::new();
app.add_plugins((
LifecycleSystemsPlugin,
ActionPlugin::<InsertInDuration<RunResult>>::default(),
));

let on_result = observe_triggers::<OnRunResult>(app.world_mut());

app.add_plugins(ActionPlugin::<(
RunTimer,
TriggerInDuration<OnRunResult>,
)>::default());

app.configure_sets(Update, PreTickSet.before(TickSet));

app.insert_time();

let root = app
.world_mut()
.spawn((
Running,
RunTimer::default(),
InsertInDuration::<RunResult>::new(
RunResult::default(),
Duration::from_secs(1),
),
))
.id();
app.world_mut().spawn((
Running,
RunTimer::default(),
TriggerInDuration::<OnRunResult>::new(
OnRunResult::default(),
Duration::from_secs(2),
),
));

app.update_with_secs(1);

expect(&on_result).not().to_have_been_called()?;

expect(&app).to_have_component::<Running>(root)?;
app.update_with_secs(10);

app.update_with_secs(2);
expect(&on_result).to_have_been_called()?;

expect(&app).component(root)?.to_be(&RunResult::Success)?;
expect(&app).not().to_have_component::<Running>(root)?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion crates/beet_ecs/src/lifecycle/lifecycle_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Plugin for LifecyclePlugin {
app.add_plugins(LifecycleSystemsPlugin);

app.add_plugins(ActionPlugin::<(
InsertInDuration<RunResult>,
TriggerInDuration<OnRunResult>,
InsertOnRun<RunResult>,
RunTimer,
LogOnRun,
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_ecs/src/test/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn test_serde_entity(world: &mut World) -> Entity {
// utility
EmptyAction::default(),
Repeat::default(),
InsertInDuration::<RunResult>::default(),
TriggerInDuration::<OnRunResult>::default(),
// control flow actions
SequenceFlow::default(),
FallbackSelector::default(),
Expand Down
6 changes: 3 additions & 3 deletions crates/beet_examples/src/plugins/example_default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ fn assert_local_assets() {
if !path.exists() {
panic!(
r#"
🐝🐝🐝
πŸ₯πŸ₯πŸ₯
Howdy! Beet examples use large assets that are stored remotely.
Welcome! Beet examples use large assets that are stored remotely.
Windows:
Expand All @@ -93,7 +93,7 @@ curl -o ./assets.tar.gz https://storage.googleapis.com/beet-misc/assets.tar.gz
tar -xzvf ./assets.tar.gz
rm ./assets.tar.gz
🐝🐝🐝
πŸ₯πŸ₯πŸ₯
"#
);
}
Expand Down

0 comments on commit 8eaf29f

Please sign in to comment.