Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package com.test.assets;
  2.  
  3. import java.util.PriorityQueue;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.files.FileHandle;
  7. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  8. import com.badlogic.gdx.utils.JsonReader;
  9. import com.badlogic.gdx.utils.JsonValue;
  10.  
  11. public class AssetsLoader {
  12.     private enum AssetType {
  13.         ATLAS
  14.     }
  15.    
  16.     private class AssetLoadingTask {
  17.         String name;
  18.         AssetType type;
  19.         String[] params;
  20.        
  21.         AssetLoadingTask(String name, AssetType type, String... params) {
  22.             this.name = name;
  23.             this.type = type;
  24.             this.params = params;
  25.         }
  26.     }
  27.    
  28.     private Assets assets = new Assets();
  29.     private PriorityQueue<AssetLoadingTask> loadQueue = new PriorityQueue();
  30.    
  31.     private int toLoad, loaded;
  32.    
  33.     public Assets getAssets() {
  34.         return assets;
  35.     }
  36.    
  37.     public float getProgress() {
  38.         return loaded / toLoad;
  39.     }
  40.    
  41.     public boolean update() {
  42.         if (loadQueue.size() != 0) {
  43.             loadNextAsset();
  44.             loaded++;
  45.            
  46.             if (loadQueue.size() == 0)
  47.                 return true;
  48.         }
  49.        
  50.         return false;
  51.     }
  52.    
  53.     public void loadAtlasesFrom(String pathToPack) {
  54.         FileHandle packFile = Gdx.files.internal(pathToPack);
  55.         JsonValue root = new JsonReader().parse(packFile);
  56.         JsonValue nextValue = root.child;
  57.        
  58.         while (nextValue != null)
  59.         {
  60.             String name = nextValue.name;
  61.             String data = nextValue.getString("data");
  62.             String images = nextValue.getString("images");
  63.            
  64.             addTask(name, AssetType.ATLAS, data, images);
  65.            
  66.             nextValue = nextValue.next;
  67.         }
  68.     }
  69.    
  70.     private void addTask(String name, AssetType type, String... params) {
  71.         AssetLoadingTask task = new AssetLoadingTask(name, type, params);
  72.         loadQueue.offer(task);
  73.        
  74.         toLoad++;
  75.     }
  76.    
  77.     private void loadNextAsset() {
  78.         AssetLoadingTask task = loadQueue.poll();
  79.  
  80.         if (task.type == AssetType.ATLAS) {
  81.             TextureAtlas atlas = new TextureAtlas(
  82.                 Gdx.files.internal(task.params[0]),
  83.                 Gdx.files.internal(task.params[1]));
  84.            
  85.             assets.addAtlas(task.name, atlas);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement