Skip to content

Commit

Permalink
feat: simplify fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 12, 2024
1 parent 3863728 commit 0f88856
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 44 deletions.
2 changes: 1 addition & 1 deletion crates/beet_core/src/steer/forage_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn forage_behavior(world: &mut World) -> Entity {
.spawn((
Name::new("Seek"),
Score::default(),
SteerTargetScoreProvider::new(awareness_radius),
SteerTargetScoreProvider::default(),
SequenceSelector::default(),
))
.with_children(|parent| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ use bevy::prelude::*;
#[observers(provide_score)]
/// Provides a [`ScoreValue`] based on distance to the [`SteerTarget`]
pub struct SteerTargetScoreProvider {
pub radius: f32,
/// fail if already at location
pub min_radius: f32,
pub max_radius: f32,
}

impl Default for SteerTargetScoreProvider {
fn default() -> Self { Self { radius: 0.5 } }
}

impl SteerTargetScoreProvider {
pub fn new(radius: f32) -> Self { Self { radius } }
fn default() -> Self {
Self {
min_radius: 1.,
max_radius: 10.,
}
}
}

fn provide_score(
Expand All @@ -32,9 +35,13 @@ fn provide_score(

let score = if let Ok((transform, target)) = agents.get(**agent)
&& let Ok(target) = target.position(&transforms)
&& Vec3::distance(transform.translation, target) <= action.radius
{
1.
let dist = Vec3::distance(transform.translation, target);
if dist >= action.min_radius && dist <= action.max_radius {
1.
} else {
0.
}
} else {
0.
};
Expand Down
1 change: 1 addition & 0 deletions crates/beet_ecs/src/actions/global/end_continued_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn end_continued_run(
running: Query<Entity, With<Running>>,
) {
if let Some(entity) = running.get(trigger.entity()).ok() {
// log::info!("end_continued_run: {entity}");
commands.entity(entity).remove::<Running>();
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/beet_ecs/src/lifecycle/actions/set_agent_on_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ fn set_agent_on_run<T: GenericActionComponent>(

if let Ok(mut dst) = agents.get_mut(**target) {
*dst = action.0.clone();
log::info!("setting agent value");
} else {
log::warn!("SetAgentOnRun: Agent with component not found");
}
Expand Down
4 changes: 3 additions & 1 deletion crates/beet_examples/src/plugins/example_plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ impl Plugin for ExampleMlPlugin {
InsertOnAssetEvent<RunResult, Bert>,
InsertSentenceSteerTarget<Collectable>,
RemoveOnTrigger<OnRunResult, Sentence>,
RemoveOnTrigger<OnRun, SteerTarget>,
RemoveOnTrigger<OnRunResult, SteerTarget>,
InsertOnTrigger<OnRun,Velocity>,
RemoveOnTrigger<OnRunResult,Velocity>,
)>::default())
/*-*/;
}
Expand Down
53 changes: 20 additions & 33 deletions crates/beet_examples/src/scenes/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,34 @@ pub fn fetch_npc(mut commands: Commands) {
RunOnSteerTargetRemove::default().with_source(agent),
ScoreFlow::default(),
RemoveOnTrigger::<OnRunResult, Sentence>::default(),
// RunOnSentenceChange::default(),
))
.with_children(|parent| {
parent.spawn((
Name::new("Idle"),
ScoreProvider::NEUTRAL,
TargetAgent(agent),
SetAgentOnRun(Velocity::default()),
// SetAgentOnRun(Velocity::default()),
PlayAnimation::new(idle_index).repeat_forever(),
));
parent
.spawn((
Name::new("Fetch"),
Score::default(),
TargetAgent(agent),
SteerTargetScoreProvider::new(10.),
PlayAnimation::new(walk_index).repeat_forever(),
SequenceFlow,
))
.with_children(|parent| {
parent.spawn((
Name::new("Go To Item"),
TargetAgent(agent),
Seek,
ContinueRun::default(),
EndOnArrive::new(1.),
));
#[rustfmt::skip]
parent.spawn((
Name::new("Pick Up Item"),
RemoveOnTrigger::<OnRun, SteerTarget>::default()
.with_target(agent),
EndOnRun::success(),
));
// parent.spawn((
// Name::new("Return Item To Center"),
// TargetAgent(agent),
// Seek,
// StopOnArrive::new(6.),
// ));
});
parent.spawn((
Name::new("Fetch"),
Score::default(),
TargetAgent(agent),
SteerTargetScoreProvider {
min_radius: 1.,
max_radius: 10.,
},
PlayAnimation::new(walk_index).repeat_forever(),
InsertOnTrigger::<OnRun, Velocity>::default()
.with_target(agent),
ContinueRun::default(),
Seek,
EndOnArrive::new(1.),
RemoveOnTrigger::<OnRunResult, SteerTarget>::default()
.with_target(agent),
RemoveOnTrigger::<OnRunResult, Velocity>::default()
.with_target(agent),
));
});
});
}
Expand Down

0 comments on commit 0f88856

Please sign in to comment.