Advertisement
dermetfan

AnimatedSprite (LibGDX)

Jul 7th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. /**
  2.  * Copyright 2013 Robin Stumm (serverkorken@googlemail.com, http://dermetfan.bplaced.net)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package net.dermetfan.libgdx.graphics;
  18.  
  19. import com.badlogic.gdx.Gdx;
  20. import com.badlogic.gdx.Graphics;
  21. import com.badlogic.gdx.graphics.Texture;
  22. import com.badlogic.gdx.graphics.g2d.Animation;
  23. import com.badlogic.gdx.graphics.g2d.Sprite;
  24. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  25.  
  26. /**
  27.  * An {@link AnimatedSprite} holds an {@link Animation} and sets the {@link Texture} of its super type {@link Sprite} to the correct one according to the information in the {@link Animation}.
  28.  * Usage:
  29.  * <pre>Animation animation = new Animation(1 / 3f, new TextureRegion(frame1), new TextureRegion(frame2), new TextureRegion(frame3));
  30. animation.setPlayMode(Animation.LOOP);
  31. animatedSprite = new AnimatedSprite(animation);</pre>
  32.  * Draw it using any of the {@link Sprite Sprite's} draw methods:
  33. <pre>animatedSprite.draw(batch);</pre>
  34.  *
  35.  * @author dermetfan
  36.  */
  37. public class AnimatedSprite extends Sprite {
  38.  
  39.     /** the {@link Animation} to display */
  40.     private Animation animation;
  41.  
  42.     /** the current time of the {@link Animation} */
  43.     private float time;
  44.  
  45.     /** if the animation is playing */
  46.     private boolean playing = true;
  47.  
  48.     /** if the animation should be updated every time it's drawn */
  49.     private boolean autoUpdate = true;
  50.  
  51.     /** if the size of the first frame should be kept by the following frames */
  52.     private boolean keepSize;
  53.  
  54.     /**
  55.      * creates a new {@link AnimatedSprite} with the given {@link Animation}
  56.      * @param animation the {@link #animation} to use
  57.      */
  58.     public AnimatedSprite(Animation animation) {
  59.         this(animation, false);
  60.     }
  61.  
  62.     /**
  63.      * creates a new {@link AnimatedSprite} with the given {@link Animation}
  64.      * @param animation the {@link #animation} to use
  65.      * @param keepSize the {@link #keepSize} to use
  66.      */
  67.     public AnimatedSprite(Animation animation, boolean keepSize) {
  68.         super(animation.getKeyFrame(0));
  69.         this.animation = animation;
  70.         this.keepSize = keepSize;
  71.     }
  72.  
  73.     /** updates the {@link AnimatedSprite} with the delta time fetched from {@link Graphics#getDeltaTime()  Gdx.graphics.getDeltaTime()} */
  74.     public void update() {
  75.         update(Gdx.graphics.getDeltaTime());
  76.     }
  77.  
  78.     /** updates the {@link AnimatedSprite} with the given delta time */
  79.     public void update(float delta) {
  80.         if(playing) {
  81.             setRegion(animation.getKeyFrame(time += delta).getTexture());
  82.             if(!keepSize)
  83.                 setSize(getRegionWidth(), getRegionHeight());
  84.         }
  85.     }
  86.  
  87.     @Override
  88.     public void draw(SpriteBatch spriteBatch) {
  89.         if(autoUpdate)
  90.             update();
  91.         super.draw(spriteBatch);
  92.     }
  93.  
  94.     @Override
  95.     public void draw(SpriteBatch spriteBatch, float alphaModulation) {
  96.         if(autoUpdate)
  97.             update();
  98.         super.draw(spriteBatch, alphaModulation);
  99.     }
  100.  
  101.     /** sets {@link #playing} to true */
  102.     public void play() {
  103.         playing = true;
  104.     }
  105.  
  106.     /** sets {@link #playing} to false */
  107.     public void pause() {
  108.         playing = false;
  109.     }
  110.  
  111.     /** pauses and sets the {@link #time} to 0 */
  112.     public void stop() {
  113.         playing = false;
  114.         time = 0;
  115.     }
  116.  
  117.     /** @param time the {@link #time} to go to */
  118.     public void setTime(float time) {
  119.         this.time = time;
  120.     }
  121.  
  122.     /** @return the current {@link #time} */
  123.     public float getTime() {
  124.         return time;
  125.     }
  126.  
  127.     /** @return the {@link #animation} */
  128.     public Animation getAnimation() {
  129.         return animation;
  130.     }
  131.  
  132.     /** @param animation the {@link #animation} to set */
  133.     public void setAnimation(Animation animation) {
  134.         this.animation = animation;
  135.     }
  136.  
  137.     /** @return if this {@link AnimatedSprite} is playing */
  138.     public boolean isPlaying() {
  139.         return playing;
  140.     }
  141.  
  142.     /** @param playing if the {@link AnimatedSprite} should be playing */
  143.     public void setPlaying(boolean playing) {
  144.         this.playing = playing;
  145.     }
  146.  
  147.     /** @return if the {@link #animation} has finished playing */
  148.     public boolean isAnimationFinished() {
  149.         return animation.isAnimationFinished(time);
  150.     }
  151.  
  152.     /** @return the {@link #autoUpdate} */
  153.     public boolean isAutoUpdate() {
  154.         return autoUpdate;
  155.     }
  156.  
  157.     /** @param autoUpdate the {@link #autoUpdate} to set */
  158.     public void setAutoUpdate(boolean autoUpdate) {
  159.         this.autoUpdate = autoUpdate;
  160.     }
  161.  
  162.     /** @return the {{@link #keepSize} */
  163.     public boolean isKeepSize() {
  164.         return keepSize;
  165.     }
  166.  
  167.     /** @param keepSize the {@link #keepSize} to set */
  168.     public void setKeepSize(boolean keepSize) {
  169.         this.keepSize = keepSize;
  170.     }
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement