Skip to content

Input Handling Overview

polyrain edited this page Aug 1, 2021 · 1 revision

Introduction

Note: This page only includes the Introduction and Behind the Scenes for input Handling. The 'Usage' section can be viewed in Using the Input System.

The Input System is used to process inputs from the user, e.g. mouse movements, keyboard presses, touch interactions, etc.

Currently there are two input types recognised within the game: KEYBOARD and TOUCH. Both use keyboard and mouse input, however, if you'd like to also support more input types, for example touch gestures or remote controller input, then this should provide a good foundation.

Key System Components

  1. Input Factory - Implements the 'Abstract Factory Pattern' to create type-specific input factories: InputFactory.
  2. Type-specific Input Factory - Creates input handlers of the matching input type, for example: KeyboardInputFactory, TouchInputFactory.
  3. Input Handler - Components that can process given inputs, for example: KeyboardPlayerInputComponent, TouchPlayerInputComponent.
  4. Input Service - Iterates through registered input handlers until the input is processed: InputService.

Creating Input Handlers

The Input System uses the Abstract Factory pattern to create input handlers that can process the received input. For example, if the user is using a keyboard and mouse, then the active input handlers should be able to process keyboard and mouse input; or, if the user is using a game controller, then the active input handlers should be able to process game controller input. The Abstract Factory pattern is quite a complex pattern, but is necessary for ensuring our code remains modular each time a new input-type is added.

Architecture

Processing Input

The Input Service is set as the game's input processer which means it will receive all user input. All active input handlers must register themselves with the Input Service and provide a priority level. The Input Service maintains a list of the registered input handlers, sorted by descending priority. When input is received, the input service iterates through the list of input handlers until the input is processed.

Example

There are three input handlers 1, 2 and 3, with priority orders 10, 5 and 1, respectively. The input can be handled by input handlers 2 and 3.

The user has pressed down a key. The Input Service sends the input to the input handlers ascending priority order.

Input handler 1 cannot process the input, so it is passed to input handler 2. The input is processed by input handler 2, and therefore is not passed on to input handler 3.

Why do we want this?

It is important that we are able to control the order in which input handlers are called in and stop calling input handlers when the input is handled.

Let's consider the case where we have our input type set to KEYBAORD and we press the 'w' key within our game. The 'w' key currently has two uses, firstly it can be typed into the terminal, and secondly it can be used to move our player up.

If the terminal is open, we want 'w' to be typed into the terminal, and if the terminal is closed, we want 'w' to control the player movement. Based on this logic, we want the terminal to have a higher priority than the player so that it gets to choose whether it handles the input or not based on its state, and if it doesn't then the player can attempt to.

This explains why we need input handler ordering, but why do we need a way to stop passing the input to input handlers?

Consider the case where we've just typed 'w' and it's appeared in the terminal. If this input was then also handled by the player, as we typed, we'd see the player in the background moving around too. This is not intended behaviour, so we need a way to prevent handled input from being passed to any other input handlers.

To do this, we make each input handler return a boolean: true, if the input has been handled, and false if it hasn't. Now the InputService which is passing the input to the input handlers, knows when to stop.

Behind the Scenes

Why the Abstract Factory pattern?

To understand why this pattern is necessary, let's consider some the most basic way we could write code for moving the player up and down with a keyboard:

if (keycode == Input.Keys.W) {
  player.moveUp();
} else if (keycode == Input.Keys.S) {
  player.moveDown();
} 

If we now want to support a game controller, we would have to extend it like so:

if (inputType == KEYBOARD) {
  if (input == 'W') {
    player.moveUp();
  } else if (input == 'S') {
    player.moveDown();
  }
} else if (inputType == GAME_CONTROLLER) {
  if (input == 'Y') {
    player.moveUp();
  } else if (input == 'A') {
    player.moveDown();
  }
}

You can see how the code will grow very quickly as soon as we try to add more functionality or more input types.

The Abstract Factory pattern allows us to abstract away input-specific code into the different type-specific InputComponent files. This means for every input handler we write we only have to worry about one input type, making each input handler's code much simpler.

Another benefit of the Abstract Factory pattern is that it allows us to write any code interacting with input handler's to be input type agnostic. Let's consider the player as an example.

