Skip to content

Commit

Permalink
Bevy 0.13, on_event fix, trigger combinator fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Feb 21, 2024
1 parent 3d92493 commit 7cd23c1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 34 deletions.
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
name = "seldom_state"
version = "0.9.0"
edition = "2021"
categories = [ "game-development" ]
categories = ["game-development"]
description = "Component-based state machine plugin for Bevy. Useful for AI, player state, and other entities that occupy various states."
exclude = [ "assets/" ]
keywords = [ "gamedev", "bevy", "ai", "state-machine" ]
exclude = ["assets/"]
keywords = ["gamedev", "bevy", "ai", "state-machine"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/Seldom-SE/seldom_state"

[features]
leafwing_input = [ "dep:leafwing-input-manager" ]
leafwing_input = ["dep:leafwing-input-manager"]

[dependencies]
bevy = { version = "0.12.0", default-features = false }
bevy = { version = "0.13.0", default-features = false }
either = "1.9"
leafwing-input-manager = { version = "0.11.1", default-features = false, optional = true }
seldom_fn_plugin = "0.5.0"
leafwing-input-manager = { version = "0.13.0", default-features = false, optional = true }
seldom_fn_plugin = "0.6.0"

[dev-dependencies]
bevy = "0.12.0"
leafwing-input-manager = "0.11.1"
bevy = "0.13.0"
leafwing-input-manager = "0.13.0"

[[example]]
name = "input"
required-features = [ "leafwing_input" ]
required-features = ["leafwing_input"]

[package.metadata.docs.rs]
all-features = true
6 changes: 3 additions & 3 deletions examples/chase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ const PLAYER_SPEED: f32 = 200.;

fn move_player(
mut players: Query<&mut Transform, With<Player>>,
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
players.single_mut().translation += Vec3::new(
(keys.pressed(KeyCode::Right) as i32 - keys.pressed(KeyCode::Left) as i32) as f32,
(keys.pressed(KeyCode::Up) as i32 - keys.pressed(KeyCode::Down) as i32) as f32,
(keys.pressed(KeyCode::ArrowRight) as i32 - keys.pressed(KeyCode::ArrowLeft) as i32) as f32,
(keys.pressed(KeyCode::ArrowUp) as i32 - keys.pressed(KeyCode::ArrowDown) as i32) as f32,
0.,
)
.normalize_or_zero()
Expand Down
5 changes: 4 additions & 1 deletion examples/done.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ fn init(mut commands: Commands, asset_server: Res<AssetServer>) {
));
}

