Skip to content

Split Moblings

Chia-Chuan Tsou (Jason) edited this page Sep 29, 2023 · 9 revisions

SplitMoblings Component Documentation

Overview

The SplitMoblings is a game component that when added to a mob entity created in the NPCFactory, splits into multiple mobs when the original entity dies. This component adds a method onDeath to an existing event listener dieStart. Amount of spawned mobs and the respective sizes compared to the original scale may be configured if desired.

At the current time of this documentation (29/09/2023), the target Entity will split into multiple water slime mobs, and as such, it is recommended to have this specified component added onto a water slime mob found in the NPCFactory.

Usage

To use the SplitMoblings component, simply initialize the component with the desired configuration, and add it to the specified game entity.

Entity mob = NPCFactory.createBaseWaterSlime();
int amount = 10;
SplitMoblings splitMoblingsComponent = new SplitMoblings(amount);
mob.addComponent(splitMoblingsComponent);

Configuration

This component allows flexible configuration of the spawned mobs, but not so much of the original entity itself.

  • amount: Specifies the amount of mobs to be spawned after the original entity is dead and disposed of. Must be >= 0.
  • scale: Scales the split moblings horizontal and vertical size based on this float value and the current entity's size ( parent entity's size_x * scale, parent entity's size_y * scale ). If not provided, the scale is assumed to be 0.75f.
  • scaleX and scaleY: Similar to scale but allows more flexibility to the horizontal and vertical sizing of the moblings.

Requirements

  • This component actually duplicates the specified entity to the desired amount, copying all of the water slime's base stats (health and attack) with its respective tasks and components. Furthermore, the Entity should also be also able to die since the dieStart event needs to be invoked in order for the component to function properly. As such it is vital that the entity has a CombatStatsComponent with base configurations of health that is > 0.
  • This component should also have a self-disposing mechanism so that the specified entity may be disposed of when the additional mobs are spawned. One such way is to rely on the existing MobWanderTask that detects if the mob isDead(), and consequently sets the entity's deletion flag to true. However, this dispose on death funtionality is completely up to you.

Functionality

As established, the core functionality of this component simply duplicates the water slime entity and multiplies it based on the amount specified. If the amount is 1, the spawn location of the mobling will be -1.5f from the death point of the entity in the x-axis. However, if there is more than one amount, the moblings will be spawned evenly in a circular fashion with a radius of 1.5f. For instance, if there are two mobs, one will be spawned (-1.5f, entity.y) and the other will be spawned (1.5f, entity.y). This calculation is defined as:

for (int i = 0; i < amount; i++) {
      float currAngle = (float) (360 / amount) * i;
      double radians = currAngle * Math.PI / 180;

      float newX = entity.getPosition().x + (float) OFFSET_DISTANCE *
          (float) Math.cos(radians);
      float newY = entity.getPosition().y + (float) OFFSET_DISTANCE *
          (float) Math.sin(radians);

      if (withinBounds(newX, newY))
        spawnAdditionalMob(newX, newY, initialScaleX, initialScaleY);
    }

The withinBounds helper method prevents mobs from spawning out of map bounds. This means that the moblings spawned may not match up to the required amount if some of the mobs will be spawned out of map bounds. Thus, its imperative to change the static variables in this component class if map bounds do change.

Best Practices

  1. Ensure that the amount specified is greater than 0 for the SplitMoblings component to work.
  2. Recommended for the base entity that triggers dieStart event to be a water slime. If this changes, change the NPCFactory method in this component class accordingly.
  3. At the current time of this documentation (29/09/2023), there are no checks for the scale parameters for the constructors. Ensure these are valid scales (>0) and at appropriate amounts (< 3 recommended).

Testing

Visual testing has concluded that the mobs do spawn moblings at specified amounts on death. JUnit has confirmed the locations of these moblings do match a circular fashion with correct amounts. The prevention of these spawns from map edge bounds were also tested through JUnit test cases.

Clone this wiki locally