Class AnimationRenderComponent

All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable, Renderable, Comparable<Renderable>

public class AnimationRenderComponent extends RenderComponent
Renders animations from a texture atlas on an entity.

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
  • 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
  • Method Details

    • addAnimation

      public boolean addAnimation(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(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 playing
      playMode - 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(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(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(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 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.
    • 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 class RenderComponent
      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 interface com.badlogic.gdx.utils.Disposable
      Overrides:
      dispose in class RenderComponent