Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package dk.grp1.tanks.core.internal.managers;
  2.  
  3. import com.badlogic.gdx.graphics.Pixmap;
  4. import com.badlogic.gdx.graphics.Texture;
  5. import com.badlogic.gdx.graphics.g2d.Animation;
  6. import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;
  7. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  8. import dk.grp1.tanks.core.internal.AnimationWrapper;
  9.  
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14.  
  15. public class GameAssetManager {
  16.     private Map<String, Animation> animationMap;
  17.     private Map<String, Texture> textureMap;
  18.  
  19.  
  20.     public GameAssetManager() {
  21.         animationMap = new HashMap<>();
  22.         textureMap = new HashMap<>();
  23.     }
  24.  
  25.     /**
  26.      * checks if animation is retrieved and/or returns the required animation
  27.      * @param animationWrapper
  28.      * @return
  29.      */
  30.     public Animation checkGetAnimation(AnimationWrapper animationWrapper) {
  31.         Animation animation = animationMap.get(animationWrapper.getPath());
  32.  
  33.         if (animation == null) {
  34.             InputStream is = animationWrapper.getOrigin().getClass().getClassLoader().getResourceAsStream(animationWrapper.getPath());
  35.  
  36.             try {
  37.                 Gdx2DPixmap gmp = new Gdx2DPixmap(is, Gdx2DPixmap.GDX2D_FORMAT_RGBA8888);
  38.                 Pixmap pix = new Pixmap(gmp);
  39.                 Texture texture = new Texture(pix);
  40.                 TextureRegion textureRegion = new TextureRegion(texture);
  41.                 TextureRegion[][] tmp = textureRegion.split(texture.getWidth() / animationWrapper.getFrameCols(), texture.getHeight() / animationWrapper.getFrameRows());
  42.                 TextureRegion[] explosionFrames = new TextureRegion[animationWrapper.getFrameCols() * animationWrapper.getFrameRows()];
  43.                 int index = 0;
  44.                 for (int i = 0; i < animationWrapper.getFrameRows(); i++) {
  45.                     for (int j = 0; j < animationWrapper.getFrameCols(); j++) {
  46.                         explosionFrames[index++] = tmp[i][j];
  47.                     }
  48.                 }
  49.                 animation = new Animation(0.025f, explosionFrames);
  50.                 animation.setPlayMode(Animation.PlayMode.NORMAL);
  51.                 animationMap.put(animationWrapper.getPath(), animation);
  52.  
  53.             } catch (IOException e1) {
  54.                 e1.printStackTrace();
  55.             }
  56.  
  57.         }
  58.  
  59.         return animation;
  60.     }
  61.  
  62.     /**
  63.      * checks if texture is retrieved and/or returns the required texture
  64.      * @param objectClass
  65.      * @param path
  66.      * @return
  67.      */
  68.     public Texture checkGetTexture(Class objectClass, String path) {
  69.         Texture texture = textureMap.get(path);
  70.  
  71.         if (texture == null) {
  72.  
  73.             InputStream is = objectClass.getClassLoader().getResourceAsStream(
  74.                     path
  75.             );
  76.             try {
  77.                 Gdx2DPixmap gmp = new Gdx2DPixmap(is, Gdx2DPixmap.GDX2D_FORMAT_RGBA8888);
  78.                 Pixmap pix = new Pixmap(gmp);
  79.                 texture = new Texture(pix);
  80.                 textureMap.put(path, texture);
  81.                 pix.dispose();
  82.                 is.close();
  83.  
  84.             } catch (IOException e1) {
  85.                 e1.printStackTrace();
  86.             }
  87.         }
  88.  
  89.         return texture;
  90.     }
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement