Skip to content

KeyboardPlayerInputComponent & Player Sprint Refactor

Sebastian edited this page Sep 14, 2021 · 4 revisions

Purpose

Since more classes now rely on KeyboardPlayerInputComponent, (such as PlayerStateComponent) a general clean-up of the code written is necessary, to improve readability and also allow other classes to use KeyboardPlayerInputComponent more easily.

Additionally, Sprint will be changed to rely upon a SPRINT_MODIFIER, which means that the sprint feature can be easily changed when under the effect of buffs or debuffs, etc..

Implementation

Sprinting Changes

A sprint modifier variable was added:

/**
   * SPRINT MODIFIER - This can be changed (when buffed, etc.)
   * Change this value to control how fast the player moves whilst under the effect of sprint
   */
  public static int SPRINT_MODIFIER = 2;

Sprint now relies upon this value, which is seen in the below implementation:

this.walkDirection.add(Vector2Utils.RIGHT.cpy().scl(SPRINT_MODIFIER));

KeyboardPlayerInputComponent Changes

On all keyUp and keyDown situations, the following method is called:

private boolean handleWalk(char Key, String keyState);

This method factors out large chucks of code, and makes the keyUp and keyDown methods far more readable:

public boolean keyDown(int keycode) {

    switch (keycode) {
      case Keys.SPACE:
        return jump();
      case Keys.A:
        return handleWalk('A', "DOWN");
      case Keys.D:
        return handleWalk('D', "DOWN");
      case Keys.SHIFT_LEFT:
          return handleSprint(true);
      default:
        return false;
    }
  }

Where handleWalk() is simply the factored-out sprint & walk logic at any keyDown or keyUp event.

Table of Contents

Home

Introduction

Main Menu

Main Game Screen

Gameplay

Player Movement

Character Animations

Enemy Monster Design and Animations

Game basic functionalities

User Testing

GitHub Wiki Tutorial

Game Engine

Getting Started

Documentation

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