Skip to content

Commit

Permalink
Experimentation with action value API
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Feb 10, 2022
1 parent 602c5ba commit 76b9d15
Showing 1 changed file with 24 additions and 52 deletions.
76 changes: 24 additions & 52 deletions examples/single_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ fn main() {
.run();
}

#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug)]
#[derive(Actionlike, PartialEq, Clone, Copy, Debug)]
enum ArpgAction {
// Movement
Up,
Down,
Left,
Right,
Movement(Direction),
// Abilities
Ability1,
Ability2,
Expand All @@ -36,26 +33,6 @@ enum ArpgAction {
Ultimate,
}

impl ArpgAction {
// Lists like this can be very useful for quickly matching subsets of actions
const DIRECTIONS: [Self; 4] = [
ArpgAction::Up,
ArpgAction::Down,
ArpgAction::Left,
ArpgAction::Right,
];

fn direction(self) -> Direction {
match self {
ArpgAction::Up => Direction::NORTH,
ArpgAction::Down => Direction::SOUTH,
ArpgAction::Left => Direction::EAST,
ArpgAction::Right => Direction::WEST,
_ => Direction::NEUTRAL,
}
}
}

#[derive(Component)]
pub struct Player;

Expand All @@ -70,8 +47,7 @@ struct PlayerBundle {

impl PlayerBundle {
fn default_input_map() -> InputMap<ArpgAction> {
// This allows us to replace `ArpgAction::Up` with `Up`,
// significantly reducing boilerplate
// This allows us to significantly reducing boilerplate
use ArpgAction::*;
let mut input_map = InputMap::default();

Expand All @@ -81,17 +57,18 @@ impl PlayerBundle {
input_map.set_gamepad(Gamepad(0));

// Movement
input_map.insert(Up, KeyCode::Up);
input_map.insert(Up, GamepadButtonType::DPadUp);
// FIXME: this doesn't work
input_map.insert(Movement(Direction::NORTH), KeyCode::Up);
input_map.insert(Movement(Direction::NORTH), GamepadButtonType::DPadUp);

input_map.insert(Down, KeyCode::Down);
input_map.insert(Down, GamepadButtonType::DPadDown);
input_map.insert(Movement(Direction::SOUTH), KeyCode::Down);
input_map.insert(Movement(Direction::SOUTH), GamepadButtonType::DPadDown);

input_map.insert(Left, KeyCode::Left);
input_map.insert(Left, GamepadButtonType::DPadLeft);
input_map.insert(Movement(Direction::EAST), KeyCode::Left);
input_map.insert(Movement(Direction::EAST), GamepadButtonType::DPadLeft);

input_map.insert(Right, KeyCode::Right);
input_map.insert(Right, GamepadButtonType::DPadRight);
input_map.insert(Movement(Direction::WEST), KeyCode::Right);
input_map.insert(Movement(Direction::WEST), GamepadButtonType::DPadRight);

// Abilities
input_map.insert(Ability1, KeyCode::Q);
Expand Down Expand Up @@ -137,15 +114,14 @@ fn player_dash(query: Query<&ActionState<ArpgAction>, With<Player>>) {
let action_state = query.single();

if action_state.just_pressed(ArpgAction::Ability4) {
let mut direction = Direction::NEUTRAL;

for input_direction in ArpgAction::DIRECTIONS {
if action_state.pressed(input_direction) {
direction += input_direction.direction();
}
// The internal `Direction::NEUTRAL` value is irrelevant;
// we merely need to get the information from the data
// FIXME: this is extremely ugly and unintuitive.
if let ArpgAction::Movement(direction) =
action_state.action_value(ArpgAction::Movement(Direction::NEUTRAL))
{
println!("Dashing in {direction:?}");
}

println!("Dashing in {direction:?}");
}
}

Expand All @@ -159,15 +135,11 @@ fn player_walks(
) {
let action_state = query.single();

let mut direction = Direction::NEUTRAL;

for input_direction in ArpgAction::DIRECTIONS {
if action_state.pressed(input_direction) {
direction += input_direction.direction();
if let ArpgAction::Movement(direction) =
action_state.action_value(ArpgAction::Movement(Direction::NEUTRAL))
{
if direction != Direction::NEUTRAL {
event_writer.send(PlayerWalk { direction });
}
}

if direction != Direction::NEUTRAL {
event_writer.send(PlayerWalk { direction });
}
}

0 comments on commit 76b9d15

Please sign in to comment.