Class Character

All Implemented Interfaces:
Element

public class Character extends Positionable
A character in the game. The character has a name, an avatar, an inventory of items, and a path of visited places . The character can move to a place, move back to the previous place, and hold an item.
Author:
José Paulo Leal jpleal@fc.up.pt
Implementation Note:
Corresponds to a Leaf in the Composite design pattern.
  • Field Details

    • name

      String name
      The name of the character
    • inventory

      List<Item> inventory
      The inventory of items
    • holding

      Item holding
      The item being held
    • path

      Stack<Place> path
      The path of visited places. Needed to backtrack.
  • Constructor Details

    • Character

      public Character(String name, Avatar avatar)
      Create a character.
      Parameters:
      name - the name of the character
      avatar - the avatar of the character
  • Method Details

    • getName

      public String getName()
      Get the name of the character.
      Returns:
      the name of the character.
    • getPlace

      public Place getPlace()
      Get the place where the character is currently in.
      Returns:
      the place where the character is currently in, or null if the character is not in any place.
    • move

      public void move(Place place)
      Move to the given place. In that place, the character is postioned at the entrance.
      Parameters:
      place - the place to move to
    • moveBack

      public Place moveBack()
      Move back to the previous place, if possible. Cannot move back if the character has not moved to any place yet, apart from the starting place.
      Returns:
      the place where the character moves to, or null if the character cannot ho back.
    • getInventory

      public List<Item> getInventory()
      Get the inventory, the items the character is carrying.
      Returns:
      the inventory
    • addItem

      public void addItem(Item item)
      Add an item to the inventory.
      Parameters:
      item - the item to add
    • dropItem

      public boolean dropItem(Item item)
      Drop an item from the inventory.
      Parameters:
      item - the item to drop
      Returns:
      true if the item was dropped, false otherwise
    • holdItem

      public void holdItem(Item item)
      Hold the given item. A single item can be held by a character at a time. An item must be held to be used. The argument can be null to stop holding an item.
      Parameters:
      item - to hold
    • getHolding

      public Item getHolding()
      Get the item being held.
      Returns:
      the item being held, or null if no item is being held.
    • accept

      public void accept(Visitor visitor)
      Accept a visitor.
      Parameters:
      visitor - the visitor to accept