Skip to content

Powerups

connor-golin edited this page Aug 31, 2023 · 30 revisions

Overview

Powerups create a more fun and engaging experience by granting the user enhanced abilities, each with a unique duration and effect. Powerups are commonly found in random locations within the map and can be picked up with the 'F' key.

Health Powerup: Restores the player to full health.

Movement Powerup: Increases the player's default movement speed by 66% for 1.5 seconds.

Usage

Powerups can be treated as regular Entities and therefore have Components as well as a scale and position within the game world (See Entities for more details). All created Powerups are automatically registered with the EntityService and can be found in an array with the following call:

ServiceLocator.getEntityService().getEntitiesByComponent(PowerupComponent.class);

Example - Spawning Powerups

private void spawnPowerups() {
    // Choose two random locations within the game map
    GridPoint2 minPos = new GridPoint2(0, 0);
    GridPoint2 maxPos = terrain.getMapBounds(0).sub(2, 2);
    GridPoint2 healthPosition = RandomUtils.random(minPos, maxPos);
    GridPoint2 speedPosition = RandomUtils.random(minPos, maxPos);

    // Create a health and speed powerup
    Entity healthPowerup = PowerupFactory.createHealthPowerup();
    Entity speedPowerup = PowerupFactory.createSpeedPowerup();

    // Spawn the created powerups at the two random locations
    spawnEntityAt(healthPowerup, healthPosition, true, false);
    spawnEntityAt(speedPowerup, speedPosition, true, false);
  }
Clone this wiki locally