Package com.csse3200.game.rendering
Class AnimationRenderComponent
java.lang.Object
com.csse3200.game.components.Component
com.csse3200.game.rendering.RenderComponent
com.csse3200.game.rendering.AnimationRenderComponent
- All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable
,Renderable
,Comparable<Renderable>
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
ConstructorDescriptionAnimationRenderComponent
(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas) Create the component for a given texture atlas.AnimationRenderComponent
(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas1, com.badlogic.gdx.graphics.g2d.TextureAtlas atlas2) -
Method Summary
Modifier and TypeMethodDescriptionboolean
addAnimation
(String name, float frameDuration) Register an animation from the texture atlas.boolean
addAnimation
(String name, float frameDuration, com.badlogic.gdx.graphics.g2d.Animation.PlayMode playMode) Register an animation from the texture atlas.void
dispose()
Called when the component is disposed.protected void
draw
(com.badlogic.gdx.graphics.g2d.SpriteBatch batch) Draw the renderable.Get the name of the animation currently being played.boolean
hasAnimation
(String name) Whether the animator has added the given animation.boolean
Has the playing animation finished? This will always be false for looping animations.boolean
removeAnimation
(String name) Remove an animation from this animator.void
Scale the entity to a width of 1 and a height matching the texture's ratiovoid
startAnimation
(String name) Start playback of an animation.boolean
Stop the currently running animation.Methods inherited from class com.csse3200.game.rendering.RenderComponent
compareTo, create, getLayer, getZIndex, render
Methods inherited from class com.csse3200.game.components.Component
earlyUpdate, getEntity, setEnabled, setEntity, toString, triggerEarlyUpdate, triggerUpdate, update
-
Constructor Details
-
AnimationRenderComponent
public AnimationRenderComponent(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas) Create the component for a given texture atlas.- Parameters:
atlas
- libGDX-supported texture atlas containing desired animations
-
AnimationRenderComponent
public AnimationRenderComponent(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas1, com.badlogic.gdx.graphics.g2d.TextureAtlas atlas2)
-
-
Method Details
-
addAnimation
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(String name, float frameDuration, com.badlogic.gdx.graphics.g2d.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
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
Whether the animator has added the given animation.- Parameters:
name
- Name of the added animation.- Returns:
- true if added, false otherwise.
-
startAnimation
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
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.
-
draw
protected void draw(com.badlogic.gdx.graphics.g2d.SpriteBatch batch) 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 interfacecom.badlogic.gdx.utils.Disposable
- Overrides:
dispose
in classRenderComponent
-