Inventory code logic - UQdeco2800/2022-studio-2 GitHub Wiki

Inventory initialisation

The inventory consists 3 separated storage: inventory, quick bar, and equippables. The size of the storage units is fixed.

  private static final int INVENTORY_SIZE = 16;
  private List<Entity> inventory = new ArrayList<>(INVENTORY_SIZE);

  private static final int QUICKBAR_SIZE= 3;
  private List<Entity> quickBarItems = new ArrayList<>(QUICKBAR_SIZE);

  private static final int EQUIP_SLOTS = 2;
  private Entity[] equipables = new Entity[EQUIP_SLOTS];

The player's inventory component is added to the player entity when created. The quantity of the storage is handled by other array variables which stores the quantity of the item with the same index in storage.

  private int[] itemQuantity = new int[inventorySize];

  private int[] quickBarQuantity = new int[quickBarSize];

Sprint 3 updates

Sprint 3 updated method

Sorting algorithm of inventory items

Inventory sorting algorithm

Sprint 2 updated methods

Adding items

Adding items to the inventory can be handled using the addItem methods. AddItem() is an overloaded method that can take the following parameters.

Entity - Adds the entity to the inventory with a quantity of 1.

Entity and quantity - Adds the entity to the inventory with the input quantity. (Mainly for testing purposes)

It adds the item into the inventory, if there is the same type of entity already existing then it stacks the item.

  public void addItem(Entity item) {
    if (inventory.size() == inventorySize) {
      logger.info("Inventory if full");
    } else if (!inventory.contains(item)) {
      inventory.add(item);
    } else if ((item.checkEntityType(EntityTypes.MELEE)
            || item.checkEntityType(EntityTypes.RANGED))
            && !inventory.contains(item)) {
      inventory.add(item);
    }
    ++itemQuantity[inventory.indexOf(item)];
  }

  public void addItem(Entity item, int quantity) {
    if (inventory.size() == inventorySize) {
      logger.info("Inventory if full");
    } else if (!inventory.contains(item)) {
      inventory.add(item);
      itemQuantity[inventory.indexOf(item)] = quantity;
    }
  }

Removing items

Removing items from the inventory is handled by an overloaded method that takes the followwing parameters.

Entity -Removes the exact input entity in the inventory or reduces 1 of the entity quantity.

index -Removes the entity at the index or reduces 1 of the entity quantity.

EntityTypes -Removes the entity based on the entity type. This implementation is designed for crafting materials and should be only used for crafting material removements.

Based on the parameter that is passed in the method will remove the specific entity. (Note: this method requires the inventory containing the item that is to be removed)

 public void removeItem(Entity item) {
    --itemQuantity[inventory.indexOf(item)];
    if (getItemQuantity(item) == 0) {
      inventory.remove(item);
    }
  }

  public void removeItem(int index) {
    --itemQuantity[index];
    if (getItemQuantity(index) == 0) {
      inventory.remove(index);
    }
  }

  public void removeItem(EntityTypes type) {
    for (int i = 0; i < inventory.size(); ++i) {
      if (inventory.get(i).checkEntityType(type)) {
        removeItem(i);
      }
    }
  }

Adding potions to quick bar

Due to the nature of stacking implementation of potions. The add method differs to the main inventory. This function checks if the potion that is to be added has the same effect type with the existing potions in the quick bar. Then, it proceeds to check to stack the potion or add a new entry to the quick bar.

public void addQuickBarItems(Entity potion) {
    boolean hasPotion = hasPotion(potion);

    if (quickBarItems.size() == quickBarSize) {
      if (!hasPotion) {
        logger.info("Inventory is full");
      } else {
        if (quickBarQuantity[getPotionIndex(potion)] < 9) {
            logger.info("Added to quick bar");
            ++quickBarQuantity[getPotionIndex(potion)];
        }
      }
    } else {
      if (hasPotion) {
        if (quickBarQuantity[getPotionIndex(potion)] < 9) {
          logger.info("Added to quick bar");
          ++quickBarQuantity[getPotionIndex(potion)];
        } else {
          logger.info("Inventory is full");
        }
      } else {
        logger.info("Added to quick bar");
        quickBarItems.add(potion);
        ++quickBarQuantity[getPotionIndex(potion)];
      }
    }
  }

Back to Inventory Page

Author

  • Li-Sung Ou
  • GitHub: @PeterOu8
  • Discord: Secret Agent Randy Beans#6754
  • Slack: Li-Sung Ou
⚠️ **GitHub.com Fallback** ⚠️