Package com.deco2800.game.rendering
Class AnimationRenderComponent
java.lang.Object
com.deco2800.game.components.Component
com.deco2800.game.rendering.RenderComponent
com.deco2800.game.rendering.AnimationRenderComponent
- All Implemented Interfaces:
Disposable
,Renderable
,java.lang.Comparable<Renderable>
public class AnimationRenderComponent extends RenderComponent
Renders animations from a texture atlas on an entity.
- libgdx texture packer (included in External Libraries/gdx-tools)
- gdx-texture-packer-gui (recommended) https://github.com/crashinvaders/gdx-texture-packer-gui
- other third-party tools, e.g. https://www.codeandweb.com/texturepacker
Example usage:
AnimationRenderComponent animator = new AnimationRenderComponent("player.atlas"); entity.addComponent(animator); animator.addAnimation("attack", 0.1f); // Only need to add animation once per entity animator.startAnimation("attack");Texture atlases can be created using:
- libgdx texture packer (included in External Libraries/gdx-tools)
- gdx-texture-packer-gui (recommended) https://github.com/crashinvaders/gdx-texture-packer-gui
- other third-party tools, e.g. https://www.codeandweb.com/texturepacker
-
Field Summary
-
Constructor Summary
Constructors Constructor Description AnimationRenderComponent(TextureAtlas atlas)
Create the component for a given texture atlas. -
Method Summary
Modifier and Type Method Description boolean
addAnimation(java.lang.String name, float frameDuration)
Register an animation from the texture atlas.boolean
addAnimation(java.lang.String name, float frameDuration, Animation.PlayMode playMode)
Register an animation from the texture atlas.void
dispose()
Called when the component is disposed.protected void
draw(SpriteBatch batch)
Draw the renderable.float
getAnimationPlayTime()
Get the playing time of the animationjava.lang.String
getCurrentAnimation()
Get the name of the animation currently being played.boolean
hasAnimation(java.lang.String name)
Whether the animator has added the given animation.boolean
isFinished()
Has the playing animation finished? This will always be false for looping animations.boolean
removeAnimation(java.lang.String name)
Remove an animation from this animator.void
scaleEntity()
Scale the entity to a width of 1 and a height matching the texture's ratiovoid
startAnimation(java.lang.String name)
Start playback of an animation.boolean
stopAnimation()
Stop the currently running animation.Methods inherited from class com.deco2800.game.rendering.RenderComponent
compareTo, create, getLayer, getZIndex, render
Methods inherited from class com.deco2800.game.components.Component
earlyUpdate, getEntity, setEnabled, setEntity, toString, triggerEarlyUpdate, triggerUpdate, update
-
Constructor Details
-
AnimationRenderComponent
Create the component for a given texture atlas.- Parameters:
atlas
- libGDX-supported texture atlas containing desired animations
-
-
Method Details
-
addAnimation
public boolean addAnimation(java.lang.String name, float frameDuration)Register an animation from the texture atlas. Will play once when called with startAnimation()- Parameters:
name
- Name of the animation. Must match the name of this animation inside the texture atlas.frameDuration
- How long, in seconds, to show each frame of the animation for when playing- Returns:
- true if added successfully, false otherwise
-
addAnimation
public boolean addAnimation(java.lang.String name, float frameDuration, Animation.PlayMode playMode)Register an animation from the texture atlas.- Parameters:
name
- Name of the animation. Must match the name of this animation inside the texture atlas.frameDuration
- How long, in seconds, to show each frame of the animation for when playingplayMode
- How the animation should be played (e.g. looping, backwards)- Returns:
- true if added successfully, false otherwise
-
scaleEntity
public void scaleEntity()Scale the entity to a width of 1 and a height matching the texture's ratio -
removeAnimation
public boolean removeAnimation(java.lang.String name)Remove an animation from this animator. This is not required before disposing.- Parameters:
name
- Name of the previously added animation.- Returns:
- true if removed, false if animation was not found.
-
hasAnimation
public boolean hasAnimation(java.lang.String name)Whether the animator has added the given animation.- Parameters:
name
- Name of the added animation.- Returns:
- true if added, false otherwise.
-
startAnimation
public void startAnimation(java.lang.String name)Start playback of an animation. The animation must have been added using addAnimation().- Parameters:
name
- Name of the animation to play.
-
stopAnimation
public boolean stopAnimation()Stop the currently running animation. Does nothing if no animation is playing.- Returns:
- true if animation was stopped, false if no animation is playing.
-
getCurrentAnimation
public java.lang.String getCurrentAnimation()Get the name of the animation currently being played.- Returns:
- current animation name, or null if not playing.
-
isFinished
public boolean isFinished()Has the playing animation finished? This will always be false for looping animations.- Returns:
- true if animation was playing and has now finished, false otherwise.
-
getAnimationPlayTime
public float getAnimationPlayTime()Get the playing time of the animation- Returns:
- float The time the animation has been played (seconds).
-
draw
Description copied from class:RenderComponent
Draw the renderable. Should be called only by the renderer, not manually.- Specified by:
draw
in classRenderComponent
- Parameters:
batch
- Batch to render to.
-
dispose
public void dispose()Description copied from class:Component
Called when the component is disposed. Dispose of any internal resources here.- Specified by:
dispose
in interfaceDisposable
- Overrides:
dispose
in classRenderComponent
-