LootBoxes - UQcsse3200/2024-studio-2 GitHub Wiki

UniversalLootBox

Package: com.csse3200.game.inventory.items.lootbox

Overview

The UniversalLootBox class is a type of consumable item that, when used, generates a random set of items based on a loot table. It interacts with the player's inventory and the game world by adding items directly to the inventory or dropping them nearby if the inventory is full. The class includes logic for consuming the loot box, managing inventory, and updating the player's display.

Features

  • Randomized Loot Generation: Generates items based on a specified loot table and a defined number of rolls.
  • Inventory Management: Adds generated items to the player's inventory or drops them near the player if the inventory is full.
  • Event-Driven Updates: Uses event triggers to update the inventory display and show loot results to the player.
  • Customizable Loot Behavior: The number of rolls and the items' rarity or types can be configured through the BaseLootTable class.

Key Components

Fields

  • BaseLootTable lootTable: The loot table that defines the potential items and their drop chances.
  • int rolls: Number of times the loot table is rolled to determine the items.
  • Entity player: Reference to the player entity using the loot box.
  • Logger logger: Logs loot box activities such as adding items or dropping them.

Constructor

UniversalLootBox(BaseLootTable lootTable, int rolls, Entity player, String description, String texturePath, int code)

Parameters

  • lootTable: The loot table for generating random items.
  • rolls: Number of times the loot table is rolled to determine the items.
  • player: Reference to the player entity using the loot box.
  • description: Description of the loot box.
  • **`texturePath: Path to the loot box texture.
  • **code: Unique code identifier for the lootbox
  • Throws: IllegalArgumentException if the number of rolls is less than 1.

Constructor

LootBox(BaseLootTable lootTable, int rolls, Entity player)

Methods

  • List<AbstractItem> open()

    • Description: Opens the loot box and returns a list of randomly selected items based on the loot table and number of rolls.
    • Returns: List of AbstractItem objects.
  • void useItem(ItemUsageContext context)

    • Description: Consumes the loot box and adds the generated items to the player's inventory. If the inventory is full, items are dropped near the player.
    • Parameters:
      • context: Context in which the item is being used, typically includes information about the entity using the item.
    • Throws:
      • ConsumedException if the loot box has been fully consumed.
    • Behavior:
      • Uses open() to generate items.
      • Removes one instance of the loot box from the player's inventory.
      • Adds new items to the inventory or drops them if the inventory is full.
      • Triggers events to update the player's inventory display.
  • Entity getPlayer()

    • Description: Returns the player entity associated with the loot box.
    • Returns: Entity object representing the player.

Example Usage To create a LootBox:

// Create a loot table and player entity
BaseLootTable lootTable = new ExampleLootTable();
Entity player = new PlayerEntity();

// Instantiate a UniversalLootBox with the loot table and player entity
UniversalLootBox lootBox = new UniversalLootBox(lootTable, 3, player, 
    "A mysterious box containing random treasures.", 
    "textures/lootbox.png", 
    1001);

// Use the loot box
try {
    lootBox.useItem(new ItemUsageContext(player));
} catch (ConsumedException e) {
    System.out.println("The loot box is empty and cannot be used again.");
}

// Access player and loot box details
Entity lootBoxUser = lootBox.getPlayer();

Notes

  • The UniversalLootBox is designed for flexibility with different loot tables and roll counts, making it easy to integrate new types of loot boxes by changing the loot table or item probabilities.
  • It assumes that the player's inventory component has the necessary methods for managing items and events for adding, removing, or displaying inventory items.