From e8598299058ab9b668819699a16bec8528bbfdf5 Mon Sep 17 00:00:00 2001 From: hyranno Date: Thu, 12 Oct 2023 11:07:25 +0900 Subject: [PATCH 1/2] impl common traits for convinience --- src/trigger.rs | 31 ++++++++++++++++++++++++++----- src/trigger/input.rs | 18 +++++++++--------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/trigger.rs b/src/trigger.rs index 4248aea..1266dae 100644 --- a/src/trigger.rs +++ b/src/trigger.rs @@ -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, } @@ -145,7 +145,7 @@ impl OptionTrigger for T { } /// Trigger that always transitions -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct AlwaysTrigger; impl Trigger for AlwaysTrigger { @@ -180,6 +180,13 @@ impl Trigger for NotTrigger { } } +impl Clone for NotTrigger { + fn clone(&self) -> Self { + Self(self.0.clone()) + } +} +impl Copy for NotTrigger {} + /// Trigger that combines two triggers by logical AND #[derive(Debug)] pub struct AndTrigger(pub T, pub U); @@ -202,6 +209,13 @@ impl Trigger for AndTrigger { } } +impl Clone for AndTrigger { + fn clone(&self) -> Self { + Self(self.0.clone(), self.1.clone()) + } +} +impl Copy for AndTrigger {} + /// Trigger that combines two triggers by logical OR #[derive(Debug)] pub struct OrTrigger(pub T, pub U); @@ -227,9 +241,16 @@ impl Trigger for OrTrigger { } } +impl Clone for OrTrigger { + fn clone(&self) -> Self { + Self(self.0.clone(), self.1.clone()) + } +} +impl Copy for OrTrigger {} + /// 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 @@ -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, @@ -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(PhantomData); impl OptionTrigger for EventTrigger { diff --git a/src/trigger/input.rs b/src/trigger/input.rs index 87d2787..9aef381 100644 --- a/src/trigger/input.rs +++ b/src/trigger/input.rs @@ -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 { /// The action pub action: A, @@ -69,7 +69,7 @@ impl ValueTrigger { } /// Trigger that transitions if the given [`Actionlike`]'s value is within the given bounds -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ClampedValueTrigger { /// The action pub action: A, @@ -132,7 +132,7 @@ impl ClampedValueTrigger { /// Trigger that transitions if the given [`Actionlike`]'s [`DualAxisData`] is within the given /// bounds -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct AxisPairTrigger { /// The action pub action: A, @@ -247,7 +247,7 @@ impl AxisPairTrigger { /// Trigger that transitions if the given [`Actionlike`]'s [`DualAxisData`] is within the given /// bounds -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ClampedAxisPairTrigger { /// The action pub action: A, @@ -361,7 +361,7 @@ impl ClampedAxisPairTrigger { } /// Trigger that transitions upon pressing the given [`Actionlike`] -#[derive(Debug, Deref, DerefMut)] +#[derive(Debug, Deref, DerefMut, Clone)] pub struct JustPressedTrigger(pub A); impl BoolTrigger for JustPressedTrigger { @@ -382,7 +382,7 @@ impl BoolTrigger for JustPressedTrigger { } /// Trigger that transitions while pressing the given [`Actionlike`] -#[derive(Debug, Deref, DerefMut)] +#[derive(Debug, Deref, DerefMut, Clone)] pub struct PressedTrigger(pub A); impl BoolTrigger for PressedTrigger { @@ -403,7 +403,7 @@ impl BoolTrigger for PressedTrigger { } /// Trigger that transitions upon releasing the given [`Actionlike`] -#[derive(Debug, Deref, DerefMut)] +#[derive(Debug, Deref, DerefMut, Clone)] pub struct JustReleasedTrigger(pub A); #[cfg(feature = "leafwing_input")] @@ -425,7 +425,7 @@ impl BoolTrigger for JustReleasedTrigger { } /// Trigger that transitions while the given [`Actionlike`] is released -#[derive(Debug, Deref, DerefMut)] +#[derive(Debug, Deref, DerefMut, Clone)] pub struct ReleasedTrigger(pub A); impl BoolTrigger for ReleasedTrigger { @@ -446,7 +446,7 @@ impl BoolTrigger for ReleasedTrigger { } /// Trigger that always transitions, providing the given [`Actionlike`]'s [`ActionData`] -#[derive(Debug, Deref, DerefMut)] +#[derive(Debug, Deref, DerefMut, Clone)] pub struct ActionDataTrigger(pub A); impl Trigger for ActionDataTrigger { From 1ebdce72316778cfbeb2b63dcb21b8cbb165328e Mon Sep 17 00:00:00 2001 From: Seldom-SE <38388947+Seldom-SE@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:32:47 -0700 Subject: [PATCH 2/2] fmt --- src/machine.rs | 8 ++++++-- src/trigger.rs | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index ec12ba5..cf0de9d 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -79,7 +79,9 @@ where fn run(&mut self, world: &World, entity: Entity) -> Option<(Box, 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, TypeId::of::())) } @@ -286,7 +288,9 @@ impl StateMachine { .iter_mut() .filter(|(type_id, _)| *type_id == current || *type_id == TypeId::of::()) .find_map(|(_, transition)| transition.run(world, entity)) - else { return }; + else { + return; + }; let to = &self.states[&next_state]; for event in from.on_exit.iter() { diff --git a/src/trigger.rs b/src/trigger.rs index 1266dae..3a14bc6 100644 --- a/src/trigger.rs +++ b/src/trigger.rs @@ -185,6 +185,7 @@ impl Clone for NotTrigger { Self(self.0.clone()) } } + impl Copy for NotTrigger {} /// Trigger that combines two triggers by logical AND @@ -214,6 +215,7 @@ impl Clone for AndTrigger { Self(self.0.clone(), self.1.clone()) } } + impl Copy for AndTrigger {} /// Trigger that combines two triggers by logical OR @@ -246,6 +248,7 @@ impl Clone for OrTrigger { Self(self.0.clone(), self.1.clone()) } } + impl Copy for OrTrigger {} /// Marker component that represents that the current state has completed. Removed from every entity