fn click(mouse: Res<Input<MouseButton>>, cursor_position: Res<CursorPosition>) -> Option<Vec2> {
fn click(
mouse: Res<ButtonInput<MouseButton>>,
cursor_position: Res<CursorPosition>,
) -> Option<Vec2> {
mouse
.just_pressed(MouseButton::Left)
.then_some(())
Expand Down
8 changes: 4 additions & 4 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ fn init(mut commands: Commands, asset_server: Res<AssetServer>) {
// From `leafwing-input-manager`
InputManagerBundle {
input_map: InputMap::default()
.insert(VirtualAxis::horizontal_arrow_keys(), Action::Move)
.insert(Action::Move, VirtualAxis::horizontal_arrow_keys())
.insert(
SingleAxis::symmetric(GamepadAxisType::LeftStickX, 0.),
Action::Move,
SingleAxis::symmetric(GamepadAxisType::LeftStickX, 0.),
)
.insert(KeyCode::Space, Action::Jump)
.insert(GamepadButtonType::South, Action::Jump)
.insert(Action::Jump, KeyCode::Space)
.insert(Action::Jump, GamepadButtonType::South)
.build(),
..default()
},
Expand Down
52 changes: 48 additions & 4 deletions src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,52 @@ pub trait IntoTrigger<Marker>: Sized {
fn into_trigger(self) -> Self::Trigger;

/// Negates the trigger. Do not override.
fn not(self) -> impl Trigger {
fn not(
self,
) -> impl Trigger<
Out = Result<
<<Self::Trigger as Trigger>::Out as TriggerOut>::Err,
<<Self::Trigger as Trigger>::Out as TriggerOut>::Ok,
>,
> {
NotTrigger(self.into_trigger())
}

/// Combines these triggers by logical AND. Do not override.
fn and<Marker2>(self, other: impl IntoTrigger<Marker2>) -> impl Trigger {
fn and<Marker2, T: IntoTrigger<Marker2>>(
self,
other: T,
) -> impl Trigger<
Out = Result<
(
<<Self::Trigger as Trigger>::Out as TriggerOut>::Ok,
<<T::Trigger as Trigger>::Out as TriggerOut>::Ok,
),
Either<
<<Self::Trigger as Trigger>::Out as TriggerOut>::Err,
<<T::Trigger as Trigger>::Out as TriggerOut>::Err,
>,
>,
> {
AndTrigger(self.into_trigger(), other.into_trigger())
}

/// Combines these triggers by logical OR. Do not override.
fn or<Marker2>(self, other: impl IntoTrigger<Marker2>) -> impl Trigger {
fn or<Marker2, T: IntoTrigger<Marker2>>(
self,
other: T,
) -> impl Trigger<
Out = Result<
Either<
<<Self::Trigger as Trigger>::Out as TriggerOut>::Ok,
<<T::Trigger as Trigger>::Out as TriggerOut>::Ok,
>,
(
<<Self::Trigger as Trigger>::Out as TriggerOut>::Err,
<<T::Trigger as Trigger>::Out as TriggerOut>::Err,
),
>,
> {
OrTrigger(self.into_trigger(), other.into_trigger())
}
}
Expand Down Expand Up @@ -291,7 +326,16 @@ pub fn done(expected: Option<Done>) -> impl Trigger<Out = bool> {
}

/// Trigger that transitions when it receives the associated event
pub fn on_event<T: Clone + Event>(mut reader: EventReader<T>) -> Option<T> {
pub fn on_event<T: Clone + Event>(
mut has_run: Local<bool>,
mut reader: EventReader<T>,
) -> Option<T> {
if !*has_run {
reader.read().last();
*has_run = true;
return None;
}

reader.read().last().cloned()
}

Expand Down
24 changes: 12 additions & 12 deletions src/trigger/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn value<A: Actionlike>(action: A, bounds: Range<f32>) -> impl Trigger<Out =
type_name::<A>()
)
})
.value(action.clone());
.value(&action);

if bounds.contains(&value) {
Ok(value)
Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn clamped_value<A: Actionlike>(
type_name::<A>()
)
})
.clamped_value(action.clone());
.clamped_value(&action);

if bounds.contains(&value) {
Ok(value)
Expand Down Expand Up @@ -109,7 +109,7 @@ pub fn axis_pair<A: Actionlike>(
type_name::<A>()
)
})
.axis_pair(action.clone());
.axis_pair(&action);

axis_pair
.and_then(|axis_pair| {
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn clamped_axis_pair<A: Actionlike>(
type_name::<A>()
)
})
.clamped_axis_pair(action.clone());
.clamped_axis_pair(&action);

axis_pair
.and_then(|axis_pair| {
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn just_pressed<A: Actionlike>(action: A) -> impl Trigger<Out = bool> {
type_name::<A>()
)
})
.just_pressed(action.clone())
.just_pressed(&action)
})
.into_trigger()
}
Expand All @@ -285,7 +285,7 @@ pub fn pressed<A: Actionlike>(action: A) -> impl Trigger<Out = bool> {
type_name::<A>()
)
})
.pressed(action.clone())
.pressed(&action)
})
.into_trigger()
}
Expand All @@ -301,24 +301,24 @@ pub fn just_released<A: Actionlike>(action: A) -> impl Trigger<Out = bool> {
type_name::<A>()
)
})
.just_released(action.clone())
.just_released(&action)
})
.into_trigger()
}

/// Trigger that always transitions, providing the given [`Actionlike`]'s [`ActionData`]
pub fn action_data<A: Actionlike>(action: A) -> impl Trigger<Out = Result<ActionData, Never>> {
/// Provides the given [`Actionlike`]'s [`ActionData`]
pub fn action_data<A: Actionlike>(action: A) -> impl Trigger<Out = Option<ActionData>> {
(move |In(entity): In<Entity>, actors: Query<&ActionState<A>>| {
Ok(actors
actors
.get(entity)
.unwrap_or_else(|_| {
panic!(
"entity {entity:?} with `ActionDataTrigger<{0}>` is missing `ActionState<{0}>`",
type_name::<A>()
)
})
.action_data(action.clone())
.clone())
.action_data(&action)
.cloned()
})
.into_trigger()
}

0 comments on commit 7cd23c1

Please sign in to comment.