Class Chain

java.lang.Object
net.dermetfan.gdx.physics.box2d.Chain

public class Chain extends Object
Holds segments and connections to simulate a chain. Also provides modification methods that use a Chain.Builder.
  • Constructor Details

    • Chain

      public Chain(Chain other)
      creates a shallow copy of the given Chain instance
      Parameters:
      other - the Chain to copy
    • Chain

      public Chain(Chain.Builder builder)
      Parameters:
      builder - the builder
    • Chain

      public Chain(int length, Chain.Builder builder)
      creates a new Chain and builds it to the given length
      Parameters:
      length - the desired length of this Chain
      builder - the builder
      See Also:
    • Chain

      public Chain(int length, Chain.Builder builder, boolean build)
      creates a new Chain and builds it to the given length if build is true
      Parameters:
      length - The desired length of this Chain. Will be ignored if build is false.
      builder - the builder
      build - if this Chain should be build to the given length
    • Chain

      public Chain(Chain.Builder builder, com.badlogic.gdx.physics.box2d.Body... segments)
      creates a new Chain with the given segments
      Parameters:
      builder - the builder
      segments - the segments
  • Method Details

    • extend

      public Chain extend(int length)
      extends this chain by the given length using the builder
      See Also:
    • extend

      public Chain extend(int length, Chain.Builder builder)
      extends this chain by the given length using the given Chain.Builder
      See Also:
    • extend

      public com.badlogic.gdx.physics.box2d.Body extend()
      extends this Chain using the builder
      See Also:
    • extend

      public com.badlogic.gdx.physics.box2d.Body extend(Chain.Builder builder)
      creates and adds a new segment to this Chain
      Returns:
      the created and added segment
    • shorten

      public void shorten()
      destorys the last segment
    • shorten

      public void shorten(int length)
      destroys the given amount of segments from the end of the Chain
    • createSegment

      public com.badlogic.gdx.physics.box2d.Body createSegment(int index)
      creates a segment for the given index using the builder
      See Also:
    • createSegment

      public com.badlogic.gdx.physics.box2d.Body createSegment(int index, Chain.Builder builder)
      Creates a segment using the given Chain.Builder passing the correct parameters to Chain.Builder.createSegment(int, int, Chain) specified by the given index. Does NOT add it to this Chain.
      See Also:
    • createConnection

      public Chain.Connection createConnection(int segmentIndex1, int segmentIndex2)
      See Also:
    • createConnection

      public Chain.Connection createConnection(int segmentIndex1, int segmentIndex2, Chain.Builder builder)
      Creates a Chain.Connection using the Chain.Builder passing the correct parameters to Chain.Builder.createConnection(Body, int, Body, int) specified by the given indices. Does NOT add it to this Chain.
      See Also:
    • add

      public void add(com.badlogic.gdx.physics.box2d.Body segment)
      adds the given segment to the end of this Chain
      Parameters:
      segment - the segment to add
    • add

      public void add(com.badlogic.gdx.physics.box2d.Body... segments)
      adds the given segments to the end of this Chain
      See Also:
    • insert

      public com.badlogic.gdx.physics.box2d.Body insert(int index)
      creates a new segment and inserts it into this Chain
      See Also:
    • insert

      public void insert(int index, com.badlogic.gdx.physics.box2d.Body segment)
      inserts a segment into this Chain
      Parameters:
      index - the index at which to insert the given segment
      segment - the segment to insert
    • replace

      public com.badlogic.gdx.physics.box2d.Body replace(int index, com.badlogic.gdx.physics.box2d.Body segment)
      Parameters:
      index - the index of the segment to replace
      segment - the segment to insert
      Returns:
      the segment that was at the given index previously
    • remove

      public com.badlogic.gdx.physics.box2d.Body remove(com.badlogic.gdx.physics.box2d.Body segment)
      Parameters:
      segment - The Body to remove. Must be a segment of this Chain.
      Returns:
      the given body
      See Also:
    • remove

      public com.badlogic.gdx.physics.box2d.Body remove(int index)
      removes a segment from this Chain
      Parameters:
      index - the index of the segment to remove
      Returns:
      the removed segment
      See Also:
      • Array.removeIndex(int)
    • remove

      public com.badlogic.gdx.utils.Array<com.badlogic.gdx.physics.box2d.Body> remove(int beginIndex, int endIndex)
      removes all segmetns from beginIndex to endIndex
      Parameters:
      beginIndex - the first index to remove
      endIndex - the last index to remove
      Returns:
      an Array holding the removed segments
      See Also:
      • tmpSegments
    • destroy

      public void destroy(com.badlogic.gdx.physics.box2d.Body segment)
      Parameters:
      segment - the segment to destroy
      See Also:
    • destroy

      public void destroy(int index)
      Parameters:
      index - the index of the segment to destroy
      See Also:
    • destroy

      public void destroy(int beginIndex, int endIndex)
      destroys all segments from beginIndex to endIndex
      Parameters:
      beginIndex - the first index to destroy
      endIndex - the last index to destroy
    • split

      public Chain split(Chain.Connection connection)
      Parameters:
      connection - the Chain.Connection in connections to split
      Returns:
      the new Chain
      See Also:
    • split

      public Chain split(int connectionIndex)
      splits this Chain at the given index and returns a new Chain consisting of the segments up to the given index
      Parameters:
      connectionIndex - the index of the connection to destroy
      Returns:
      a Chain consisting of the segments before the given index
    • length

      public int length()
      Returns:
      the amount of segments in this Chain
      See Also:
      • Array.size
    • getSegment

      public com.badlogic.gdx.physics.box2d.Body getSegment(int index)
      Parameters:
      index - the index of the desired segment
      Returns:
      the segment at the given index
    • getConnection

      public Chain.Connection getConnection(int index)
      Parameters:
      index - the index of the desired Joint
      Returns:
      the Joint at the given index
    • getSegments

      public com.badlogic.gdx.utils.Array<com.badlogic.gdx.physics.box2d.Body> getSegments()
      Warning: The returned Array is used by other methods and may be modified unexpectedly. It's highly recommended to add its items to your own collection.
      Returns:
      the segments
      See Also:
      • tmpSegments
    • getConnections

      public com.badlogic.gdx.utils.Array<Chain.Connection> getConnections()
      Warning: The returned Array is used by other methods and may be modified unexpectedly. It's highly recommended to add its items to your own collection.
      Returns:
      an Array representing the current state of connections
      See Also:
      • tmpConnections
    • getBuilder

      public Chain.Builder getBuilder()
      Returns:
      the builder
    • setBuilder

      public void setBuilder(Chain.Builder builder)
      Parameters:
      builder - the builder to set