Skip to content

AbstractPotion

PHONGNGYUEN1 edited this page Aug 28, 2024 · 4 revisions

Introduction: The AbstractPotion class is a foundational component designed to manage the core functionality of potion items in the game. It provides the necessary structure for defining various potion types by handling common behaviours like effect management and usage tracking. This class is meant to be extended by specific potion types (e.g. HealingPotion), which implemented the unique effects and behaviour of those potions.

Expected Behavior

  • Effect Management: The class holds a list of possible effects that the potion can apply. These effects are applied in sequence whenever the potion is used.
  • Usage Tracking: The class tracks the number of uses left for the potion. Each use tracks the number of uses left for the potion. Each use decreases the available quantity, eventually rendering the potion useless once the quantity reaches zero.
  • Abstract Method Requirement: Subclasses must implement the applyEffect() method which defines how the potion's effect are applied when consumed.

Example Usage

To create a specific potion type, extend AbstractPotion and implement the applyEffect() method:

public class HealingPotion extends AbstractPotion {

    public HealingPotion(int quantity) {
        super(quantity, List.of(new HealEffect(50)));
    }

    @Override
    public void applyEffect() {
        for (AbstractEffect effect : possibleEffects) {
            effect.apply();
        }
    }
}

In this example, HealingPotion is a concrete implementation that applies a healing effect when used.

Integration

  • Subclasses: Extend AbstractPotion to create different potion types. Each subclass should initialize the potion with a specific set of effects and implement applyEffect() method to define its unique behaviour
  • Usage: When the useItem() method is called on a potion, the effects defined in the possibleEffects list are applied in order. The potion's usage decremented with each use.

Key Points

  • Customization: Easily extendable to create various potion types by implementing the abstract method applyEffect()
  • Modular Design: Designed to integrate seamlessly with the game's item system, providing a flexible way to manage item effects and usage.

Images

Health Potion

image rendered by @PHONGNGUYEN1

Clone this wiki locally