Class AnimationRenderComponent

All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable, com.badlogic.gdx.utils.Json.Serializable, Renderable, Comparable<Renderable>
Direct Known Subclasses:
AnimationEffectsComponent

public class AnimationRenderComponent extends RenderComponent
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 Details

    • timeSource

      protected final GameTime 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 animations
      scaleFactor - 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 class RenderComponent
    • 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 using the texture's ration and configured scale factor
    • 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.
    • 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 class RenderComponent
      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 interface Renderable
      Overrides:
      getLayer in class RenderComponent
      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 interface com.badlogic.gdx.utils.Disposable
      Overrides:
      dispose in class RenderComponent