Class KeyComponent


public class KeyComponent extends CollectableComponent
A collectable key component that allows the player to pick up and store keys in their InventoryComponent. Each KeyComponent has a unique keyId string which identifies it, and is used by systems such as DoorComponent to determine whether a door can be unlocked.

When the player collides with an entity containing this component, the key is added to the player's inventory. Multiple keys of the same keyId may be collected and are tracked as a count in the inventory.

  • Field Details

    • keyId

      public final String keyId
  • Constructor Details

    • KeyComponent

      public KeyComponent(String keyId)
      Creates a KeyComponent with a given key identifier.
      Parameters:
      keyId - the unique ID string used to identify this key
  • Method Details

    • onCollect

      protected boolean onCollect(Entity player)
      Attempts to collect this key into the player's inventory.

      On success, the key is added to the InventoryComponent of the colliding player entity, and debug information is logged to the console.

      Specified by:
      onCollect in class CollectableComponent
      Parameters:
      player - the player entity attempting to collect the key
      Returns:
      true if the key was successfully collected and stored, false if the player entity is null or has no inventory
    • getKeyId

      public String getKeyId()
      Returns this key types identifier.
      Returns:
      the non-null key identifier string