Skip to content

Player Sprinting Functionality

Sebastian edited this page Aug 22, 2021 · 8 revisions

Purpose

A sprint feature is a mechanic in the game. Whilst it is easy to use, the timing of the sprint feature might become crucial when the game gets more difficult, as sprint is a limited resource.

Usage

While in game, if the left-shift key is pressed, the player's movement will be increased and the sprint gauge (bellow the health value) will decrease. If the player is standing still and decides to sprint, nothing will happen and as long as the left-shit key is held down, the sprint gauge will decrease. Currently, the sprint gauge cannot be regenerated. However, in the future this might be possible when the player collects a power up, etc.

Implementation

The SprintComponent class was made, and was added to the PlayerFactory class:

.addComponent(new SprintComponent(100));

Within the KeyboardPlayerInputComponent class, logic for a sprint event was added:

private void triggerSprintEvent(boolean sprinting) {
    if (entity.getComponent(SprintComponent.class).getSprint() == 0){
      return;
    }
   if (walkDirection.epsilonEquals(Vector2.Zero)) {
     entity.getEvents().trigger("walkStop");
    } else {
      entity.getEvents().trigger("sprint", walkDirection, sprinting);
   }
  }

Currently, sprint works by adding another movement vector in whichever way the player is moving, this can most definitely be improved by scaling the current movement vector, but that caused issues when the player would suddenly change direction mid-sprint, so it was implemented as so:

void sprint(Vector2 direction, boolean sprinting){
      if (direction.x > 0){
        //if the player is moving right
        if (sprinting){
          //if sprint was called on keyDown, increase speed
          this.walkDirection.add(Vector2Utils.RIGHT);
        } else {
          //player has stopped sprinting, subtract speed
          this.walkDirection.sub(Vector2Utils.RIGHT);
        }
      }
      if (direction.x < 0){
        //if the player is moving left
        if (sprinting){
          this.walkDirection.add(Vector2Utils.LEFT);
        } else {
          this.walkDirection.sub(Vector2Utils.LEFT);
        }
      }
  }

Another issue that caused problems was when the player would hit the sprint key first, and then start moving. This was fixed (however there are probably better solutions) by checking this at every keyDown / keyUp event. For example:

case Keys.A:
        if (entity.getComponent(SprintComponent.class).getSprint() > 0 && isSprinting){
          //if sprint is active before moving, add twice the speed
          walkDirection.add(Vector2Utils.LEFT);
          walkDirection.add(Vector2Utils.LEFT);
          triggerWalkEvent();
          return true;
        }

A timer was used to deplete the sprint amount at a certain rate (as long as the player is sprinting):

public Timer timer = new Timer();
  public Timer.Task removeSprint = new Timer.Task() {

    @Override
    public void run() {
      entity.getComponent(SprintComponent.class).removeSprint(1);
      if (entity.getComponent(SprintComponent.class).getSprint() == 0){
        //if sprint has fully depleted
        if (walkDirection.x > 1){
          walkDirection.sub(Vector2Utils.RIGHT);
        }
        if (walkDirection.x < -1){
          walkDirection.sub(Vector2Utils.LEFT);
        }
        timer.stop();
        isSprinting = false;
      }
    }
  };

Finally, the sprint amount was added on to the UI, similar to the way the health was added:

int sprint = entity.getComponent(SprintComponent.class).getSprint();
CharSequence sprintText = String.format("Sprint: %d", sprint);
sprintLabel = new Label(sprintText, skin, "large");

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