Inventory System - UQcsse3200/2024-studio-1 GitHub Wiki

Objective

Efficiently managing in-game items such as weapons, tools, and consumables is crucial for a smooth gameplay experience. To achieve this, the project utilizes key software design patterns and principles, including interfaces and the factory system, ensuring flexibility, scalability, and consistent item management throughout the game.

Collectible Interface

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

What It Does:

The Collectible interface defines a standard structure for any item that can be collected by the player, including weapons and consumable items. This uniformity allows different types of collectibles to be managed seamlessly within the inventory system, ensuring that the game can easily handle various item types without additional complexity.

Why It's Implemented:

The Collectible interface is implemented to provide a flexible and extensible framework for adding new items to the game. By enforcing a consistent structure, the interface supports polymorphism, allowing different item types to be treated the same way by the inventory system. This approach simplifies code maintenance and enhances the game's ability to evolve, as new items can be introduced without altering existing code.


Inventory Class

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

What It Does:

The Inventory class represents the player's inventory system, responsible for tracking and managing all the items the player has collected, including melee and ranged weapons. It serves as the central hub for item management, handling the storage, retrieval, and manipulation of items within the game.

Why It's Implemented:

The Inventory class is implemented to provide a structured way to manage the player's collected items, ensuring that item management is consistent and organized. By encapsulating the logic for handling various types of collectibles, this class supports smooth gameplay, enabling players to interact with their items in a predictable and intuitive manner.


Inventory Component

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

What It Does:

The InventoryComponent class acts as a manager for the player's inventory, integrating with other game components to facilitate item pickup and drop actions. It relies on an Inventory object to store and track items, providing methods to add or remove items from the inventory and triggering appropriate in-game events.

Why It's Implemented:

The InventoryComponent class is implemented to link the inventory system directly with the player's actions and the game world. By managing how items are added to and removed from the inventory, this class ensures that the player's interactions with items are handled consistently, supporting a cohesive and immersive gameplay experience.

Example Usage:

// Using InventoryComponent to manage items
InventoryComponent playerInventoryComponent = new InventoryComponent();
Entity player = new Entity();
player.addComponent(playerInventoryComponent);

Collectible potion = new HealthPotion();
Collectible sword = new MeleeWeapon("Sword");

// Picking up items
playerInventoryComponent.pickup(potion);
playerInventoryComponent.pickup(sword);

// Dropping items
playerInventoryComponent.drop(potion);

Collectible Factory

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

What It Does:

The CollectibleFactory class is responsible for creating instances of collectible items based on given specifications. It converts these items into game entities, allowing them to be used within the game world, whether as part of the player's inventory or as interactable objects within the environment.

Why It's Implemented:

The CollectibleFactory class is implemented to streamline the creation and integration of collectible items into the game. By centralizing the item creation process, this factory pattern supports easy item customization and ensures that all collectibles adhere to the game's predefined standards. This approach enhances code reusability and simplifies the addition of new items to the game.

Example Usage:

// Creating items using CollectibleFactory
CollectibleFactory factory = new CollectibleFactory();

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

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

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

// Convert collectibles to entities and add to the game world
Entity swordEntity = factory.createCollectibleEntity(sword);
Entity bowEntity = factory.createCollectibleEntity(bow);
Entity potionEntity = factory.createCollectibleEntity(potion);

// Or do them both in one step
Entity knifeEntity = factory.createCollectibleEntity("melee:Knife");

UML Diagram

Testing and Validation

to test the validity and reliability of this class, unit testing was conducted to ensure effective functionality. Test Plans for Inventory System describes the steps taken and scenarios that were considered while testing for this component.