Sprint 4: Enemy Loot - UQdeco2800/2021-studio-6 GitHub Wiki

Drops loot

When the enemy dies, there is a chance that a new item will be spawned where the enemy died or close by. Each enemy has a specific:

  • Chance that a loot item will drop
  • The item type that will drop (currently only coins or ammo)
  • The minimum amount of that item to drop
  • The maximum amount of that item to drop

The loot reward scales with the difficulty of the enemy, with the large enemy giving the highest chance of a good reward. We decided that the spawner will be the only enemy that drops ammo, as it is a unique type of enemy compared to the others.

These are the parameters for each enemy: Spawner: Drops 3 - 5 ammo with 100% drop rate Small enemy: Drops 1 - 2 coins with 30% drop rate Large enemy: Drops 5 - 10 coins with 100% drop rate Long range enemy: Drops 1 - 3 coins with 50% drop rate Tough long ranged enemy: drops 3 - 7 coins with 100% drop rate

Justification

When user testing in sprint 3 we noticed that the primary strategy for users was to run through each level without attacking any of the enemies. The aim of adding a loot component was to give more of a sense of accomplishment to the user for defeating an enemy and also give them an advantage later in the level (a risk vs reward dynamic).

Implementation

A new LootComponent class was created that can be configured on initialisation. This class listens to the "hit" event for the entity.

this.entity.getEvents().addListener("hit", this::npcHit);

When a "hit" event is triggered, the entities CombatStatsComponent will be checked to see if it is now considered dead.

if (combatStatsComponent.isDead()) {
  enemyDead = true;
}

When it is dead, it will get the world position coordinates of where the entity died.

Vector2 deathPos = entity.getPosition();
GridPoint2 deathGridPoint = ServiceLocator.getGameArea().getTerrain().worldPositionToTile(deathPos);

The chance and quantity of the loot is then calculated, created using the ItemFactory, and spawned at the position of the entities death.

  if (MathUtils.randomBoolean(chanceOfLoot)) {
	int lootQuantity = MathUtils.random(minItems, maxItems);
	Entity loot;

	if (item.equalsIgnoreCase("ammo")) {
	  loot = ItemFactory.createAmmoPickup(lootQuantity);
	} else {
	  loot = ItemFactory.createCoinPickup(lootQuantity);
	}

	ServiceLocator.getGameArea().spawnEntityAt(loot, deathGridPoint, true, false);
  }

Loot sequence diagram

loot sequence diagram

Note: Right click and select open image in new tab to see the full quality image.

This is the UML sequence diagram that shows the how the enemy loot is created when an enemy is killed. Initial creation of the entity and the loot component in the NPCFactory is not displayed for simplicity.