Class ObstacleSprite
- All Implemented Interfaces:
ObstacleData
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
FieldsModifier and TypeFieldDescriptionprotected com.badlogic.gdx.graphics.ColorThe line color for debug drawingprotected SpriteMeshThe mesh defining this particular spriteprotected ObstacleThe actual physics objectsprotected SpriteSheetThe sprite sheet for drawing this particular physics objectprotected com.badlogic.gdx.math.Affine2An affine transform for drawing -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new obstacle spriteObstacleSprite(Obstacle obstacle) Creates a new obstacle sprite about the given obstacleObstacleSprite(Obstacle obstacle, boolean mesh) Creates a new obstacle sprite about the given obstacle -
Method Summary
Modifier and TypeMethodDescriptionvoiddraw(SpriteBatch batch) Draws this sprite to the given sprite batchvoiddraw(SpriteBatch batch, com.badlogic.gdx.math.Affine2 affine) Draws this sprite to the given sprite batchvoiddrawDebug(SpriteBatch batch) Draws a debug wireframe to the given sprite batchvoiddrawDebug(SpriteBatch batch, com.badlogic.gdx.math.Affine2 affine) Draws a debug wireframe to the given sprite batchcom.badlogic.gdx.graphics.ColorReturns the debug color of this spritegetMesh()Returns the sprite mesh associated with the obstacle.getName()Returns the name of the obstacle associated with this sprite.Returns the obstacle associated with this sprite.com.badlogic.gdx.graphics.g2d.TextureRegionReturns a reference to the associated sprite sheetvoidsetDebugColor(com.badlogic.gdx.graphics.Color color) Sets the debug color of this spritevoidsetObstacle(Obstacle obstacle) Sets the obstacle associated with this sprite.voidsetSpriteSheet(SpriteSheet sheet) Sets the sprite sheet for this sprite.voidsetTexture(com.badlogic.gdx.graphics.Texture texture) Sets the texture for this sprite.voidsetTextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion region) Sets the texture region for this sprite.voidupdate(float dt) Updates this object when the associated obstacle is updated.
-
Field Details
-
sprite
The sprite sheet for drawing this particular physics object -
mesh
The mesh defining this particular sprite -
obstacle
The actual physics objects -
debug
protected com.badlogic.gdx.graphics.Color debugThe line color for debug drawing -
transform
protected com.badlogic.gdx.math.Affine2 transformAn affine transform for drawing
-
-
Constructor Details
-
ObstacleSprite
public ObstacleSprite()Creates a new obstacle spriteThe 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
Creates a new obstacle sprite about the given obstacleThe 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
Creates a new obstacle sprite about the given obstacleIf 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 wrapmesh- Whether to construct a mesh from the obstacle
-
-
Method Details
-
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
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
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
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 sheetModifying this sprite sheet (particularly
SpriteSheet.setFrame(int)will affect the associateddraw(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
SpriteSheetand usesetSpriteSheet(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
SpriteSheetand usesetSpriteSheet(edu.cornell.gdiac.graphics.SpriteSheet).- Parameters:
region- The sprite texture region
-
setSpriteSheet
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 spriteObstacle 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 spriteObstacle 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
Draws this sprite to the given sprite batchThe 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
Draws this sprite to the given sprite batchThe 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 withaffine- The coordinate transform
-
drawDebug
Draws a debug wireframe to the given sprite batchThe 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
Draws a debug wireframe to the given sprite batchThe 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 withaffine- 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:
updatein interfaceObstacleData- Parameters:
dt- The time since this last call to update
-