Skip to content

Latest commit

ย 

History

297 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ @benev/tact

from keypress to couch co-op

npm install @benev/tact

tact is a toolkit for handling user input on the web.
customizable keybindings, multiple-gamepad couch co-op, ready-made configurator ui, efficient networking, mobile virtual thumbsticks.


๐ŸŽฎ tact basics

import {
  Devices, KeyboardDevice, PointerDevice,
  makeIntentsResolver, makeActionsResolver,
} from "@benev/tact"
  1. setup your game's keybindings
    const bindings = {
      running: {forward: "KeyW", jump: ["and", "ShiftLeft", "Space"]},
      gunning: {shoot: ["or", "pointer.button.left", "gamepad.trigger.right"]},
    }
  2. create your devices (which provide raw input samples)
    const devices = new Devices(
      new KeyboardDevice(),
      new PointerDevice(),
    )
  3. make your resolvers
    const resolveIntents = makeIntentsResolver(bindings)
    const resolveActions = makeActionsResolver(bindings)
  4. run your resolvers, read your actions
    import {cycle} from "@e280/stz"
    
    cycle(function myGameTick() {
    
      // ๐Ÿง™โ€โ™‚๏ธ samples get resolved into compact intent tuples, good for networking.
      const intents = resolveIntents(Date.now(), devices.samples())
    
      // ๐Ÿง™โ€โ™‚๏ธ actions are your ergonomic accessors for values.
      const actions = resolveActions(intents)
    
      // ๐Ÿง™โ€โ™‚๏ธ okay, now check your actions.
      actions.running.forward.value // 1.0
      actions.running.forward.pressed // true
    })

๐Ÿง™โ€โ™‚๏ธ wizardly tips

  1. for networking, you should encode intents as Uint8Array for better efficiency down the wire.
    import {encodeIntents, decodeIntents} from "@benev/tact"
    
    const intents = [[1, 0.7853981633974483]]
    
    console.log(JSON.stringify(intents).length)
      // 22 โ€” yikes
    
    const bytes = encodeIntents(intents)
    
    console.log(bytes.length)
      // 6 โ€” noice
    
    const intents = decodeIntents(bytes)
  2. you can normalize bindings
    import {normalizeBindings} from "@benev/tact"
    
    const bindings = normalizeBindings(appBindings, susBindings)
    this clones your app bindings and cherry-picks any compatible user bindings. useful when loading from localStorage, so changes to your schema donโ€™t break things.



๐Ÿ‘ผ https://benev.gg/

About

๐ŸŽฎ keybindings and couch co-op

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages