Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.assets;
- import java.util.PriorityQueue;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.files.FileHandle;
- import com.badlogic.gdx.graphics.g2d.TextureAtlas;
- import com.badlogic.gdx.utils.JsonReader;
- import com.badlogic.gdx.utils.JsonValue;
- public class AssetsLoader {
- private enum AssetType {
- ATLAS
- }
- private class AssetLoadingTask {
- String name;
- AssetType type;
- String[] params;
- AssetLoadingTask(String name, AssetType type, String... params) {
- this.name = name;
- this.type = type;
- this.params = params;
- }
- }
- private Assets assets = new Assets();
- private PriorityQueue<AssetLoadingTask> loadQueue = new PriorityQueue();
- private int toLoad, loaded;
- public Assets getAssets() {
- return assets;
- }
- public float getProgress() {
- return loaded / toLoad;
- }
- public boolean update() {
- if (loadQueue.size() != 0) {
- loadNextAsset();
- loaded++;
- if (loadQueue.size() == 0)
- return true;
- }
- return false;
- }
- public void loadAtlasesFrom(String pathToPack) {
- FileHandle packFile = Gdx.files.internal(pathToPack);
- JsonValue root = new JsonReader().parse(packFile);
- JsonValue nextValue = root.child;
- while (nextValue != null)
- {
- String name = nextValue.name;
- String data = nextValue.getString("data");
- String images = nextValue.getString("images");
- addTask(name, AssetType.ATLAS, data, images);
- nextValue = nextValue.next;
- }
- }
- private void addTask(String name, AssetType type, String... params) {
- AssetLoadingTask task = new AssetLoadingTask(name, type, params);
- loadQueue.offer(task);
- toLoad++;
- }
- private void loadNextAsset() {
- AssetLoadingTask task = loadQueue.poll();
- if (task.type == AssetType.ATLAS) {
- TextureAtlas atlas = new TextureAtlas(
- Gdx.files.internal(task.params[0]),
- Gdx.files.internal(task.params[1]));
- assets.addAtlas(task.name, atlas);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement