Package model

Class Ghost


public abstract class Ghost extends Actor
A ghost in the game. This abstract class models the common behaviors and attributes of all ghosts including their appearance and different behavioral states:

- WAIT: The ghost is not currently present in the maze, its `delay` specifies how long before it enters.

- CHASE: The ghost is actively seeking PacMann. Catching up to PacMann will stop the round.

- FLEE: The ghost is running away to its designated location on the board. If PacMann catches up to this ghost, it will leave the board and enter its WAIT state.

Subclasses are responsible for specifying the `target()` vertices of that ghost in its CHASE and FLEE states.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The three possible behavior states of ghosts

    Nested classes/interfaces inherited from class model.Actor

    Actor.DPair, Actor.Location
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected double
    When in the FLEE state, the number of milliseconds remaining before this ghost returns to its CHASE state
    private final Color
    The color of this ghost in its CHASE state
    The edges comprising the most recently calculated path to this ghosts' `target()`
    private final int
    The total number of milliseconds before this ghost enters the maze in its CHASE state
    The current behavioral state of this ghost
    private double
    When in the FLEE state, the number of milliseconds remaining before this ghost returns to its CHASE state

    Fields inherited from class model.Actor

    location, model
  • Constructor Summary

    Constructors
    Constructor
    Description
    Ghost(GameModel model, Color ghostColor, int initialDelay)
    Construct a ghost associated to the given `model` with specified color and initial delay
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Return the current base speed of this actor, before factoring in the weight of the edge they are traversing.
    Return this ghost's color (for painting)
    double
    Return the amount of time remaining on this ghost's FLEE timer
    In their WAIT state, the ghosts move from side to side in their box, otherwise, their bounding box location is delegated up to `Actor.getBoundingBoxUL`.
    Return the list of Edges in this actor's current planned path through the maze (if any).
    double
    Return the amount of game time (in ms) that this actor is able to travel at its current speed before reaching a vertex or changing state.
    Returns the first edge along the shortest path from this ghost's `currentVertex()` to its `target()`.
    void
    propagate(double dt)
    Calculate the new position for this actor after `ms` milliseconds of game time have elapsed.
    void
    Transition this ghost to its WAIT state and reset the `waitTimeRemaining` to this ghost's `INITIAL_DELAY`
    void
    Transition this ghost to its WAIT state when it is caught in its FLEE state, and resets `waitTimeRemaining`.
    void
    Transition this ghost to its FLEE state and reset the `fleeTimeRemaining`.
    Return the current state of this ghost
    protected abstract MazeGraph.MazeVertex
    Return the vertex that this ghost is targeting
    void
    Ghosts do not take any actions when visiting vertices
    double
    Return the amount of time remaining on this ghost's WAIT timer

    Methods inherited from class model.Actor

    currentEdge, edgeSpeed, location, nearestVertex, traverseEdge

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • state

      protected Ghost.GhostState state
      The current behavioral state of this ghost
    • initialDelay

      private final int initialDelay
      The total number of milliseconds before this ghost enters the maze in its CHASE state
    • waitTimeRemaining

      private double waitTimeRemaining
      When in the FLEE state, the number of milliseconds remaining before this ghost returns to its CHASE state
    • fleeTimeRemaining

      protected double fleeTimeRemaining
      When in the FLEE state, the number of milliseconds remaining before this ghost returns to its CHASE state
    • ghostColor

      private final Color ghostColor
      The color of this ghost in its CHASE state
    • guidancePath

      private List<MazeGraph.MazeEdge> guidancePath
      The edges comprising the most recently calculated path to this ghosts' `target()`
  • Constructor Details

    • Ghost

      public Ghost(GameModel model, Color ghostColor, int initialDelay)
      Construct a ghost associated to the given `model` with specified color and initial delay
  • Method Details

    • target

      protected abstract MazeGraph.MazeVertex target()
      Return the vertex that this ghost is targeting
    • state

      public Ghost.GhostState state()
      Return the current state of this ghost
    • color

      public Color color()
      Return this ghost's color (for painting)
    • fleeTimeRemaining

      public double fleeTimeRemaining()
      Return the amount of time remaining on this ghost's FLEE timer
    • waitTimeRemaining

      public double waitTimeRemaining()
      Return the amount of time remaining on this ghost's WAIT timer
    • getBoundingBoxUL

      public Actor.DPair getBoundingBoxUL()
      In their WAIT state, the ghosts move from side to side in their box, otherwise, their bounding box location is delegated up to `Actor.getBoundingBoxUL`.
      Overrides:
      getBoundingBoxUL in class Actor
    • visitVertex

      public void visitVertex(MazeGraph.MazeVertex v)
      Ghosts do not take any actions when visiting vertices
      Specified by:
      visitVertex in class Actor
    • nextEdge

      public MazeGraph.MazeEdge nextEdge()
      Returns the first edge along the shortest path from this ghost's `currentVertex()` to its `target()`.
      Specified by:
      nextEdge in class Actor
    • guidancePath

      public List<MazeGraph.MazeEdge> guidancePath()
      Description copied from class: Actor
      Return the list of Edges in this actor's current planned path through the maze (if any). This is intended to be used for visualizing actors' intended behavior. Sequence of edges must form a continuous path (e.g., `ans.get(i).dst().equals(ans.get(i+1).src())`).
      Specified by:
      guidancePath in class Actor
    • reset

      public void reset()
      Transition this ghost to its WAIT state and reset the `waitTimeRemaining` to this ghost's `INITIAL_DELAY`
      Specified by:
      reset in class Actor
    • baseSpeed

      public double baseSpeed()
      Description copied from class: Actor
      Return the current base speed of this actor, before factoring in the weight of the edge they are traversing.
      Specified by:
      baseSpeed in class Actor
    • startFlee

      public void startFlee()
      Transition this ghost to its FLEE state and reset the `fleeTimeRemaining`. Has no effect if the ghost is in its WAIT state.
    • respawn

      public void respawn()
      Transition this ghost to its WAIT state when it is caught in its FLEE state, and resets `waitTimeRemaining`. Requires that this ghost is currently in the FLEE state.
    • propagate

      public void propagate(double dt)
      Calculate the new position for this actor after `ms` milliseconds of game time have elapsed. In the WAIT state, this updates its delay and spawns the ghost into the maze in its CHASE state when the delay reaches 0. In the FLEE state, the `fleeTimeRemaining` is updated and the ghost reverts to its CHASE state when this reaches 0. Otherwise, delegates to `Actor.updateLocation()`.
      Overrides:
      propagate in class Actor
    • maxPropagationTime

      public double maxPropagationTime()
      Description copied from class: Actor
      Return the amount of game time (in ms) that this actor is able to travel at its current speed before reaching a vertex or changing state. Return POSITIVE_INFINITY if this actor is standing still and has no anticipated state changes.
      Overrides:
      maxPropagationTime in class Actor