Skip to content

Commit

Permalink
feat: input movement axis 🎮
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Dec 13, 2023
1 parent 5d95fe0 commit 2716595
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 95 deletions.
27 changes: 18 additions & 9 deletions examples/jump.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::too_many_arguments)]

use std::cmp::Ordering;

use bevy::{
prelude::*,
sprite::MaterialMesh2dBundle,
Expand All @@ -10,8 +12,9 @@ use hello_bevy::{
GameOptions,
GamePlugin,
GameState,
Keybind,
KeyBind,
Keybinds,
Movement,
};

const SIZE: Vec2 = Vec2::new(600., 600.);
Expand Down Expand Up @@ -118,10 +121,11 @@ fn init_sample(

fn update_sample(
time: Res<Time>,
input: Res<Input<KeyBind>>,
movement: Res<Movement>,
keybinds: Res<Persistent<Keybinds>>,
mut objects: Query<(&mut Player, &mut Transform)>,
mut counter: Query<(&mut Text, &mut Counter)>,
input: Res<Input<Keybind>>,
keybinds: Res<Persistent<Keybinds>>,
) {
for (mut player, mut trans) in objects.iter_mut() {
let t = &mut trans.translation;
Expand All @@ -148,15 +152,20 @@ fn update_sample(
}

// Move
if keybinds.left.iter().any(|bind| input.pressed(*bind)) {
player.velocity.x = -MOVE_VEL;
} else if keybinds.right.iter().any(|bind| input.pressed(*bind)) {
player.velocity.x = MOVE_VEL;
} else if player.velocity.x.abs() > MOVE_CUTOFF {
let vel = keybinds
.x_axis
.iter()
.map(|bind| movement.get(*bind))
.sum::<f32>()
.clamp(-1., 1.);

player.velocity.x = vel * MOVE_VEL;

/*if player.velocity.x.abs() > MOVE_CUTOFF {
player.velocity.x *= MOVE_FACTOR;
} else {
player.velocity.x = 0.;
}
}*/

// Update position based on velocity and add bounds
*t += player.velocity.extend(0.) * time.delta_seconds();
Expand Down
73 changes: 39 additions & 34 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use serde::{
};

use crate::{
input::Keybind,
input::{
KeyBind,
MoveBind,
},
ui::{
FONT_MULTIPLIERS,
FONT_SIZES,
Expand Down Expand Up @@ -91,20 +94,25 @@ pub struct GameOptions {

#[derive(Resource, Serialize, Deserialize, Reflect)]
pub struct Keybinds {
pub up: Vec<Keybind>,
pub down: Vec<Keybind>,
pub left: Vec<Keybind>,
pub right: Vec<Keybind>,
pub jump: Vec<Keybind>,
pub interact: Vec<Keybind>,
pub inventory: Vec<Keybind>,
pub pause: Vec<Keybind>,
pub x_axis: Vec<MoveBind>,
pub y_axis: Vec<MoveBind>,
pub jump: Vec<KeyBind>,
pub interact: Vec<KeyBind>,
pub inventory: Vec<KeyBind>,
pub pause: Vec<KeyBind>,
}

impl Keybinds {
pub fn all(&self) -> Vec<&Keybind> {
pub fn keys(&self) -> Vec<&KeyBind> {
self.iter_fields()
.filter_map(|f| f.downcast_ref::<Vec<Keybind>>())
.filter_map(|f| f.downcast_ref::<Vec<KeyBind>>())
.flatten()
.collect()
}

pub fn moves(&self) -> Vec<&MoveBind> {
self.iter_fields()
.filter_map(|f| f.downcast_ref::<Vec<MoveBind>>())
.flatten()
.collect()
}
Expand All @@ -113,38 +121,32 @@ impl Keybinds {
impl Default for Keybinds {
fn default() -> Self {
Self {
up: vec![
Keybind::Key(KeyCode::W),
Keybind::Gamepad(GamepadButtonType::DPadUp),
],
down: vec![
Keybind::Key(KeyCode::S),
Keybind::Gamepad(GamepadButtonType::DPadDown),
x_axis: vec![
MoveBind::KeyAxis(KeyCode::D, KeyCode::A),
MoveBind::Gamepad(GamepadAxisType::LeftStickX),
// MoveBind::MouseAxis(MouseAxis::X),
],
left: vec![
Keybind::Key(KeyCode::A),
Keybind::Gamepad(GamepadButtonType::DPadLeft),
],
right: vec![
Keybind::Key(KeyCode::D),
Keybind::Gamepad(GamepadButtonType::DPadRight),
y_axis: vec![
MoveBind::KeyAxis(KeyCode::W, KeyCode::S),
MoveBind::Gamepad(GamepadAxisType::LeftStickY),
// MoveBind::MouseAxis(MouseAxis::Y),
],
jump: vec![
Keybind::Key(KeyCode::Space),
Keybind::Gamepad(GamepadButtonType::South),
KeyBind::Key(KeyCode::Space),
KeyBind::Gamepad(GamepadButtonType::South),
],
interact: vec![
Keybind::Key(KeyCode::E),
Keybind::Mouse(MouseButton::Left),
Keybind::Gamepad(GamepadButtonType::East),
KeyBind::Key(KeyCode::E),
KeyBind::Mouse(MouseButton::Left),
KeyBind::Gamepad(GamepadButtonType::East),
],
inventory: vec![
Keybind::Key(KeyCode::Tab),
Keybind::Gamepad(GamepadButtonType::West),
KeyBind::Key(KeyCode::Tab),
KeyBind::Gamepad(GamepadButtonType::West),
],
pause: vec![
Keybind::Key(KeyCode::Escape),
Keybind::Gamepad(GamepadButtonType::Start),
KeyBind::Key(KeyCode::Escape),
KeyBind::Gamepad(GamepadButtonType::Start),
],
}
}
Expand All @@ -160,6 +162,9 @@ fn init_persistence(mut cmd: Commands) {
#[cfg(target_arch = "wasm32")]
let config_dir = Path::new("local");

// Append the package name to have unique configs (especially on web)
let config_dir = config_dir.join(env!("CARGO_PKG_NAME"));

cmd.insert_resource(
Persistent::<GameOptions>::builder()
.name("options")
Expand Down
Loading

0 comments on commit 2716595

Please sign in to comment.