Skip to content

Commit

Permalink
Remove Direction in favour of Direction2d from Bevy (#488)
Browse files Browse the repository at this point in the history
* Remove Direction in favour of Direction2d from Bevy

* Fix test failures and correct examples

* Fixed outstanding doc issues and bumped version

* Correct release note formatting

* Correct crate version

* Remove explicit targets in fixed docs

---------

Co-authored-by: Alice Cecile <[email protected]>
  • Loading branch information
Ant59 and alice-i-cecile authored Feb 23, 2024
1 parent 4a92146 commit a645abf
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 272 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "leafwing-input-manager"
description = "A powerfully direct stateful input manager for the Bevy game engine."
version = "0.13.3"
version = "0.14.0"
authors = ["Leafwing Studios"]
homepage = "https://leafwing-studios.com/"
repository = "https://github.com/leafwing-studios/leafwing-input-manager"
Expand Down
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## Version 0.14.0 (Unreleased)

### Breaking Changes

- Removed `Direction` type. Use `bevy::math::primitives::Direction2d`.

## Version 0.13.3

### Bugs
Expand Down
22 changes: 10 additions & 12 deletions examples/single_player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::{errors::NearlySingularConversion, orientation::Direction};

fn main() {
App::new()
Expand Down Expand Up @@ -44,12 +43,12 @@ impl ArpgAction {
ArpgAction::Right,
];

fn direction(self) -> Option<Direction> {
fn direction(self) -> Option<Direction2d> {
match self {
ArpgAction::Up => Some(Direction::NORTH),
ArpgAction::Down => Some(Direction::SOUTH),
ArpgAction::Left => Some(Direction::WEST),
ArpgAction::Right => Some(Direction::EAST),
ArpgAction::Up => Some(Direction2d::Y),
ArpgAction::Down => Some(Direction2d::NEG_Y),
ArpgAction::Left => Some(Direction2d::NEG_X),
ArpgAction::Right => Some(Direction2d::X),
_ => None,
}
}
Expand Down Expand Up @@ -133,14 +132,13 @@ fn player_dash(query: Query<&ActionState<ArpgAction>, With<Player>>) {
if action_state.pressed(&input_direction) {
if let Some(direction) = input_direction.direction() {
// Sum the directions as 2D vectors
direction_vector += Vec2::from(direction);
direction_vector += *direction;
}
}
}

// Then reconvert at the end, normalizing the magnitude
let net_direction: Result<Direction, NearlySingularConversion> =
direction_vector.try_into();
let net_direction = Direction2d::new(direction_vector);

if let Ok(direction) = net_direction {
println!("Dashing in {direction:?}");
Expand All @@ -150,7 +148,7 @@ fn player_dash(query: Query<&ActionState<ArpgAction>, With<Player>>) {

#[derive(Event)]
pub struct PlayerWalk {
pub direction: Direction,
pub direction: Direction2d,
}

fn player_walks(
Expand All @@ -165,13 +163,13 @@ fn player_walks(
if action_state.pressed(&input_direction) {
if let Some(direction) = input_direction.direction() {
// Sum the directions as 2D vectors
direction_vector += Vec2::from(direction);
direction_vector += *direction;
}
}
}

// Then reconvert at the end, normalizing the magnitude
let net_direction: Result<Direction, NearlySingularConversion> = direction_vector.try_into();
let net_direction = Direction2d::new(direction_vector);

if let Ok(direction) = net_direction {
event_writer.send(PlayerWalk { direction });
Expand Down
10 changes: 5 additions & 5 deletions src/axislike.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Tools for working with directional axis-like user inputs (game sticks, D-Pads and emulated equivalents)
use crate::buttonlike::{MouseMotionDirection, MouseWheelDirection};
use crate::orientation::{Direction, Rotation};
use crate::orientation::Rotation;
use crate::user_input::InputKind;
use bevy::input::{
gamepad::{GamepadAxisType, GamepadButtonType},
keyboard::KeyCode,
};
use bevy::math::primitives::Direction2d;
use bevy::math::Vec2;
use bevy::reflect::Reflect;
use bevy::utils::FloatOrd;
Expand Down Expand Up @@ -673,14 +674,13 @@ impl DualAxisData {
self.xy
}

/// The [`Direction`] that this axis is pointing towards, if any
/// The [`Direction2d`] that this axis is pointing towards, if any
///
/// If the axis is neutral (x,y) = (0,0), a (0, 0) `None` will be returned
#[must_use]
#[inline]
pub fn direction(&self) -> Option<Direction> {
// TODO: replace this quick-n-dirty hack once Direction::new no longer panics
(self.xy.length() > 0.00001).then(|| Direction::new(self.xy))
pub fn direction(&self) -> Option<Direction2d> {
Direction2d::new(self.xy).ok()
}

/// The [`Rotation`] (measured clockwise from midnight) that this axis is pointing towards, if any
Expand Down
Loading

0 comments on commit a645abf

Please sign in to comment.