Skip to content

Commit

Permalink
feat: repeat action
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Nov 3, 2023
1 parent 5ae16ab commit 416c615
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions crates/gamai/src/common_selectors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod fallback_selector;
pub use self::fallback_selector::*;
pub mod repeat;
pub use self::repeat::*;
pub mod score_selector;
pub use self::score_selector::*;
pub mod sequence_selector;
Expand Down
20 changes: 20 additions & 0 deletions crates/gamai/src/common_selectors/repeat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::node::*;
use crate::*;
use bevy_ecs::prelude::*;

/// This action will ensure that all children always have the `Running` component
#[action]
pub fn repeat<N: AiNode>(
mut commands: Commands,
mut query: Query<
N::ChildQueryOptMut<Running>,
With<Prop<Running, N>>,
>,
) {
for running in query.iter_mut() {
let children = N::children_opt_mut(running);
for mut running in children {
running.set(&mut commands, Some(Running));
}
}
}
9 changes: 4 additions & 5 deletions crates/gamai/src/common_selectors/sequence_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ pub fn sequence<N: AiNode>(
>,
) {
for (entity, running, out) in query.iter_mut() {
let mut children = std::iter::zip(
N::children_opt_mut(running).into_iter(),
N::children_opt_mut(out).into_iter(),
)
.collect::<Vec<_>>();
let mut children = N::children_opt_mut(running)
.into_iter()
.zip(N::children_opt_mut(out).into_iter())
.collect::<Vec<_>>();

if children.iter().any(|(running, _)| running.get().is_some()) {
continue;
Expand Down
8 changes: 4 additions & 4 deletions crates/gamai/src/prop/child_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ impl<'a, T: IntoProp, N: AiNode> IntoChildPropOptMut<'a, T>
fn get(&self) -> Option<&T> { self.value.as_ref().map(|v| v.deref()) }
fn value(&'a mut self) -> &mut Option<Mut<'a, T>> { &mut self.value }
/// Sets child state to given value, with least effort possible.
/// If both are equal, do nothing
/// If current is None, use insert command
/// If next is None, use remove command
/// If both are Some, mutate self
/// If both are [None], do nothing
/// If current is [None], use insert command
/// If next is [None], use remove command
/// If both are [Some], mutate self
fn set(&mut self, commands: &mut Commands, next: Option<T>) {
match (self.value.as_mut(), next) {
(None, None) => {
Expand Down
3 changes: 2 additions & 1 deletion docs/src/gamai/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## TODO
- interrupts (recursive cleanup)
- ActionTimers
- single post_update system for a tree
- all cleanups etc in a single system

## main
- private action inners
Expand Down

0 comments on commit 416c615

Please sign in to comment.