GithubHelp home page GithubHelp logo

hassle-input's Introduction

Hassle::Input

This is a reference Input Management system. It is designed to make it easy to bind and rebind gamepad, keyboard, mouse and touch controls to named actions. Using actions instead of direct controls allows the developer to decouple from the actual keys used, making it easier to configure and change inputs both in code and at runtime.

Example Setup

Hassle Input has enums for standard mouse buttons, keys and gamepad buttons. Your responsibility is to define your game's actions. There are three types of actions. Digital actions are binary, they're either 1 or 0. These are used for keyboard buttons, mouse buttons etc. Analog actions are numbers and are useful for capturing thumbsticks, mouse movement etc. Touch actions are a little strange and give an array of Touch objects which have an 'active' status and the same normalized x/y coords as the mouse axes. The number of these is set by the amount the system supports, i.e, 0 for a none touch device, 1 for messing around with Mobile mode in Chrome Devtools and more for a real touch device.

In general actions are a list of all the interactions that a player can make with your game. After you have defined your list of actions, the next step is to make an InputBindings object. A valid input bindings object has three main children, gamepad, mouse,keys and a special touch category

import { MouseButtons, MouseActions, Keys, InputBindings } from 'Input/types' 

enum DigitalActions {
    Use,
    Examine,
    WalkLeft,
    WalkRight,
}

enum AnalogActions {
    MouseX,
    MouseY,
    MoveLeft,
    MoveRight,
}

// we don't need any touch actions so we leave this empty
enum TouchActions {
    Touch
}

const bindings: InputBindings<DigitalActions, AnalogActions, TouchActions> = {
    gamepad: {
        axis: {
            [GamepadAxis.LX]: AnalogActions.MoveLeft,
            [GamepadAxis.LY]: AnalogActions.MoveRight,
        },
        buttons
    },
    mouse: {
        axis: {
            [MouseAxis.X]: AnalogActions.MouseX,
            [MouseAxis.Y]: AnalogActions.MouseY,
        },
        buttons: {
            [MouseButtons.Left]: DigitalActions.Interact,
            [MouseButtons.Right]: ButtonActions.Examine,
        }
    },
    keys: {
        axis: null,
        buttons: {
            [Keys.Right]: DigitalActions.WalkRight,
            [Keys.Left]: DigitalActions.WalkLeft,
            [Keys.Space]: DigitalActions.Interact,
            []
        }
    },
    touch: {

    }
}

const inputs = new InputManager<DigitalActions, AnalogActions, TouchActions>();
inputs.bindActions(bindings);

In addition if you want to use the mouse cursor or touch events (and want to know when the mouse is over your game) you need to pass in a DOM element for binding to.

inputs.setMouseAndTouchActiveElement(document.getElementById('canvas'));

Integrating

Initalize:

// initialize the input system
inputs.initialize();

Each tick (via setInterval or requestAnimationFrame)

    // every tick of your game...
    inputs.update();
    // then...
    return inputs.getActionStatus();

When you're done..

inputs.destroy();

Running the test page

This has been mostly set up for running in Electron.

yarn install
yarn testpage
electron ./TestPage/main.js

NPM Package?

I'm using this as a submodule atm so not a priority, but eventually, if dev continues.

hassle-input's People

Contributors

charlottegore avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.