Skip to content

Commit

Permalink
feat: seek3d
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 9, 2024
1 parent 7f2e24b commit 85839a7
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 23 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 @@ -31,7 +31,7 @@ pub fn forage_behavior(world: &mut World) -> Entity {
parent.spawn((
Name::new("Go to flower"),
Seek::default(),
SucceedOnArrive { radius: 0.1 },
StopOnArrive { radius: 0.1 },
));
parent.spawn((
Name::new("Wait 1 second"),
Expand Down
4 changes: 2 additions & 2 deletions crates/beet_core/src/steer/steer_actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub use self::seek::*;
pub mod separate;
#[allow(unused_imports)]
pub use self::separate::*;
pub mod succeed_on_arrive;
pub mod stop_on_arrive;
#[allow(unused_imports)]
pub use self::succeed_on_arrive::*;
pub use self::stop_on_arrive::*;
pub mod wander;
#[allow(unused_imports)]
pub use self::wander::*;
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@ use bevy::prelude::*;


/// Succeeds when the agent arrives at the [`SteerTarget`].
/// Fails if the target is not found.
#[derive(Debug, Clone, PartialEq, Action, Reflect)]
#[reflect(Default, Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(succeed_on_arrive.in_set(TickSet))]
pub struct SucceedOnArrive {
pub struct StopOnArrive {
pub radius: f32,
}

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

impl SucceedOnArrive {
impl StopOnArrive {
pub fn new(radius: f32) -> Self { Self { radius } }
}

pub fn succeed_on_arrive(
mut commands: Commands,
agents: Query<(&Transform, &SteerTarget)>,
transforms: Query<&Transform>,
mut query: Query<(Entity, &TargetAgent, &SucceedOnArrive), With<Running>>,
mut query: Query<(Entity, &TargetAgent, &StopOnArrive), With<Running>>,
) {
for (entity, agent, succeed_on_arrive) in query.iter_mut() {
if let Ok((transform, target)) = agents.get(**agent) {
if let Ok(target) = target.position(&transforms) {
if Vec3::distance(transform.translation, target)
<= succeed_on_arrive.radius
{
commands.entity(entity).insert(RunResult::Success);
commands.entity(entity).trigger(OnRunResult::success());
}
} else {
commands.entity(entity).insert(RunResult::Failure);
commands.entity(entity).trigger(OnRunResult::failure());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_core/src/steer/steer_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Plugin for SteerPlugin {
Separate<GroupSteerAgent>,
Align<GroupSteerAgent>,
Cohere<GroupSteerAgent>,
SucceedOnArrive,
StopOnArrive,
FindSteerTarget,
ScoreSteerTarget,
DespawnSteerTarget,
Expand Down
17 changes: 11 additions & 6 deletions crates/beet_ecs/src/lifecycle/actions/set_agent_on_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy::prelude::*;
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Agent)]
#[systems(set_agent_on_run::<T>.in_set(PostTickSet))]
#[observers(set_agent_on_run::<T>)]
pub struct SetAgentOnRun<T: GenericActionComponent>(pub T);

impl<T: GenericActionComponent> SetAgentOnRun<T> {
Expand All @@ -18,12 +18,17 @@ impl<T: Default + GenericActionComponent> Default for SetAgentOnRun<T> {
}

fn set_agent_on_run<T: GenericActionComponent>(
trigger: Trigger<OnRun>,
mut agents: Query<&mut T>,
mut query: Query<(&TargetAgent, &SetAgentOnRun<T>), Added<Running>>,
query: Query<(&TargetAgent, &SetAgentOnRun<T>)>,
) {
for (entity, src) in query.iter_mut() {
if let Ok(mut dst) = agents.get_mut(**entity) {
*dst = src.0.clone();
}
let (target, action) = query
.get(trigger.entity())
.expect(expect_action::NO_ACTION_COMP);

if let Ok(mut dst) = agents.get_mut(**target) {
*dst = action.0.clone();
} else {
log::warn!("SetAgentOnRun: Agent with component not found");
}
}
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 @@ -15,7 +15,6 @@ impl Plugin for LifecyclePlugin {
InsertOnRun<RunResult>,
LogOnRun,
// CallOnRun,
Repeat,
SetOnSpawn<Score>,
// selectors
FallbackSelector,
Expand All @@ -27,6 +26,7 @@ impl Plugin for LifecyclePlugin {
)>::default())
.add_plugins(ActionPlugin::<(
// ContinueRun,
Repeat,
InsertOnTrigger<OnRun, Running>,
RemoveOnTrigger<OnRunResult, Running>,
SequenceFlow,
Expand Down
4 changes: 2 additions & 2 deletions crates/beet_examples/src/scenes/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn fetch_npc(mut commands: Commands) {
Name::new("Go To Item"),
TargetAgent(agent),
Seek,
SucceedOnArrive::new(1.),
StopOnArrive::new(1.),
));
parent.spawn((
Name::new("Pick Up Item"),
Expand All @@ -95,7 +95,7 @@ pub fn fetch_npc(mut commands: Commands) {
// Name::new("Return Item To Center"),
// TargetAgent(agent),
// Seek,
// SucceedOnArrive::new(6.),
// StopOnArrive::new(6.),
// ));
});
});
Expand Down
8 changes: 5 additions & 3 deletions crates/beet_examples/src/scenes/seek_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ pub fn seek_3d(mut commands: Commands) {
parent
.spawn((
Name::new("Seek Behavior"),
Running,
SequenceSelector,
RunOnSpawn,
SequenceFlow,
Repeat::default(),
))
.with_children(|parent| {
parent.spawn((
Name::new("Idle"),
ContinueRun::default(),
TargetAgent(agent),
SetAgentOnRun(Velocity::default()),
PlayAnimation::new(idle_index)
Expand All @@ -70,12 +71,13 @@ pub fn seek_3d(mut commands: Commands) {
));
parent.spawn((
Name::new("Seek"),
ContinueRun::default(),
TargetAgent(agent),
Seek,
PlayAnimation::new(walk_index)
.repeat_forever()
.with_transition_duration(transition_duration),
SucceedOnArrive::new(6.),
StopOnArrive::new(6.),
));
});
});
Expand Down
2 changes: 1 addition & 1 deletion crates/beet_web/test/steering/despawn_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn works() -> Result<()> {
.child((
Seek::default(),
FindSteerTarget::new("flower", 10.),
SucceedOnArrive { radius: 0.1 },
StopOnArrive { radius: 0.1 },
))
.child((
InsertOnRun(RunResult::Success),
Expand Down
1 change: 1 addition & 0 deletions examples/seek_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn main() {
scenes::lighting_3d,
scenes::ground_3d,
scenes::seek_3d,
scenes::ui_terminal,
),
)
.run();
Expand Down

0 comments on commit 85839a7

Please sign in to comment.