As an example, when we create the player's input component, we use the InputFactory stored within the InputService.

 InputComponent inputComponent = ServiceLocator.getInputService().getInputFactory().createForPlayer();

Notice that we don't need to declare whether we want the KeyboardPlayerInputComponent or the TouchPlayerInputComponent. This is already done for us behind the scenes.

Gameplay

Home

Main Character

πŸ‘Ύ Obstacle/Enemy

Food & Water

Pickable Items

Game achievements

Game Design

Emotional Goals

Game Story

Influences

Style

Pixel Grid Resolution

Camera Angle and The Player's Perspective

Features Design

Achievements Screen

Achievements Trophies and Cards

Music Selection GUI

πŸ‘Ύ Obstacle/Enemy

 Monster Manual
 Obstacles/Enemies
  - Alien Plants
  - Variation thorns
  - Falling Meteorites
  - FaceHugger
  - AlienMonkey
 Spaceship & Map Entry
 Particle effect

Buffs

Debuffs

Buffs and Debuffs manual

Game Instruction

[code for debuff animations](code for debuff animations)

Infinite loop game system

Main Menu Screen

New Setting Screen

Hunger and Thirst

Goals and Objectives

HUD User Interface

Inventory System

Item Bar System

Scoring System

Props store

BGM design

Sound Effect design

Main game interface

Invisible ceiling

New terrain sprint 4

New game over screen sprint 4

Code Guidelines

Main Character Movement, Interactions and Animations - Code Guidelines

Item Pickup

ItemBar & Recycle system

Main Menu Button Code

Game Instructions Code

πŸ‘Ύ Obstacle/Enemy

 Obstacle/Enemy
 Monster Manual
 Spaceship Boss
 Particle effects
 Other Related Code
 UML & Sequence diagram of enemies/obstacles

Scoring System Implementation Explanation

Music Implementation

Buff and Debuff Implementation

Score History Display

code improvement explanation

Infinite generating terrains Implementation Explanation

Game Over Screen and functions explaination

Buffer timer before game start

Scrolling background

Multiple Maps

Invisible ceiling

Rocks and woods layout optimization

Magma and nails code implementation

Background Music Selection

Chooser GUI Implementation

Chooser GUI Logic Persistence

Guide: Adding Background music for a particular screen

Achievements Ecosystem - Code Guidelines

Achievements System

Achievements Screen

Adding Achievements (Guide)

Game Records

DateTimeUtils

History Scoreboard - Score Details

Listening for important events in the Achievements ecosystem

Food and Water System

Food System Water System

Hunger and Thirst icon code guidelines

Asset Creation

In Game Background Music

User Testing

Hunger and Thirst User Testing

Main Character Testing

Buffs and Debuffs Testing

Buff and Debuff Manual User Testing

Game Instruction User Testing

The Main Menu User Test

The New Button User Test in Setting Page

The Main Menu Buttons User Testing

Hunger and Thirst User Test

Infinite loop game and Terrain Testing

Item Bar System Testing

Randomised Item Drops

Recycle System Testing

Scoring System Testing

Music User test

https://github.com/UQdeco2800/2021-ext-studio-2.wiki.git

πŸ‘Ύ Obstacle/Enemy

 Obstacle testing
  - Alien Plants & Variation Thorns
  - Falling Meteorites
 Enemy testing
  - Alien Monkeys & Facehugger
  - Spaceship Boss
 Monster Manual
 Particle-effect
 Player attack testing
  - Player Attack

Inventory system UI layout

Props store user testing

Achievements User Testing

Sprint 1

Sprint 2

Sprint 3

Sprint 4

Items testing

Player Status testing

Changeable background & Buffer time testing

Main game interface test

Invisible ceiling test

Game over screen test sprint 4

New terrain textures on bonus map test sprint 4

Buying Props User Testing

Testing

Hunger and Thirst Testing

Main Character Player

Achievements System, Game Records and Unlockable Chapters

DateTimeUtils Testing

Scoring System Testing Plan

Distance Display Testing Plan

Musics Implementation Testing plan

History Board Testing plan

Rocks and woods testing plan

Sprint 4 terrain tests

Items

Item Bar System Testing Plan

Recycle System Testing Plan

Game Engine

Game Engine Help

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally