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
,com.badlogic.gdx.utils.Json.Serializable
,Renderable
,Comparable<Renderable>
- Direct Known Subclasses:
AnimationEffectsComponent
Renders animations from a texture atlas on an entity.
Example usage:
AnimationRenderComponent animator = new AnimationRenderComponent("player.atlas", 16f); 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
FieldsModifier and TypeFieldDescriptionprotected float
protected com.badlogic.gdx.graphics.g2d.Animation<com.badlogic.gdx.graphics.g2d.TextureRegion>
protected final GameTime
-
Constructor Summary
ConstructorsConstructorDescriptionAnimationRenderComponent
(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas) Create the component for a given texture atlas.AnimationRenderComponent
(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas, float scaleFactor) Create the component for a given texture atlas.AnimationRenderComponent
(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas, float scaleFactor, int layer) -
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
create()
Called when the entity is created and registered.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.int
getLayer()
Layer to be rendered in.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 using the texture's ration and configured scale factorvoid
startAnimation
(String name) Start playback of an animation.boolean
Stop the currently running animation.void
Methods inherited from class com.csse3200.game.rendering.RenderComponent
compareTo, getZIndex, render
Methods inherited from class com.csse3200.game.components.Component
earlyUpdate, getEntity, read, setEnabled, setEntity, toString, triggerEarlyUpdate, triggerUpdate, update, write
-
Field Details
-
timeSource
-
currentAnimation
protected com.badlogic.gdx.graphics.g2d.Animation<com.badlogic.gdx.graphics.g2d.TextureRegion> currentAnimation -
animationPlayTime
protected float animationPlayTime
-
-
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 atlas, float scaleFactor) Create the component for a given texture atlas.- Parameters:
atlas
- libGDX-supported texture atlas containing desired animationsscaleFactor
- factor for scaling the atlas region when drawing animations
-
AnimationRenderComponent
public AnimationRenderComponent(com.badlogic.gdx.graphics.g2d.TextureAtlas atlas, float scaleFactor, int layer)
-
-
Method Details
-
create
public void create()Description copied from class:Component
Called when the entity is created and registered. Initial logic such as calls to GetComponent should be made here, not in the constructor which is called before an entity is finished.- Overrides:
create
in classRenderComponent
-
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 using the texture's ration and configured scale factor -
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.
-
togglePauseAnimation
public void togglePauseAnimation() -
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.
-
getLayer
public int getLayer()Description copied from interface:Renderable
Layer to be rendered in. Higher layers will be rendered on top of lower layers.- Specified by:
getLayer
in interfaceRenderable
- Overrides:
getLayer
in classRenderComponent
- Returns:
- layer
-
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
-