Class CompoundPieceCollection

java.lang.Object
VASSAL.build.module.map.CompoundPieceCollection
All Implemented Interfaces:
PieceCollection
Direct Known Subclasses:
DefaultPieceCollection, LayeredPieceCollection.Collection

public abstract class CompoundPieceCollection extends Object implements PieceCollection
Base class for PieceCollection implementation that organizes GamePieces -- a category which in this case also includes Decks and Stacks, in addition to ordinary pieces -- into distinct visual layers. The layers are drawn in order of their index, i.e. layer 0 is drawn first and thus appears "on the bottom", as higher layers are drawn over top of it.

Maintains, usually on behalf of a Map, lists of all the pieces in each of several layers, along with a set of "enabled" flags marking which layers are disabled/hidden and which are enabled/visible. When a piece is added to the overall collection, its appropriate layer is determined and it is added to the list of pieces on that layer.
  • Field Details

    • layers

      protected SimplePieceCollection[] layers
    • bottomLayer

      protected int bottomLayer
    • enabled

      protected boolean[] enabled
  • Constructor Details

    • CompoundPieceCollection

      protected CompoundPieceCollection(int layerCount)
  • Method Details

    • initLayers

      protected void initLayers(int layerCount)
    • getLayerForPiece

      public int getLayerForPiece(GamePiece p)
      Default implementation is "degenerate", having only a single layer -- when extending this class, this method takes a piece and determines which of several layers it belongs in, returning an index.
      Parameters:
      p - A game piece
      Returns:
      Index for the visual layer the piece should be drawn with.
    • getLayerNameForPiece

      public String getLayerNameForPiece(GamePiece p)
      Default implementation is "degenerate", having only a single layer -- when extending this class, this method takes a piece and determines which of several layers it belongs in, returning the layer name.
      Parameters:
      p - A game piece
      Returns:
      the layer name it belongs in.
    • getLayerForName

      public int getLayerForName(String layerName)
      Default implementation is "degenerate", having only a single layer -- when extending this class, this method takes layer name and returns the index for that layer, or -1 if the string does not name a valid layer.
      Parameters:
      layerName - the name of a layer
      Returns:
      the index for the layer
    • getCollectionForPiece

      protected PieceCollection getCollectionForPiece(GamePiece p)
      Given a game piece, returns the simple piece collection for the layer that it belongs in
      Parameters:
      p - A game piece
      Returns:
      the piece collection for the layer that it belongs in
    • add

      public void add(GamePiece p)
      Adds a piece to the overall collection, by adding it to the simple collection for the layer it belongs in.
      Specified by:
      add in interface PieceCollection
      Parameters:
      p - Game piece to add
    • clear

      public void clear()
      Clears the whole collection.
      Specified by:
      clear in interface PieceCollection
    • getPieces

      public GamePiece[] getPieces()
      Description copied from interface: PieceCollection
      Return all currently-visible pieces in the collection as a read-only array
      Specified by:
      getPieces in interface PieceCollection
    • getPieces

      protected GamePiece[] getPieces(boolean includeDisabled)
      Parameters:
      includeDisabled - true if pieces in disabled layers should be included
      Returns:
      A list of all pieces in this overall collection, or all that are in "enabled" layers, depending on the parameter
    • getAllPieces

      public GamePiece[] getAllPieces()
      Description copied from interface: PieceCollection
      Return all pieces in the collection, regardless of visibility
      Specified by:
      getAllPieces in interface PieceCollection
      Returns:
      a list of all pieces in any layer of this collection.
    • indexOf

      public int indexOf(GamePiece p)
      Description copied from interface: PieceCollection
      Returns the index of a piece. When painting the map, pieces are drawn in order of index -- lowest index drawn first and therefore appearing "below" later pieces which are drawn on top of it.
      Specified by:
      indexOf in interface PieceCollection
      Parameters:
      p - A game piece
      Returns:
      A unique index for the game piece within the overall collection.
    • remove

      public void remove(GamePiece p)
      Description copied from interface: PieceCollection
      Removes the piece
      Specified by:
      remove in interface PieceCollection
      Parameters:
      p - piece to remove from the collection
    • moveToBack

      public void moveToBack(GamePiece p)
      Description copied from interface: PieceCollection
      Reposition a piece to the back of all others in the same visual layer
      Specified by:
      moveToBack in interface PieceCollection
      Parameters:
      p - piece to move to the visual back of its layer
    • moveToFront

      public void moveToFront(GamePiece p)
      Description copied from interface: PieceCollection
      Reposition a piece to the front of all others in the same visual layer
      Specified by:
      moveToFront in interface PieceCollection
      Parameters:
      p - piece to move to visual front of its layer
    • canMerge

      public boolean canMerge(GamePiece p1, GamePiece p2)
      Used when moving a piece on top of another piece to determine whether they can be merged together (e.g. can this Piece be added to that Stack, can this Stack be added to that Deck, can these two Stacks be combined together into one). Default preconditions for merging into a Stack include not being Invisible due to a Hideable trait, and not having a "Does Not Stack" trait (Immobilized). When extending the class additional requirements can be added (e.g. LayeredPieceCollection requires pieces to be in the same visual layer in order to form a stack).
      Specified by:
      canMerge in interface PieceCollection
      Parameters:
      p1 - one piece/stack/deck
      p2 - another piece/stack/deck
      Returns:
      Whether the two pieces are legal to merge
    • canStacksMerge

      protected boolean canStacksMerge(Stack s1, Stack s2)
      Stacks can merge if their component pieces can merge
      Parameters:
      s1 - stack
      s2 - another stack
      Returns:
      can they make one stack?
    • canStackAndPieceMerge

      protected boolean canStackAndPieceMerge(Stack s, GamePiece p)
      A piece can be merged into a stack if it is merge-compatible with the first piece in the stack.
      Parameters:
      s - stack
      p - piece
      Returns:
      can they make one stack?
    • canPiecesMerge

      protected boolean canPiecesMerge(GamePiece p1, GamePiece p2)
      Two pieces can merge in the default case as long as neither is presently invisible and neither has a Does Not Stack (Hideable) trait.
      Parameters:
      p1 - a piece
      p2 - another piece
      Returns:
      WonderTwin powers activate?
    • getLayerCount

      public int getLayerCount()
      Returns:
      the number of visual layers in this collection
    • setBottomLayer

      public void setBottomLayer(int layer)
    • getBottomLayer

      public int getBottomLayer()
      Returns:
      current bottom layer
    • getTopLayer

      public int getTopLayer()
      Returns:
      the current top layer
    • rotate

      public void rotate(boolean rotateUp, boolean skipNullLayers)
    • rotate

      public void rotate(boolean rotateUp)
    • setLayerEnabled

      public void setLayerEnabled(int layer, boolean b)
      Enable/Disable layers
      Parameters:
      layer - layer index
      b - true to enable the layer, false to disable
    • toggleLayerEnabled

      public void toggleLayerEnabled(int layer)
      Toggle for Enable/Disable of layers
      Parameters:
      layer - layer index
    • setLayerEnabled

      public void setLayerEnabled(String layer, boolean b)
      Enable/Disable layers
      Parameters:
      layer - layer name
      b - true to enable the layer, false to disable
    • toggleLayerEnabled

      public void toggleLayerEnabled(String layer)
      Toggle for Enable/Disable of layers
      Parameters:
      layer - layer name
    • reset

      public void reset()