Skip to content

Inventory System

manya-k edited this page Aug 26, 2024 · 9 revisions

Objective

In the design of a game, managing in-game items such as weapons, tools, or consumable efficiently is crucial for a smooth gameplay experience. To achieve this, the project employs several key software design patterns and principles, including interfaces and the factory system.

Collectible Interface

com.csse3200.game.components.player.inventory.Collectible Interface

Explanation of Interfaces

Collectible Interface: The Collectible interface defines the basic properties and behaviors of any item that can be collected by the player. By using an interface, the game ensures that any collectible object, whether it's a weapon, item, or other entity, adheres to a consistent structure and can be managed uniformly within the inventory system.

Why Interfaces? Flexibility: Interfaces allow different types of collectibles (items, melee weapons, ranged weapons) to be treated uniformly, enabling polymorphism. Extensibility: New types of collectibles can be added without modifying existing code, as long as they implement the Collectible interface.

  • Type getType(): Returns the type of the item, determining how it should be used by the player. Types include 'ITEM', 'MELEE_WEAPON', and 'RANGED_WEAPON'.
  • String getName(): Returns the name of the item.
  • Texture getIcon(): Returns the texture used as the item's icon.
  • String getSpecification(): Provides a string representation of the collectible item that can be parsed by the CollectibleFactory.
  • void pickup(Inventory inventory): Allows the entity to pick up the item, applying any associated effects.
  • void drop(Inventory inventory): Removes the item from the entity's inventory.

Type Enum: Defines the possible types of collectibles: ITEM, MELEE_WEAPON, RANGED_WEAPON.


Inventory Class

com.csse3200.game.components.player.inventory.Inventory Class**

This class represents the inventory system for the player. It tracks the items the player has collected, including melee and ranged weapons.

  • Inventory(InventoryComponent component): Constructs an inventory attached to a specific inventory component.
  • Entity getEntity(): Returns the entity to which this inventory is attached.
  • Optional<MeleeWeapon> getMelee(): Returns the currently held melee weapon.
  • void setMelee(MeleeWeapon melee): Sets the currently held melee weapon and resets any previous one.
  • void resetMelee(): Resets the melee weapon to the default (empty).
  • Optional<RangedWeapon> getRanged(): Returns the currently held ranged weapon.
  • void setRanged(RangedWeapon ranged): Sets the currently held ranged weapon and resets any previous one.
  • void resetRanged(): Resets the ranged weapon to the default (empty).
  • Array<Collectible> getItems(): Returns a list of currently collected items.
  • void addItem(Collectible item): Adds a new item to the inventory and triggers an update event.

Inventory Component

com.csse3200.game.components.player.inventory.InventoryComponent Class

This class represents a component that manages the player's inventory. It uses an Inventory object to track items and weapons.

  • InventoryComponent(): Constructs a new, empty inventory component.
  • void pickup(Collectible item): Adds an item to the inventory by calling its pickup method.
  • void drop(Collectible item): Removes an item from the inventory by calling its drop method.
  • Inventory getInventory(): Returns the underlying Inventory object.

USAGE EXAMPLE

InventoryComponent playerInventoryComponent = new InventoryComponent();
Entity player = new Entity();
player.addComponent(playerInventoryComponent);

Collectible potion = new Item("Health Potion");
playerInventoryComponent.pickup(potion);

Collectible Factory

com.csse3200.game.entities.factories.CollectibleFactory Class

This factory class is responsible for creating collectible items and converting them into entities.

  • Collectible create(String specification): Creates a collectible item based on a specification string. The specification indicates the type of the item (melee, ranged, or item) and the details of the item.
  • Entity createCollectibleEntity(Collectible collectible): Converts a collectible item into an entity with components

USAGE EXAMPLE

CollectibleFactory factory = new CollectibleFactory();

// Create a melee weapon collectible
Collectible sword = factory.create("melee:Knife");

// Create a ranged weapon collectible
Collectible bow = factory.create("ranged:shotgun");

// Create a generic item collectible
Collectible potion = factory.create("item:health_potion");

UML Diagram

image

Table of Contents

Home

Design

Design Document

Design Choices

Game Wiki

Gameplay

Controls

Game Features

Utilities
Animals
Menus/screens
Character
Map
Weapon
Projectile
Items
Music/sound

User Guide

Starting the game

Game Engine

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Enhancement of Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally