Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Clone on triggers if available #8

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ where

fn run(&mut self, world: &World, entity: Entity) -> Option<(Box<dyn Insert>, TypeId)> {
let state = self.system_state.as_mut().unwrap();
let Ok(res) = self.trigger.trigger(entity, state.get(world)) else { return None };
let Ok(res) = self.trigger.trigger(entity, state.get(world)) else {
return None;
};
(self.builder)(Prev::from_entity(entity, world), res)
.map(|state| (Box::new(state) as Box<dyn Insert>, TypeId::of::<Next>()))
}
Expand Down Expand Up @@ -286,7 +288,9 @@ impl StateMachine {
.iter_mut()
.filter(|(type_id, _)| *type_id == current || *type_id == TypeId::of::<AnyState>())
.find_map(|(_, transition)| transition.run(world, entity))
else { return };
else {
return;
};
let to = &self.states[&next_state];

for event in from.on_exit.iter() {
Expand Down
34 changes: 29 additions & 5 deletions src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) fn trigger_plugin(app: &mut App) {

/// Wrapper for [`core::convert::Infallible`]. Use for [`Trigger::Err`] if the trigger is
/// infallible.
#[derive(Debug, Deref, DerefMut)]
#[derive(Debug, Deref, DerefMut, Clone, Copy, PartialEq, Eq)]
pub struct Never {
never: Infallible,
}
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<T: BoolTrigger> OptionTrigger for T {
}

/// Trigger that always transitions
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct AlwaysTrigger;

impl Trigger for AlwaysTrigger {
Expand Down Expand Up @@ -180,6 +180,14 @@ impl<T: Trigger> Trigger for NotTrigger<T> {
}
}

impl<T: Trigger + Clone> Clone for NotTrigger<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<T: Trigger + Copy> Copy for NotTrigger<T> {}

/// Trigger that combines two triggers by logical AND
#[derive(Debug)]
pub struct AndTrigger<T: Trigger, U: Trigger>(pub T, pub U);
Expand All @@ -202,6 +210,14 @@ impl<T: Trigger, U: Trigger> Trigger for AndTrigger<T, U> {
}
}

impl<T: Trigger + Clone, U: Trigger + Clone> Clone for AndTrigger<T, U> {
fn clone(&self) -> Self {
Self(self.0.clone(), self.1.clone())
}
}

impl<T: Trigger + Copy, U: Trigger + Copy> Copy for AndTrigger<T, U> {}

/// Trigger that combines two triggers by logical OR
#[derive(Debug)]
pub struct OrTrigger<T: Trigger, U: Trigger>(pub T, pub U);
Expand All @@ -227,9 +243,17 @@ impl<T: Trigger, U: Trigger> Trigger for OrTrigger<T, U> {
}
}

impl<T: Trigger + Clone, U: Trigger + Clone> Clone for OrTrigger<T, U> {
fn clone(&self) -> Self {
Self(self.0.clone(), self.1.clone())
}
}

impl<T: Trigger + Copy, U: Trigger + Copy> Copy for OrTrigger<T, U> {}

/// Marker component that represents that the current state has completed. Removed from every entity
/// each frame after checking triggers. To be used with [`DoneTrigger`].
#[derive(Component, Debug, Eq, PartialEq)]
#[derive(Component, Debug, Eq, PartialEq, Clone, Copy)]
#[component(storage = "SparseSet")]
pub enum Done {
/// Success variant
Expand All @@ -239,7 +263,7 @@ pub enum Done {
}

/// Trigger that transitions if the entity has the [`Done`] component with the associated variant
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum DoneTrigger {
/// Success variant
Success,
Expand Down Expand Up @@ -268,7 +292,7 @@ impl DoneTrigger {
}

/// Trigger that transitions when it receives the associated event
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone, Copy)]
pub struct EventTrigger<T: Clone + Event>(PhantomData<T>);

impl<T: Clone + Event> OptionTrigger for EventTrigger<T> {
Expand Down
18 changes: 9 additions & 9 deletions src/trigger/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use leafwing_input_manager::{
use crate::prelude::*;

/// Trigger that transitions if the given [`Actionlike`]'s value is within the given bounds
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ValueTrigger<A: Actionlike> {
/// The action
pub action: A,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<A: Actionlike> ValueTrigger<A> {
}

/// Trigger that transitions if the given [`Actionlike`]'s value is within the given bounds
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ClampedValueTrigger<A: Actionlike> {
/// The action
pub action: A,
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<A: Actionlike> ClampedValueTrigger<A> {

/// Trigger that transitions if the given [`Actionlike`]'s [`DualAxisData`] is within the given
/// bounds
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct AxisPairTrigger<A: Actionlike> {
/// The action
pub action: A,
Expand Down Expand Up @@ -247,7 +247,7 @@ impl<A: Actionlike> AxisPairTrigger<A> {

/// Trigger that transitions if the given [`Actionlike`]'s [`DualAxisData`] is within the given
/// bounds
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ClampedAxisPairTrigger<A: Actionlike> {
/// The action
pub action: A,
Expand Down Expand Up @@ -361,7 +361,7 @@ impl<A: Actionlike> ClampedAxisPairTrigger<A> {
}

/// Trigger that transitions upon pressing the given [`Actionlike`]
#[derive(Debug, Deref, DerefMut)]
#[derive(Debug, Deref, DerefMut, Clone)]
pub struct JustPressedTrigger<A: Actionlike>(pub A);

impl<A: Actionlike> BoolTrigger for JustPressedTrigger<A> {
Expand All @@ -382,7 +382,7 @@ impl<A: Actionlike> BoolTrigger for JustPressedTrigger<A> {
}

/// Trigger that transitions while pressing the given [`Actionlike`]
#[derive(Debug, Deref, DerefMut)]
#[derive(Debug, Deref, DerefMut, Clone)]
pub struct PressedTrigger<A: Actionlike>(pub A);

impl<A: Actionlike> BoolTrigger for PressedTrigger<A> {
Expand All @@ -403,7 +403,7 @@ impl<A: Actionlike> BoolTrigger for PressedTrigger<A> {
}

/// Trigger that transitions upon releasing the given [`Actionlike`]
#[derive(Debug, Deref, DerefMut)]
#[derive(Debug, Deref, DerefMut, Clone)]
pub struct JustReleasedTrigger<A: Actionlike>(pub A);

#[cfg(feature = "leafwing_input")]
Expand All @@ -425,7 +425,7 @@ impl<A: Actionlike> BoolTrigger for JustReleasedTrigger<A> {
}

/// Trigger that transitions while the given [`Actionlike`] is released
#[derive(Debug, Deref, DerefMut)]
#[derive(Debug, Deref, DerefMut, Clone)]
pub struct ReleasedTrigger<A: Actionlike>(pub A);

impl<A: Actionlike> BoolTrigger for ReleasedTrigger<A> {
Expand All @@ -446,7 +446,7 @@ impl<A: Actionlike> BoolTrigger for ReleasedTrigger<A> {
}

/// Trigger that always transitions, providing the given [`Actionlike`]'s [`ActionData`]
#[derive(Debug, Deref, DerefMut)]
#[derive(Debug, Deref, DerefMut, Clone)]
pub struct ActionDataTrigger<A: Actionlike>(pub A);

impl<A: Actionlike> Trigger for ActionDataTrigger<A> {
Expand Down