Skip to content

Commit

Permalink
impl common traits for convinience
Browse files Browse the repository at this point in the history
  • Loading branch information
hyranno committed Oct 12, 2023
1 parent 9f1f353 commit e859829
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
31 changes: 26 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,13 @@ 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 +209,13 @@ 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 +241,16 @@ 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 +260,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 +289,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

0 comments on commit e859829

Please sign in to comment.