Skip to content

Commit

Permalink
feat: beet_ecs - use action macro
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Jul 8, 2024
1 parent 9938ef5 commit 12832ba
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 189 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// "files.autoSave": "off",
"files.autoSaveDelay": 2000,
// "files.autoSaveDelay": 2000,
"files.exclude": {
"**/.git": true,
// ".vscode": true,
Expand Down
1 change: 0 additions & 1 deletion crates/beet_ecs/src/action/beet_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::prelude::*;
use bevy::ecs::intern::Interned;
use bevy::ecs::schedule::ScheduleLabel;
use bevy::prelude::*;
Expand Down
4 changes: 1 addition & 3 deletions crates/beet_ecs/src/actions/flow/sequence_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy::prelude::*;

#[derive(Default, Action, Reflect)]
#[reflect(Default, Component)]
#[category(ActionCategory::ChildBehaviors)]
#[observers(sequence_start, sequence_next)]
pub struct SequenceFlow;

Expand Down Expand Up @@ -39,9 +40,6 @@ fn sequence_next(
}
}

impl ActionMeta for SequenceFlow {
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

#[cfg(test)]
mod test {
Expand Down
15 changes: 3 additions & 12 deletions crates/beet_ecs/src/lifecycle/actions/empty_action.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
use crate::prelude::*;
use bevy::ecs::schedule::IntoSystemConfigs;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Does what it says on the tin, useful for tests etc
#[derive(Debug, Default, Clone, PartialEq, Component, Reflect)]
#[derive(Debug, Default, Clone, PartialEq, Action, Reflect)]
#[reflect(Default, Component, ActionMeta)]
pub struct EmptyAction;

impl ActionMeta for EmptyAction {
fn category(&self) -> ActionCategory { ActionCategory::World }
}

impl ActionSystems for EmptyAction {
fn systems() -> SystemConfigs { (|| {}).into_configs() }
}
#[category(ActionCategory::Behavior)]
pub struct EmptyAction;
15 changes: 3 additions & 12 deletions crates/beet_ecs/src/lifecycle/actions/insert_agent_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Inserts a component on the agent when this behavior starts running.
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Component, Reflect)]
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Agent)]
#[systems(set_agent_on_run::<T>.in_set(PostTickSet))]
pub struct InsertAgentOnRun<T: GenericActionComponent>(pub T);

impl<T: Default + GenericActionComponent> Default for InsertAgentOnRun<T> {
Expand All @@ -15,16 +16,6 @@ impl<T: GenericActionComponent> InsertAgentOnRun<T> {
pub fn new(value: impl Into<T>) -> Self { Self(value.into()) }
}


impl<T: GenericActionComponent> ActionMeta for InsertAgentOnRun<T> {
fn category(&self) -> ActionCategory { ActionCategory::Agent }
}

impl<T: GenericActionComponent> ActionSystems for InsertAgentOnRun<T> {
fn systems() -> SystemConfigs { set_agent_on_run::<T>.in_set(PostTickSet) }
}


fn set_agent_on_run<T: GenericActionComponent>(
mut commands: Commands,
mut query: Query<(&TargetAgent, &InsertAgentOnRun<T>), Added<Running>>,
Expand Down
16 changes: 4 additions & 12 deletions crates/beet_ecs/src/lifecycle/actions/insert_in_duration.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;
use std::time::Duration;

#[derive(Debug, Clone, Component, Reflect)]
#[reflect(Component, ActionMeta)]
/// Inserts the given component after running for a given duration. Has no effect if
/// the action completes before the duration.
/// # Requires
/// - [`RunTimer`]
#[derive(Debug, Clone, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(insert_in_duration::<T>.in_set(TickSet))]
pub struct InsertInDuration<T: GenericActionComponent> {
pub duration: Duration,
pub value: T,
Expand All @@ -18,15 +19,6 @@ impl<T: Default + GenericActionComponent> Default for InsertInDuration<T> {
fn default() -> Self { Self::new(T::default(), Duration::from_secs(1)) }
}


impl<T: GenericActionComponent> ActionMeta for InsertInDuration<T> {
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

impl<T: GenericActionComponent> ActionSystems for InsertInDuration<T> {
fn systems() -> SystemConfigs { insert_in_duration::<T>.in_set(TickSet) }
}

impl<T: GenericActionComponent> InsertInDuration<T> {
pub fn new(value: T, duration: Duration) -> Self {
Self { value, duration }
Expand Down
14 changes: 3 additions & 11 deletions crates/beet_ecs/src/lifecycle/actions/insert_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Inserts the given component when this behavior starts running.
#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Component, Reflect)]
#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(insert_on_run::<T>.in_set(PreTickSet))]
pub struct InsertOnRun<T: GenericActionComponent>(pub T);

impl<T: Default + GenericActionComponent> Default for InsertOnRun<T> {
fn default() -> Self { Self(T::default()) }
}

impl<T: GenericActionComponent> ActionMeta for InsertOnRun<T> {
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

impl<T: GenericActionComponent> ActionSystems for InsertOnRun<T> {
fn systems() -> SystemConfigs { insert_on_run::<T>.in_set(PreTickSet) }
}


impl<T: GenericActionComponent> InsertOnRun<T> {
pub fn new(value: impl Into<T>) -> Self { Self(value.into()) }
}
Expand Down
17 changes: 3 additions & 14 deletions crates/beet_ecs/src/lifecycle/actions/insert_on_trigger.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::DerefMut;

/// Triggers the given event when this behavior starts Insertning.
#[derive(Debug, Clone, PartialEq, Component, Reflect)]
#[derive(Debug, Clone, PartialEq, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::World)]
#[systems(inset_on_trigger::<E, T>.in_set(TickSet))]
pub struct InsertOnTrigger<E: GenericActionEvent, T: GenericActionComponent> {
pub value: T,
#[reflect(ignore)]
Expand Down Expand Up @@ -45,18 +46,6 @@ impl<E: GenericActionEvent, T: GenericActionComponent> InsertOnTrigger<E, T> {
}
}

impl<E: GenericActionEvent, T: GenericActionComponent> ActionMeta
for InsertOnTrigger<E, T>
{
fn category(&self) -> ActionCategory { ActionCategory::World }
}

impl<E: GenericActionEvent, T: GenericActionComponent> ActionSystems
for InsertOnTrigger<E, T>
{
fn systems() -> SystemConfigs { inset_on_trigger::<E, T>.in_set(TickSet) }
}

fn inset_on_trigger<E: GenericActionEvent, T: GenericActionComponent>(
mut commands: Commands,
mut reader: EventReader<E>,
Expand Down
15 changes: 4 additions & 11 deletions crates/beet_ecs/src/lifecycle/actions/log_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;
use std::borrow::Cow;

#[derive(Debug, Clone, PartialEq, Component, Reflect)]
#[reflect(Default, Component, ActionMeta)]
/// Mostly used for hello-world programs, logs a message when the action is run.
/// Use [`BeetDebugPlugin`] for debugging run-state.
#[derive(Debug, Clone, PartialEq, Action, Reflect)]
#[reflect(Default, Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(log_on_run.in_set(TickSet))]
pub struct LogOnRun(pub Cow<'static, str>);

impl ActionMeta for LogOnRun {
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

impl ActionSystems for LogOnRun {
fn systems() -> SystemConfigs { log_on_run.in_set(TickSet) }
}

impl Default for LogOnRun {
fn default() -> Self { Self(Cow::Borrowed("Running...")) }
}
Expand Down
16 changes: 3 additions & 13 deletions crates/beet_ecs/src/lifecycle/actions/remove_agent_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;
use std::marker::PhantomData;

/// Removes a component on the agent when this behavior starts running.
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Component, Reflect)]
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Action, Reflect)]
#[reflect(Component)]
#[category(ActionCategory::Agent)]
#[systems(remove_agent_on_run::<T>.in_set(PostTickSet))]
pub struct RemoveAgentOnRun<T: GenericActionComponent>(
#[reflect(ignore)] pub PhantomData<T>,
);
Expand All @@ -18,17 +19,6 @@ impl<T: GenericActionComponent> Default for RemoveAgentOnRun<T> {
// pub fn new() -> Self { Self::default() }
// }

impl<T: GenericActionComponent> ActionMeta for RemoveAgentOnRun<T> {
fn category(&self) -> ActionCategory { ActionCategory::Agent }
}

impl<T: GenericActionComponent> ActionSystems for RemoveAgentOnRun<T> {
fn systems() -> SystemConfigs {
remove_agent_on_run::<T>.in_set(PostTickSet)
}
}


fn remove_agent_on_run<T: GenericActionComponent>(
mut commands: Commands,
mut query: Query<(&TargetAgent, &RemoveAgentOnRun<T>), Added<Running>>,
Expand Down
13 changes: 3 additions & 10 deletions crates/beet_ecs/src/lifecycle/actions/repeat.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;


#[derive(Debug, Default, Clone, PartialEq, Component, Reflect)]
#[derive(Debug, Default, Clone, PartialEq, Action, Reflect)]
#[reflect(Default, Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(repeat.in_set(PreTickSet))]
/// Reattaches the [`Running`] component whenever it is removed.
pub struct Repeat;

impl ActionMeta for Repeat {
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

impl ActionSystems for Repeat {
fn systems() -> SystemConfigs { repeat.in_set(PreTickSet) }
}

/// This relys on [`sync_running`]
fn repeat(
mut commands: Commands,
Expand Down
15 changes: 3 additions & 12 deletions crates/beet_ecs/src/lifecycle/actions/set_agent_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Sets an agent's component when this behavior starts running.
/// This does nothing if the agent does not have the component.
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Component, Reflect)]
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Agent)]
#[systems(set_agent_on_run::<T>.in_set(PostTickSet))]
pub struct SetAgentOnRun<T: GenericActionComponent>(pub T);

impl<T: GenericActionComponent> SetAgentOnRun<T> {
Expand All @@ -16,16 +17,6 @@ impl<T: Default + GenericActionComponent> Default for SetAgentOnRun<T> {
fn default() -> Self { Self(T::default()) }
}


impl<T: GenericActionComponent> ActionMeta for SetAgentOnRun<T> {
fn category(&self) -> ActionCategory { ActionCategory::Agent }
}

impl<T: GenericActionComponent> ActionSystems for SetAgentOnRun<T> {
fn systems() -> SystemConfigs { set_agent_on_run::<T>.in_set(PostTickSet) }
}


fn set_agent_on_run<T: GenericActionComponent>(
mut agents: Query<&mut T>,
mut query: Query<(&TargetAgent, &SetAgentOnRun<T>), Added<Running>>,
Expand Down
14 changes: 3 additions & 11 deletions crates/beet_ecs/src/lifecycle/actions/set_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Sets a component when this behavior starts running.
/// This does nothing if the entity does not have the component.
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Component, Reflect)]
#[derive(PartialEq, Deref, DerefMut, Debug, Clone, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(set_on_run::<T>.in_set(PostTickSet))]
pub struct SetOnRun<T: GenericActionComponent>(pub T);

impl<T: Default + GenericActionComponent> Default for SetOnRun<T> {
fn default() -> Self { Self(T::default()) }
}

impl<T: GenericActionComponent> ActionMeta for SetOnRun<T> {
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

impl<T: GenericActionComponent> ActionSystems for SetOnRun<T> {
fn systems() -> SystemConfigs { set_on_run::<T>.in_set(PostTickSet) }
}


impl<T: GenericActionComponent> SetOnRun<T> {
pub fn new(value: impl Into<T>) -> Self { Self(value.into()) }
}
Expand Down
16 changes: 4 additions & 12 deletions crates/beet_ecs/src/lifecycle/actions/set_on_spawn.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Component, Reflect)]
#[reflect(Component, ActionMeta)]
/// Sets a component when this behavior spawns.
/// This does nothing if the entity does not have the component.
#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[category(ActionCategory::Behavior)]
#[systems(set_on_spawn::<T>.in_set(PreTickSet))]
pub struct SetOnSpawn<T: GenericActionComponent>(pub T);

impl<T: Default + GenericActionComponent> Default for SetOnSpawn<T> {
fn default() -> Self { Self(T::default()) }
}

impl<T: GenericActionComponent> ActionMeta for SetOnSpawn<T> {
fn category(&self) -> ActionCategory { ActionCategory::Behavior }
}

impl<T: GenericActionComponent> ActionSystems for SetOnSpawn<T> {
fn systems() -> SystemConfigs { set_on_spawn::<T>.in_set(PreTickSet) }
}


impl<T: GenericActionComponent> SetOnSpawn<T> {
pub fn new(value: impl Into<T>) -> Self { Self(value.into()) }
}
Expand Down
12 changes: 2 additions & 10 deletions crates/beet_ecs/src/lifecycle/actions/trigger_on_run.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::prelude::*;
use bevy::ecs::schedule::SystemConfigs;
use bevy::prelude::*;

/// Triggers the given event when this behavior starts running.
#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Component, Reflect)]
#[derive(Debug, Clone, PartialEq, Deref, DerefMut, Action, Reflect)]
#[reflect(Component, ActionMeta)]
#[systems(trigger_on_run::<T>.in_set(TickSet))]
pub struct TriggerOnRun<T: GenericActionEvent>(pub T);

impl<T: Default + GenericActionEvent> Default for TriggerOnRun<T> {
Expand All @@ -15,14 +15,6 @@ impl<T: GenericActionEvent> TriggerOnRun<T> {
pub fn new(value: impl Into<T>) -> Self { Self(value.into()) }
}

impl<T: GenericActionEvent> ActionMeta for TriggerOnRun<T> {
fn category(&self) -> ActionCategory { ActionCategory::World }
}

impl<T: GenericActionEvent> ActionSystems for TriggerOnRun<T> {
fn systems() -> SystemConfigs { trigger_on_run::<T>.in_set(TickSet) }
}

fn trigger_on_run<T: GenericActionEvent>(
mut writer: EventWriter<T>,
query: Query<&TriggerOnRun<T>, Added<Running>>,
Expand Down
Loading

0 comments on commit 12832ba

Please sign in to comment.