GithubHelp home page GithubHelp logo

repcomm / gameinput-ts Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 138 KB

procedural input for javascript/typescript

Home Page: https://www.npmjs.com/package/@repcomm/gameinput-ts

TypeScript 99.53% HTML 0.47%
input remap html javascript lib npm typescript

gameinput-ts's Introduction

gameinput-ts

Runtime re-mappable procedural input for the web

current-support

  • keyboard
  • mouse
  • touch
  • gamepad
  • importing JSON input mappings during runtime!

future-support

  • UI elements
  • WebSocket API

how it works

The main idea:

  • Anything can be a button
  • Anything can be an axis

Anything being:

  • keyboard buttons
  • mouse buttons
  • mouse axes
  • touch
  • gamepad buttons
  • gamepad axes
  • HTML elements

Classes

  • Input (raw, hooks into browser APIs, used by GameInput)
  • GameInput - main API
  • Button - unique names, boolean state
  • Axis - unique names, floating point state

Using

Installing

  • Via NPM (usage with webpack or snowpack, which are tested)

``` npm install @repcomm/gameinput-ts ``` ```ts import { GameInput } from "@repcomm/gameinput-ts"; ``` - Browser ESM script:
```js <script type="module"> import { GameInput } from "/path/to/gameinput/mod.js"; </script> ```

Example Usage

//so we can use await
async function main () {
  
  //get the singleton
  let input = GameInput.get();
  
  //create an axis
  input.getOrCreateAxis ("forward")
  .addInfluence ({
    //value when triggered by boolean state (such as keys, mouse buttons, gamepad buttons)
    value: 1.0,
    
    //make it influenced by first mouse button
    mouseButtons:[0],
    
    //you can also make it take on the value of the mouse axes
    mouseAxes:[0],
    //scale factor when activated by mouse/touch
    pointerAxisScale: 0.5,
    
    //use gamepad axes!
    gpAxes:[0],
    //scale factor when activated by gamepad axes
    gpAxisScale: 2,
    //force use of specific connected gamepad
    gpIndex: 0
  });
  
  //You can also supply JSON!
  const inputMapFile = "./demo.input.json";
  console.log(`Fetching ${inputMapFile}`);
  let config = await (await fetch( inputMapFile )).json();

  //button and axis definitions will merge with pre-existing buttons and axes
  input.addJsonConfig( config );
  
  //loop
  let fps = 30;
  setInterval (()=>{
    //get the axis value
    let fwd = input.getAxisValue ("forward");
    
    console.log(fwd);
    
  }, 1000/fps);

}

main();

Compiling

To build you'll want to clone the repo
git clone https://github.com/RepComm/gameinput-ts.git

Install dev dependencies

npm install

Build

npm run build

gameinput-ts's People

Contributors

repcomm avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

gameinput-ts's Issues

Configurable dead zones for gamepad interface

Having axis mapped to both keyboard, touch, and gamepad will mean that gamepads can have their dead zones interfere with setting the input value.

Things that can be done to mitigate this:

  • get highest value from all the inputs of an axis, return that
  • add dead zone configuration per gamepad axis (and possibly touch as well)

Axis should be mappable to any set of inputs

Currently, axes are only mapped to gamepad axes, but mouse wheel, and mobile touch axes (not implemented as of yet), and even keyboard buttons should be able to be mapped to axes.

Something like this for keyboards:

GameInput.get().createAxis ("fwd-bwd")
.addKey("w", 1.0)
.addKey("s", -1.0)
.addGamepadAxis (13);

Possible name change

I've used this project is way more apps than games at this point,
I'm thinking of renaming to something like procedural-web-input or something similar.

Unfortunately I'd have to do annoying things to the repo and npm install location,
not thrilled about that.

Next update

A major API change will occur in the next version

In the next version, all glue code will be removed.

Two kinds of inputs:

  • button
  • axis

Anything can be an axis, anything can be a button.
All mappings will be runtime configurable
Mappings will be able to be saved and loaded from JSON

DOM events can also be hooked to buttons and axis, which will solve the mobile touch button problem.

A package will be created using exponent-ts and gameinput-ts as dependencies, providing
a user input mapping panel which can be mounted anywhere in the DOM at any time, and can have events fired to trigger an input manager instance (or several) changed to match.

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.