Skip to content

Instant Buff Pickup Animation

raneechu edited this page Sep 12, 2021 · 2 revisions

Purpose

The float animation that occurs after a player picks up a HP booster/HP deplete buff. Common in games and helps the player keep track of what is happening when one of these buffs are collected.

Implementation

Key Components

  • BuffFactory Creates the floating animation as an Entity
  • ForestGameArea Holds the spawning function to spawn the animation onto the map
  • BuffManager Handles the spawning/despawning of the animation on the map
  • FloatTask Handles the floating movement of the animation

The BuffFactory class handles the instantiation of the Entity.

public static Entity createBuffAnimation(BuffManager.BuffPickup pickup, BuffManager manager) {
    /* Get the texture for the pickup */
    String texture = manager.getPickupTexture(pickup);
    AITaskComponent buffComponent = new AITaskComponent().addTask(new FloatTask(new Vector2(0f, 3f)));

    Entity buffPickup = new Entity().addComponent(new TextureRenderComponent(texture))
            .addComponent(new PhysicsComponent().setBodyType(BodyDef.BodyType.DynamicBody))
            .addComponent(new PhysicsMovementComponent())
            .addComponent(buffComponent);

The buffPickup entity has an AITaskComponent with a FloatTask, which allows the entity to float the range specified in the constructor, through utilising a MovementTask

@Override
public void start() {
    super.start();
    startPos = owner.getEntity().getPosition();
    movementTask = new MovementTask(getTarget());
    movementTask.create(owner);
    movementTask.start();
}

The different types of animations are stored in a LinkedHashMap with the key entry being the BuffPickup type and the second being a list of 2 integers. The first is whether the buff has been collided with and the second is whether the animation has spawned onto the map. Both use 0 to represent false and 1 to represent true.

private LinkedHashMap<BuffPickup, List<Integer>> buffPickups;

public BuffManager(MainGameScreen mainGame, GameArea currentMap) {
    this.buffPickups.put(BuffPickup.positive, Arrays.asList(0, 0));
    this.buffPickups.put(BuffPickup.negative, Arrays.asList(0, 0));
}

The values in the list are updated when a player collides with the right buff.

public void selectBuffFunctionality(BuffTypes type, Entity buff,
        BuffInformation buffInfo) {

    /* PlayerBuffs functions to call when there is a new instant buff */
    switch (type) {
        case B_HP_UP:
            this.buffPickups.get(BuffPickup.positive).set(0, 1);
            return;
}

They are also updated in the spawnPickup function, which handles the spawning and despawning of the animation and is called in the update function.

private void spawnPickup() {
    for (BuffPickup pickup: this.buffPickups.keySet()) {
        if (this.buffPickups.get(pickup).get(0) == 1) {
            pickupCreationTime = ServiceLocator.getTimeSource().getTime();
            buffPickup = mainGame.getCurrentMap().spawnBuffDebuffPickup(pickup, this);
            this.buffPickups.get(pickup).set(0, 0);
            this.buffPickups.get(pickup).set(1, 1);
        }
        if (this.buffPickups.get(pickup).get(1) == 1) {
            if (ServiceLocator.getTimeSource().getTimeSince(pickupCreationTime) >= 1.5 * SECONDS) {
                removeBuff(buffPickup);
                this.buffPickups.get(pickup).set(1, 0);
            }
        }
    }
}

public void update() {
    spawnPickup();
}

Usage

To add another animation type, add the type to the BuffPickup enum.

public enum BuffPickup {
    positive, negative
}

And in the constructor method, add the type into the LinkedHashMap

public BuffManager(MainGameScreen mainGame, GameArea currentMap) {
    this.buffPickups.put(BuffPickup.positive, Arrays.asList(0, 0));
}

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