Skip to content

Commit

Permalink
Bevy 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Dec 19, 2024
1 parent 5e36372 commit 0df0166
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 76 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ repository = "https://github.com/Seldom-SE/seldom_state"
leafwing_input = ["dep:leafwing-input-manager"]

[dependencies]
leafwing-input-manager = { git = "https://github.com/pcwalton/leafwing-input-manager", rev = "195c97c", default-features = false, optional = true }
bevy = { version = "0.15.0-rc.1", default-features = false }
leafwing-input-manager = { version = "0.16.0", default-features = false, optional = true }
bevy = { version = "0.15.0", default-features = false }
either = "1.9"

[dev-dependencies]
bevy = "0.15.0-rc.1"
leafwing-input-manager = { git = "https://github.com/pcwalton/leafwing-input-manager", rev = "195c97c" }
bevy = "0.15.0"
leafwing-input-manager = "0.16.0"

[[example]]
name = "input"
Expand Down
4 changes: 2 additions & 2 deletions examples/chase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn follow(
follow_transform.translation += (target_translation - follow_translation)
.normalize_or_zero()
* follow.speed
* time.delta_seconds();
* time.delta_secs();
}
}

Expand Down Expand Up @@ -153,5 +153,5 @@ fn move_player(
)
.normalize_or_zero()
* PLAYER_SPEED
* time.delta_seconds();
* time.delta_secs();
}
4 changes: 2 additions & 2 deletions examples/done.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn go_to_target(
for (entity, mut transform, go_to_selection) in &mut go_to_selections {
let target = go_to_selection.target;
let delta = target - transform.translation.truncate();
let movement = delta.normalize_or_zero() * go_to_selection.speed * time.delta_seconds();
let movement = delta.normalize_or_zero() * go_to_selection.speed * time.delta_secs();

if movement.length() > delta.length() {
transform.translation = target.extend(transform.translation.z);
Expand Down Expand Up @@ -96,5 +96,5 @@ fn update_cursor_position(
**position = windows
.single()
.cursor_position()
.and_then(|cursor_position| camera.viewport_to_world_2d(transform, cursor_position));
.and_then(|cursor_position| camera.viewport_to_world_2d(transform, cursor_position).ok());
}
18 changes: 9 additions & 9 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ fn init(mut commands: Commands, asset_server: Res<AssetServer>) {
// From `leafwing-input-manager`
InputManagerBundle {
input_map: InputMap::default()
.insert(Action::Move, VirtualAxis::horizontal_arrow_keys())
.insert(
.with_axis(Action::Move, VirtualAxis::horizontal_arrow_keys())
.with_axis(
Action::Move,
SingleAxis::symmetric(GamepadAxisType::LeftStickX, 0.),
GamepadControlAxis::new(GamepadAxis::LeftStickX),
)
.insert(Action::Jump, KeyCode::Space)
.insert(Action::Jump, GamepadButtonType::South)
.build(),
.with(Action::Jump, KeyCode::Space)
.with(Action::Jump, GamepadButton::South),
..default()
},
// This state machine achieves a very rigid movement system. Consider a state machine for
Expand Down Expand Up @@ -63,8 +62,9 @@ fn init(mut commands: Commands, asset_server: Res<AssetServer>) {
));
}

#[derive(Actionlike, Clone, Eq, Hash, PartialEq, Reflect)]
#[derive(Actionlike, Clone, Eq, Hash, PartialEq, Reflect, Debug)]
enum Action {
#[actionlike(Axis)]
Move,
Jump,
}
Expand Down Expand Up @@ -92,15 +92,15 @@ const PLAYER_SPEED: f32 = 200.;

fn walk(mut groundeds: Query<(&mut Transform, &Grounded)>, time: Res<Time>) {
for (mut transform, grounded) in &mut groundeds {
transform.translation.x += *grounded as i32 as f32 * time.delta_seconds() * PLAYER_SPEED;
transform.translation.x += *grounded as i32 as f32 * time.delta_secs() * PLAYER_SPEED;
}
}

const GRAVITY: f32 = -1000.;

fn fall(mut fallings: Query<(&mut Transform, &mut Falling)>, time: Res<Time>) {
for (mut transform, mut falling) in &mut fallings {
let dt = time.delta_seconds();
let dt = time.delta_secs();
falling.velocity += dt * GRAVITY;
transform.translation.y += dt * falling.velocity;
}
Expand Down
122 changes: 63 additions & 59 deletions src/trigger/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{any::type_name, ops::Range};

use bevy::math::InvalidDirectionError;
use leafwing_input_manager::action_state::ActionData;

use crate::prelude::*;
Expand Down Expand Up @@ -97,11 +98,11 @@ pub fn clamped_value_max(
/// bounds. If no minimum length is necessary, use `0.`. To exclude specifically neutral axis pairs,
/// use a small positive value. If no maximum length is necessary, use `f32::INFINITY`, or similar.
/// If rotation bounds are not necessary, use the same value for the minimum and maximum ex.
/// `Rotation::NORTH..Rotation::NORTH`.
/// `Dir2::Y..Dir2::Y`.
pub fn axis_pair<A: Actionlike>(
action: A,
length_bounds: Range<f32>,
rotation_bounds: Range<Rot2>,
rotation_bounds: Range<Dir2>,
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
(move |In(entity): In<Entity>, actors: Query<&ActionState<A>>| {
let axis_pair = actors
Expand All @@ -114,17 +115,25 @@ pub fn axis_pair<A: Actionlike>(
})
.axis_pair(&action);

let length = axis_pair.length();
let rotation = (length != 0.).then(|| Rot2::from(axis_pair.to_angle()));

(length_bounds.contains(&length)
&& rotation
.map(|rotation| {
let angle = rotation_bounds.start.angle_between(rotation_bounds.end);
// @e test
angle == 0. || rotation_bounds.start.angle_between(rotation) <= angle
})
.unwrap_or(true))
match Dir2::new_and_length(axis_pair) {
Ok((rotation, length)) => {
length_bounds.contains(&length)
&& (rotation_bounds.start == rotation_bounds.end
|| ({
let start = rotation_bounds.start.to_angle();
let end = rotation_bounds.end.to_angle();
let rotation = rotation.to_angle();

if start < end {
rotation >= start && rotation <= end
} else {
rotation >= start || rotation <= end
}
}))
}
Err(InvalidDirectionError::Zero) => length_bounds.contains(&0.),
Err(InvalidDirectionError::Infinite | InvalidDirectionError::NaN) => false,
}
.then_some(axis_pair)
.ok_or(axis_pair)
})
Expand All @@ -134,52 +143,48 @@ pub fn axis_pair<A: Actionlike>(
/// Unbounded [`axis_pair`]
pub fn axis_pair_unbounded(
action: impl Actionlike,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
axis_pair(action, 0.0..f32::INFINITY, Rotation::NORTH..Rotation::NORTH)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
axis_pair(action, 0.0..f32::INFINITY, Dir2::Y..Dir2::Y)
}

/// [`axis_pair`] with only a minimum length bound
pub fn axis_pair_min_length(
action: impl Actionlike,
min_length: f32,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
axis_pair(
action,
min_length..f32::INFINITY,
Rotation::NORTH..Rotation::NORTH,
)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
axis_pair(action, min_length..f32::INFINITY, Dir2::Y..Dir2::Y)
}

/// [`axis_pair`] with only a maximum length bound
pub fn axis_pair_max_length(
action: impl Actionlike,
max_length: f32,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
axis_pair(action, 0.0..max_length, Rotation::NORTH..Rotation::NORTH)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
axis_pair(action, 0.0..max_length, Dir2::Y..Dir2::Y)
}

/// [`axis_pair`] with only length bounds
pub fn axis_pair_length_bounds(
action: impl Actionlike,
length_bounds: Range<f32>,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
axis_pair(action, length_bounds, Rotation::NORTH..Rotation::NORTH)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
axis_pair(action, length_bounds, Dir2::Y..Dir2::Y)
}

/// [`axis_pair`] with only rotation bounds
pub fn axis_pair_rotation_bounds(
action: impl Actionlike,
rotation_bounds: Range<Rotation>,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
rotation_bounds: Range<Dir2>,
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
axis_pair(action, 0.0..f32::INFINITY, rotation_bounds)
}

/// [`axis_pair`] with axes clamped to [-1, 1]
pub fn clamped_axis_pair<A: Actionlike>(
action: A,
length_bounds: Range<f32>,
rotation_bounds: Range<Rotation>,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
rotation_bounds: Range<Dir2>,
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
(move |In(entity): In<Entity>, actors: Query<&ActionState<A>>| {
let axis_pair = actors
.get(entity)
Expand All @@ -191,68 +196,67 @@ pub fn clamped_axis_pair<A: Actionlike>(
})
.clamped_axis_pair(&action);

axis_pair
.and_then(|axis_pair| {
let length = axis_pair.length();
let rotation = axis_pair.rotation();

(length_bounds.contains(&length)
&& rotation
.map(|rotation| {
if rotation_bounds.start < rotation_bounds.end {
rotation >= rotation_bounds.start && rotation <= rotation_bounds.end
match Dir2::new_and_length(axis_pair) {
Ok((rotation, length)) => {
length_bounds.contains(&length)
&& (rotation_bounds.start == rotation_bounds.end
|| ({
let start = rotation_bounds.start.to_angle();
let end = rotation_bounds.end.to_angle();
let rotation = rotation.to_angle();

if start < end {
rotation >= start && rotation <= end
} else {
rotation >= rotation_bounds.start || rotation <= rotation_bounds.end
rotation >= start || rotation <= end
}
})
.unwrap_or(true))
.then_some(axis_pair)
})
.ok_or(axis_pair)
}))
}
Err(InvalidDirectionError::Zero) => length_bounds.contains(&0.),
Err(InvalidDirectionError::Infinite | InvalidDirectionError::NaN) => false,
}
.then_some(axis_pair)
.ok_or(axis_pair)
})
.into_trigger()
}

/// Unbounded [`clamped_axis_pair`]
pub fn clamped_axis_pair_unbounded(
action: impl Actionlike,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
clamped_axis_pair(action, 0.0..f32::INFINITY, Rotation::NORTH..Rotation::NORTH)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
clamped_axis_pair(action, 0.0..f32::INFINITY, Dir2::Y..Dir2::Y)
}

/// [`clamped_axis_pair`] with only a minimum length bound
pub fn clamped_axis_pair_min_length(
action: impl Actionlike,
min_length: f32,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
clamped_axis_pair(
action,
min_length..f32::INFINITY,
Rotation::NORTH..Rotation::NORTH,
)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
clamped_axis_pair(action, min_length..f32::INFINITY, Dir2::Y..Dir2::Y)
}

/// [`clamped_axis_pair`] with only a maximum length bound
pub fn clamped_axis_pair_max_length(
action: impl Actionlike,
max_length: f32,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
clamped_axis_pair(action, 0.0..max_length, Rotation::NORTH..Rotation::NORTH)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
clamped_axis_pair(action, 0.0..max_length, Dir2::Y..Dir2::Y)
}

/// [`clamped_axis_pair`] with only length bounds
pub fn clamped_axis_pair_length_bounds(
action: impl Actionlike,
length_bounds: Range<f32>,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
clamped_axis_pair(action, length_bounds, Rotation::NORTH..Rotation::NORTH)
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
clamped_axis_pair(action, length_bounds, Dir2::Y..Dir2::Y)
}

/// [`clamped_axis_pair`] with only rotation bounds
pub fn clamped_axis_pair_rotation_bounds(
action: impl Actionlike,
rotation_bounds: Range<Rotation>,
) -> impl EntityTrigger<Out = Result<DualAxisData, Option<DualAxisData>>> {
rotation_bounds: Range<Dir2>,
) -> impl EntityTrigger<Out = Result<Vec2, Vec2>> {
clamped_axis_pair(action, 0.0..f32::INFINITY, rotation_bounds)
}

Expand Down

0 comments on commit 0df0166

Please sign in to comment.