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

Inventory Display

This wiki page will explain the implementation of the item equipment which allows the player to equip an item or use a potion.

Toggle inventory

When the player press the I key, toggleInventoryDisplay() will trigger the inventory display methods. It will toggle the opening and closing of the inventory menu.

  public void toggleInventoryDisplay() {
    if (!inventoryIsOpened) {
      ServiceLocator.getInventoryArea().displayInventoryMenu();
      ServiceLocator.getInventoryArea().showItem();
    } else {
      ServiceLocator.getInventoryArea().disposeInventoryMenu();
    }
    EntityService.pauseAndResume();
    inventoryIsOpened = !inventoryIsOpened;
  }

Display inventory menu

Function displayInventoryMenu() initialized the inventory menu and set size and position for the inventory.

  public void displayInventoryMenu() {
      inventoryMenu = new Image(new Texture(Gdx.files.internal
              ("images/Inventory/Inventory_Armor_V2.png")));
      //Note: the position of the asset is at the bottom left.
      inventoryMenu.setSize(768, 576 );
      inventoryMenu.setPosition(Gdx.graphics.getWidth() / 2 - inventoryMenu.getWidth(),
              Gdx.graphics.getHeight() / 2 - inventoryMenu.getHeight() / 2);
      inventoryGroup.addActor(inventoryMenu);
      stage.addActor(inventoryGroup);
      stage.draw();
  }

Show items in inventory

Once player picks up an item, the item will be added to inventory.

Function showItem() takes the entire inventory entires and display each item in the menu.

  public void showItem() {
    float padding = 12.5f;
    InventoryComponent inventory =ServiceLocator.getGameArea().getPlayer().getComponent(InventoryComponent.class);
    items = inventory.getInventory();
    for (int i = 0; i < items.size(); ++i) {
      Entity currentItem = items.get(i);
      Texture itemTexture = currentItem.getComponent(TextureRenderComponent.class).getTexture();
      TextureRegion itemTextureRegion = new TextureRegion(itemTexture);
      TextureRegionDrawable itemTextureDrawable = new TextureRegionDrawable(itemTextureRegion);
      ImageButton item = new ImageButton(itemTextureDrawable);
      item.setSize(53, 53);
      //187.5 and 360 are the magic numbers. DO NOT CHANGE!
      int row = i / 4;
      int column = i % 4;
      float horizontalPosition = (inventoryMenu.getX() + 187.5f) + column * (padding + 53);
      float verticalPosition = (inventoryMenu.getY() + 360) - row * (padding + 53);
      item.setPosition(horizontalPosition, verticalPosition);
      inventoryGroup.addActor(item);
    }
  }

Back to Inventory Page

Author

  • Li-Sung Ou
  • GitHub: @PeterOu8
  • Discord: Secret Agent Randy Beans#6754
  • Slack: Li-Sung Ou

Author

  • Siu Ming Lam
  • GitHub: @Timothy_Lam123
  • Discord: Timothy_Lam#2101
  • Slack: Siu Ming Lam