Skip to content
This repository was archived by the owner on Apr 15, 2022. It is now read-only.

Handle input

Derek Detweiler edited this page Aug 16, 2017 · 4 revisions

Initial Setup

The game engine is set up to handle keyboard, mouse, and touch input. To plug up a controller, there are two components required to make it function:

  • HandlerController - This component should be placed at a scene's layer level and fires a controller message to all entities on that layer.
  • EntityController - This component should be attached to every entity that should listen for controller input events.

The first step is to identify the scene layers that should listen for input. This will likely include the action layer where the game world exists to control the hero, but it may also include GUI layers where touch controls may reside outside of the game world layer. Once the layers have been identified, add a HandlerController component to each layer's list of components. Ideally this component should be placed prior to any logic components (typically HandlerLogic) so that entity logic components will have the updated controller state from the current tick.

The second step is to identify all the child entities that expect input messages. A common entity listening for controller input would be the game hero, so adding an EntityController component may cause the entity definition to look something like this:

{
  "id": "hero",
  "components":[{
    "type": "EntityController"
  },{
    "type": "LogicDirectionalMovement"
  },{
    "type": "RenderDebug"
  }]
}

Keyboard Controls

Now that the two most important components are in place, the next step is to determine the controls. If we want our hero to walk left and right, we may want to map the left arrow to the hero walking left and the right arrow to the hero walking right. For the EntityController component, this is called a controlMap, mapping inputs to actions. The inputs we are listening for are the left and right arrow keys, and by looking at the messages that our LogicDirectionalMovement component is listening for, we know that we want to map these inputs to "go-left" and "go-right" messages. So our hero definition with the new control mappings may look something like this:

{
  "id": "hero",
  "components":[{
    "type": "EntityController",
    "controlMap": {
      "key:left-arrow": "go-left",
      "key:right-arrow": "go-right"
    }
  },{
    "type": "LogicDirectionalMovement"
  },{
    "type": "RenderDebug"
  }]
}

For a full list of key mappings check out the HandlerController key list.

Clone this wiki locally