-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
110 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::prelude::*; | ||
use bevy::prelude::*; | ||
use std::marker::PhantomData; | ||
|
||
pub type TriggerOnRun<T> = TriggerOnTrigger<OnRun, T>; | ||
|
||
/// when [`<In>`] is called, trigger [`<Out>`] | ||
#[derive(Action, Reflect)] | ||
#[reflect(Default, Component)] | ||
#[observers(on_trigger::<In,Out>)] | ||
pub struct TriggerOnTrigger< | ||
In: GenericActionEvent, | ||
Out: Default + GenericActionEvent, | ||
> { | ||
pub out: Out, | ||
#[reflect(ignore)] | ||
phantom: PhantomData<In>, | ||
} | ||
|
||
impl<In: GenericActionEvent, Out: Default + GenericActionEvent> | ||
TriggerOnTrigger<In, Out> | ||
{ | ||
pub fn new(out: Out) -> Self { | ||
Self { | ||
out, | ||
phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
fn on_trigger<In: GenericActionEvent, Out: Default + GenericActionEvent>( | ||
trigger: Trigger<In>, | ||
query: Query<&TriggerOnTrigger<In, Out>>, | ||
mut commands: Commands, | ||
) { | ||
let action = query | ||
.get(trigger.entity()) | ||
.expect(expect_action::NO_ACTION_COMP); | ||
commands.trigger_targets(action.out.clone(), trigger.entity()); | ||
} | ||
|
||
impl<In: GenericActionEvent, Out: Default + GenericActionEvent> Default | ||
for TriggerOnTrigger<In, Out> | ||
{ | ||
fn default() -> Self { | ||
Self { | ||
out: Out::default(), | ||
phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
// see `end_on_run` for tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// use crate::prelude::*; | ||
use bevy::prelude::*; | ||
|
||
#[derive(Debug, Default, Clone, Event)] | ||
pub struct OnRun; | ||
|
||
/// Signifies an action has started running. | ||
#[derive(Debug, Default, Clone, Event, Reflect)] | ||
#[reflect(Default)] | ||
pub struct OnRun; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
|
||
|
||
#[rustfmt::skip] | ||
pub mod expect_action{ | ||
/// Action observers are removed when the component is so | ||
/// we always expect the component to exist. | ||
pub const NO_ACTION_COMP: &str = | ||
"Action component missing from observer query"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pub mod errors; | ||
#[allow(unused_imports)] | ||
pub use self::errors::*; | ||
pub mod observer_utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters