Class ObstacleSprite

java.lang.Object
edu.cornell.gdiac.physics2.ObstacleSprite
All Implemented Interfaces:
ObstacleData

public class ObstacleSprite extends Object implements ObstacleData
This class is a sprite attached to a Box2d obstacle.

This class serves two purposes. The first is to simplify the process of synchronizing a sprite position and orientation to a Box2d object. This class has a draw method that will draw the sprite in the Box2d coordinate space, accounting for physic units.

The second purpose this class is to provide a polymorphic wrapper for obstacle sprites, allowing us to organize many of our game objects into a single container.

While this is not quite an abstract class, this class does little to no initialization by itself. It is expected to be subclassed.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected com.badlogic.gdx.graphics.Color
    The line color for debug drawing
    protected SpriteMesh
    The mesh defining this particular sprite
    protected Obstacle
    The actual physics objects
    protected SpriteSheet
    The sprite sheet for drawing this particular physics object
    protected com.badlogic.gdx.math.Affine2
    An affine transform for drawing
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new obstacle sprite
    Creates a new obstacle sprite about the given obstacle
    ObstacleSprite(Obstacle obstacle, boolean mesh)
    Creates a new obstacle sprite about the given obstacle
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Draws this sprite to the given sprite batch
    void
    draw(SpriteBatch batch, com.badlogic.gdx.math.Affine2 affine)
    Draws this sprite to the given sprite batch
    void
    Draws a debug wireframe to the given sprite batch
    void
    drawDebug(SpriteBatch batch, com.badlogic.gdx.math.Affine2 affine)
    Draws a debug wireframe to the given sprite batch
    com.badlogic.gdx.graphics.Color
    Returns the debug color of this sprite
    Returns the sprite mesh associated with the obstacle.
    Returns the name of the obstacle associated with this sprite.
    Returns the obstacle associated with this sprite.
    com.badlogic.gdx.graphics.g2d.TextureRegion
    Returns a reference to the associated sprite sheet
    void
    setDebugColor(com.badlogic.gdx.graphics.Color color)
    Sets the debug color of this sprite
    void
    Sets the obstacle associated with this sprite.
    void
    Sets the sprite sheet for this sprite.
    void
    setTexture(com.badlogic.gdx.graphics.Texture texture)
    Sets the texture for this sprite.
    void
    setTextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion region)
    Sets the texture region for this sprite.
    void
    update(float dt)
    Updates this object when the associated obstacle is updated.

    Methods inherited from class java.lang.Object

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

    • sprite

      protected SpriteSheet sprite
      The sprite sheet for drawing this particular physics object
    • mesh

      protected SpriteMesh mesh
      The mesh defining this particular sprite
    • obstacle

      protected Obstacle obstacle
      The actual physics objects
    • debug

      protected com.badlogic.gdx.graphics.Color debug
      The line color for debug drawing
    • transform

      protected com.badlogic.gdx.math.Affine2 transform
      An affine transform for drawing
  • Constructor Details

    • ObstacleSprite

      public ObstacleSprite()
      Creates a new obstacle sprite

      The texture region and sprite mesh will be empty and the obstacle will be null. Create a subclass of this class to initialize these values.

    • ObstacleSprite

      public ObstacleSprite(Obstacle obstacle)
      Creates a new obstacle sprite about the given obstacle

      The mesh will be computed automatically from the outline of the obstacle. In order to work correctly, the obstacle must have the correct physics units.

      Parameters:
      obstacle - The obstacle to wrap
    • ObstacleSprite

      public ObstacleSprite(Obstacle obstacle, boolean mesh)
      Creates a new obstacle sprite about the given obstacle

      If mesh is true, then a sprite mesh will be computed automatically from the outline of the obstacle. In order to work correctly, the obstacle must have the correct physics units.

      Parameters:
      obstacle - The obstacle to wrap
      mesh - Whether to construct a mesh from the obstacle
  • Method Details

    • getName

      public String getName()
      Returns the name of the obstacle associated with this sprite. If the obstacle is null, this method will also return null.
      Returns:
      the name of the obstacle associated with this sprite.
    • getObstacle

      public Obstacle getObstacle()
      Returns the obstacle associated with this sprite. If this is null, no image will be drawn even if the sprite and mesh are present.
      Returns:
      the obstacle associated with this sprite.
    • setObstacle

      public void setObstacle(Obstacle obstacle)
      Sets the obstacle associated with this sprite. If this is null, no image will be drawn even if the sprite and mesh are present.
      Parameters:
      obstacle - The obstacle associated with this sprite.
    • getMesh

      public SpriteMesh getMesh()
      Returns the sprite mesh associated with the obstacle. The sprite mesh will be positioned and oriented according to the current obstacle position and orientation. If the obstacle is null, no image will be drawn. This attribute has no associated setter. Any necessary changes should be made directly to the mesh.
      Returns:
      the sprite mesh associated with the obstacle.
    • getSpriteSheet

      public com.badlogic.gdx.graphics.g2d.TextureRegion getSpriteSheet()
      Returns a reference to the associated sprite sheet

      Modifying this sprite sheet (particularly SpriteSheet.setFrame(int) will affect the associated draw(edu.cornell.gdiac.graphics.SpriteBatch) commands.

      Returns:
      a reference to the associated sprite sheet
    • setTexture

      public void setTexture(com.badlogic.gdx.graphics.Texture texture)
      Sets the texture for this sprite.

      Calling this method will set the sprite sheet to be a single frame encompasing the entire provided texture. For more fine control, create a SpriteSheet and use setSpriteSheet(edu.cornell.gdiac.graphics.SpriteSheet).

      Parameters:
      texture - The sprite texture
    • setTextureRegion

      public void setTextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion region)
      Sets the texture region for this sprite.

      Calling this method will set the sprite to use the given texture region. This texture region is still interpretted as a single animation frame. For more fine control, create a SpriteSheet and use setSpriteSheet(edu.cornell.gdiac.graphics.SpriteSheet).

      Parameters:
      region - The sprite texture region
    • setSpriteSheet

      public void setSpriteSheet(SpriteSheet sheet)
      Sets the sprite sheet for this sprite.

      This method will copy the sprite sheet and not retain a copy. Further maodifications to the original sprite sheet will have no effect on this object.

      Parameters:
      sheet - The sprite sheet
    • getDebugColor

      public com.badlogic.gdx.graphics.Color getDebugColor()
      Returns the debug color of this sprite

      Obstacle sprites have a method called drawDebug(edu.cornell.gdiac.graphics.SpriteBatch) which draws a wireframe of the obstacle, so that it is easier to see collisions. This color is the color of that wireframe.

      Returns:
      the debug color of this sprite
    • setDebugColor

      public void setDebugColor(com.badlogic.gdx.graphics.Color color)
      Sets the debug color of this sprite

      Obstacle sprites have a method called drawDebug(edu.cornell.gdiac.graphics.SpriteBatch) which draws a wireframe of the obstacle, so that it is easier to see collisions. This color is the color of that wireframe.

      Parameters:
      color - the debug color of this sprite
    • draw

      public void draw(SpriteBatch batch)
      Draws this sprite to the given sprite batch

      The sprite will be drawn in the Box2d world coordinate system, scaled by the appropriate physics units.

      Parameters:
      batch - The sprite batch to draw with
    • draw

      public void draw(SpriteBatch batch, com.badlogic.gdx.math.Affine2 affine)
      Draws this sprite to the given sprite batch

      The native coordinate system of a sprite is the Box2d world coordinate system, scaled by the appropriate physics units. This method modifies that coordinate system by the given affine transform.

      Parameters:
      batch - The sprite batch to draw with
      affine - The coordinate transform
    • drawDebug

      public void drawDebug(SpriteBatch batch)
      Draws a debug wireframe to the given sprite batch

      The wireframe will outline the Box2d fixtures, making it easier to see debug collisions. The wireframe will have getDebugColor() as its color. The wireframe will be drawn in the Box2d world coordinate system, scaled by the appropriate physics units.

      Parameters:
      batch - The sprite batch to draw with
    • drawDebug

      public void drawDebug(SpriteBatch batch, com.badlogic.gdx.math.Affine2 affine)
      Draws a debug wireframe to the given sprite batch

      The wireframe will outline the Box2d fixtures, making it easier to see debug collisions. The wireframe will have getDebugColor() as its color. The native coordinate system of a sprite is the Box2d world coordinate system, scaled by the appropriate physics units. This method modifies that coordinate system by the given affine transform.

      Parameters:
      batch - The sprite batch to draw with
      affine - The coordinate transform
    • update

      public void update(float dt)
      Updates this object when the associated obstacle is updated.

      This method is called by Obstacle.update(float). It allows us to notify an associated object that the obstacle has been updated.

      Specified by:
      update in interface ObstacleData
      Parameters:
      dt - The time since this last call to update