Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action->Input mapping #40

Open
musjj opened this issue Sep 30, 2023 · 2 comments
Open

Action->Input mapping #40

musjj opened this issue Sep 30, 2023 · 2 comments
Labels
nice-to-have Things which are lower priority right now until other pressing issues are fixed.

Comments

@musjj
Copy link

musjj commented Sep 30, 2023

It would be nice to have Action->Input mapping, so you can map multiple types of inputs to one action. We can then simply check if that action is being triggered.

For example we can map the left, A keyboard key and the left gamepad key to the left movement.

Some inspirations:

@darthdeus
Copy link
Owner

I agree this would be useful for remappable inputs, but I don't yet have a clear idea how this should work. If we ignore gamepads this is very simple, but with gamepad inputs it might be a bit tricky to do this in a general way?

Happy to hear thoughts, but my current opinion is that comfy should probably provide a simple way for mapping discrete actions, while leaving more complicated things up to the user. What I'd really want to avoid is some kind of arcane system that tries to address everything while not really being used fully by anyone and that doesn't cover the actual use cases people want.

@darthdeus darthdeus added the nice-to-have Things which are lower priority right now until other pressing issues are fixed. label Oct 1, 2023
@nokola
Copy link
Contributor

nokola commented Oct 5, 2023

Perhaps having an example in /examples would be enough - at least as a start.

I might put something in there once I do kbd + gamepad support for my game.

This is what I currently have, and it's working quite well. kbd only for now. Allows for easy remapping and the game logic is completely separate from the input logic. Easy to add more devices rather than just kbd without changing any game code.

The input can even come from AI.

Game code is input independent:

pub struct MeleeInput {
    /// rotation, from -1 to 1
    pub turn: f32,

    pub fire_pressed: bool,
    pub fire_held: bool,
    pub fire_released: bool,

    pub special_pressed: bool,
    pub special_held: bool,
    pub special_released: bool,

    pub thrust_pressed: bool,
    pub thrust_held: bool,
    pub thrust_released: bool,

    pub reverse_pressed: bool,
    pub reverse_held: bool,
    pub reverse_released: bool,
}

impl MeleeInput {
    /// Returns a forward or backward thrust impulse based on whether thrusters or reverse thrusters are held
    /// Used to move ship forward/backward as needed
    pub fn thrust(&self) -> f32 {
        if (self.thrust_held && self.reverse_held) || (!self.thrust_held && !self.reverse_held) {
            return 0.0;
        }

        if self.thrust_held {
            1.0
        } else {
            -1.0
        }
    }
}

Keymaps and processing once per frame:

pub struct KeyMap {
    turn_left: KeyCode,
    turn_right: KeyCode,
    fire: KeyCode,
    special: KeyCode,
    thrust: KeyCode,
    reverse: KeyCode,
}

... // called during setup/settings
            player1_controls: KeyMap {
                turn_left: KeyCode::A,
                turn_right: KeyCode::D,
                fire: KeyCode::Space,
                special: KeyCode::E,
                thrust: KeyCode::W,
                reverse: KeyCode::S,
            },
            player2_controls: KeyMap {
                turn_left: KeyCode::Left,
                turn_right: KeyCode::Right,
                fire: KeyCode::KpEnter,
                special: KeyCode::M,
                thrust: KeyCode::Up,
                reverse: KeyCode::Down,
            },


// called every frame
fn player_input(controls: &KeyMap) -> MeleeInput {
    MeleeInput {
        turn: if is_key_down(controls.turn_left) {
            0.5
        } else if is_key_down(controls.turn_right) {
            -0.5
        } else {
            0.0
        },
        fire_pressed: is_key_pressed(controls.fire),
        fire_held: is_key_down(controls.fire),
        fire_released: is_key_released(controls.fire),
        special_pressed: is_key_pressed(controls.special),
        special_held: is_key_down(controls.special),
        special_released: is_key_released(controls.special),
        thrust_pressed: is_key_pressed(controls.thrust),
        thrust_held: is_key_down(controls.thrust),
        thrust_released: is_key_released(controls.thrust),
        reverse_pressed: is_key_pressed(controls.reverse),
        reverse_held: is_key_down(controls.reverse),
        reverse_released: is_key_released(controls.reverse),
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nice-to-have Things which are lower priority right now until other pressing issues are fixed.
Projects
None yet
Development

No branches or pull requests

3 participants