Skip to content

Commit

Permalink
wip: fetch with observers
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 10, 2024
1 parent f0878de commit ba633c6
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 12 deletions.
3 changes: 3 additions & 0 deletions crates/beet_ecs/src/actions/flow/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod score_flow;
#[allow(unused_imports)]
pub use self::score_flow::*;
pub mod sequence_flow;
#[allow(unused_imports)]
pub use self::sequence_flow::*;
77 changes: 77 additions & 0 deletions crates/beet_ecs/src/actions/flow/score_flow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::prelude::*;
use bevy::prelude::*;
use std::cmp::Ordering;

#[derive(Default, Action, Reflect)]
#[reflect(Default, Component)]
#[category(ActionCategory::ChildBehaviors)]
#[observers(on_start, passthrough_run_result)]
pub struct ScoreFlow;

fn on_start(
trigger: Trigger<OnRun>,
mut commands: Commands,
query: Query<&Children>,
scores: Query<&Score>,
) {
let children = query
.get(trigger.entity())
.expect(child_expect::NO_CHILDREN);

if let Some(highest) = get_highest(scores, children) {
commands.trigger_targets(OnRun, highest);
} else {
commands.trigger_targets(OnRunResult::success(), trigger.entity());
}
}

fn get_highest(scores: Query<&Score>, children: &Children) -> Option<Entity> {
children
.iter()
.filter_map(|&child| scores.get(child).ok().map(|score| (child, score)))
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(Ordering::Equal))
.map(|(entity, _)| entity)
}


#[cfg(test)]
mod test {
use crate::prelude::*;
use anyhow::Result;
use bevy::prelude::*;
use sweet::*;

#[test]
fn works() -> Result<()> {
let mut world = World::new();
world.observe(bubble_run_result);

let on_result = observe_trigger_names::<OnRunResult>(&mut world);
let on_run = observe_triggers::<OnRun>(&mut world);

world
.spawn((Name::new("root"), ScoreFlow))
.with_children(|parent| {
parent.spawn((
Name::new("child1"),
Score::neutral(),
EndOnRun::success(),
));
parent.spawn((
Name::new("child2"),
Score::Pass,
EndOnRun::success(),
));
})
.flush_trigger(OnRun);

expect(&on_run).to_have_been_called_times(2)?;
expect(&on_result).to_have_been_called_times(2)?;
expect(&on_result)
.to_have_returned_nth_with(0, &"child2".to_string())?;
expect(&on_result)
.to_have_returned_nth_with(1, &"root".to_string())?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions crates/beet_ecs/src/actions/global/bubble_run_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub fn bubble_run_result(
}
}


/// Add this to flow actions to pass the run result to the parent.
pub fn passthrough_run_result(
trigger: Trigger<OnChildResult>,
mut commands: Commands,
Expand Down
13 changes: 4 additions & 9 deletions crates/beet_examples/src/scenes/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::beet::prelude::*;
use crate::prelude::*;
use bevy::prelude::*;
use std::time::Duration;


pub fn fetch_npc(mut commands: Commands) {
let Foxie {
Expand Down Expand Up @@ -35,9 +35,8 @@ pub fn fetch_npc(mut commands: Commands) {
.spawn((
Name::new("Idle Or Fetch"),
TargetAgent(agent),
ScoreSelector::default(),
ScoreFlow::default(),
AssetRunOnReady::<Bert>::new("default-bert.ron"),

RunOnSentenceChange::default(),
SetSentenceOnUserInput::default(),
FindSentenceSteerTarget::<Collectable>::default(),
Expand All @@ -49,11 +48,7 @@ pub fn fetch_npc(mut commands: Commands) {
TargetAgent(agent),
SetAgentOnRun(Velocity::default()),
PlayAnimation::new(idle_index).repeat_forever(),
RunTimer::default(),
InsertInDuration::new(
RunResult::Success,
Duration::from_secs(1),
),
ContinueRun::default(), // never end
));
parent
.spawn((
Expand All @@ -69,8 +64,8 @@ pub fn fetch_npc(mut commands: Commands) {
parent.spawn((
Name::new("Go To Item"),
TargetAgent(agent),
ContinueRun::default(),
Seek,
ContinueRun::default(),
EndOnArrive::new(1.),
));
parent.spawn((
Expand Down
3 changes: 3 additions & 0 deletions crates/beet_examples/src/serde_utils/ready_on_asset_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub fn ready_on_asset_load<A: Asset>(
}
_ => {}
}
for asset in all_blocks.iter() {
log::info!("remaining {}", asset);
}
}
let total_blocks = all_blocks.iter().count();
if total_blocks > 0 && total_blocks == total_ready {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn find_sentence_steer_target<T: GenericActionComponent>(
mut commands: Commands,
query: Query<(
&TargetAgent,
&Sentence,
Option<&Sentence>,
&Handle<Bert>,
&FindSentenceSteerTarget<T>,
)>,
Expand All @@ -50,6 +50,10 @@ fn find_sentence_steer_target<T: GenericActionComponent>(
log::warn!("{}", expect_asset::NOT_READY);
return;
};
let Some(target_sentence) = target_sentence else {
log::warn!("{}", "sentence not set yet?");
return;
};

match bert.closest_sentence_entity(
target_sentence.0.clone(),
Expand Down
4 changes: 2 additions & 2 deletions examples/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use bevy::prelude::*;

pub fn main() {
App::new()
.add_plugins(ExamplePluginBasics)
.add_plugins(ExamplePluginFull)
.add_systems(
Startup,
(
// scenes::beet_debug,
scenes::beet_debug,
scenes::ui_terminal_input,
scenes::lighting_3d,
scenes::ground_3d,
Expand Down

0 comments on commit ba633c6

Please sign in to comment.