DEADSOFTWARE

Fix launch on desktop
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / Assets.java
index cf87a0859cd666f3adda10c11ab57ec10c247ea1..6a4305f4a5273a2a79a36f13940e07bcb1cd7467 100644 (file)
@@ -12,14 +12,25 @@ import com.badlogic.gdx.utils.JsonValue;
 import ru.deadsoftware.cavedroid.game.objects.TouchButton;
 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
 
+import java.io.File;
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
 public class Assets {
 
+    private static final int BLOCK_DAMAGE_STAGES = 10;
+
     public static final JsonReader jsonReader = new JsonReader();
+
+    private static final List<Texture> loadedTextures = new LinkedList<>();
+
     public static final Sprite[][] playerSprite = new Sprite[2][4];
     public static final Sprite[][] pigSprite = new Sprite[2][2];
+
+    public static final Sprite[] blockDamageSprites = new Sprite[10];
+
     public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
     public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
     private static final GlyphLayout glyphLayout = new GlyphLayout();
@@ -28,6 +39,18 @@ public class Assets {
     public static Map<String, Texture> blockTextures = new HashMap<>();
     public static Map<String, Texture> itemTextures = new HashMap<>();
 
+    public static void dispose() {
+        minecraftFont.dispose();
+        loadedTextures.forEach(Texture::dispose);
+        loadedTextures.clear();
+    }
+    
+    private static Texture loadTexture(FileHandle fileHandle) {
+        Texture texture = new Texture(fileHandle);
+        loadedTextures.add(texture);
+        return texture;
+    }
+
     private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
         return new TextureRegion(texture, x, y + height, width, -height);
     }
@@ -47,13 +70,20 @@ public class Assets {
     private static void loadMob(AssetLoader assetLoader, Sprite[][] sprite, String mob) {
         for (int i = 0; i < sprite.length; i++) {
             for (int j = 0; j < sprite[i].length; j++) {
-                sprite[i][j] = flippedSprite(new Texture(
+                sprite[i][j] = flippedSprite(loadTexture(
                         assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png")));
                 sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0);
             }
         }
     }
 
+    private static void loadBlockDamage(AssetLoader assetLoader) {
+        final Texture blockDamageTexture = loadTexture(assetLoader.getAssetHandle("break.png"));
+        for (int i = 0; i < BLOCK_DAMAGE_STAGES; i++) {
+            blockDamageSprites[i] = new Sprite(flippedRegion(blockDamageTexture, i * 16, 0, 16, 16));
+        }
+    }
+
     private static void setPlayerHeadOrigin() {
         for (Sprite[] sprites : playerSprite) {
             sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
@@ -67,7 +97,7 @@ public class Assets {
     private static void loadJSON(AssetLoader assetLoader) {
         JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json"));
         for (JsonValue file = json.child(); file != null; file = file.next()) {
-            Texture texture = new Texture(assetLoader.getAssetHandle(file.name() + ".png"));
+            Texture texture = loadTexture(assetLoader.getAssetHandle(file.name() + ".png"));
             if (file.size == 0) {
                 textureRegions.put(file.name(),
                         flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
@@ -83,9 +113,28 @@ public class Assets {
         }
     }
 
+    private static Texture resolveTexture(AssetLoader assetLoader, String textureName, String lookUpPath, Map<String, Texture> cache) {
+        if (cache.containsKey(textureName)) {
+            return cache.get(textureName);
+        }
+
+        final Texture texture = loadTexture(assetLoader.getAssetHandle(lookUpPath + File.separator + textureName + ".png"));
+        cache.put(textureName, texture);
+
+        return texture;
+    }
+
+    public static Texture resolveItemTexture(AssetLoader assetLoader, String textureName) {
+        return resolveTexture(assetLoader, textureName, "textures/items", itemTextures);
+    }
+
+    public static Texture resolveBlockTexture(AssetLoader assetLoader, String textureName) {
+        return resolveTexture(assetLoader, textureName, "textures/blocks", blockTextures);
+    }
+
     private static void loadAllPngsFromDirInto(FileHandle dir, Map<String, Texture> loadInto) {
         for (FileHandle handle : dir.list((d, name) -> name.endsWith(".png"))) {
-            loadInto.put(handle.nameWithoutExtension(), new Texture(handle));
+            loadInto.put(handle.nameWithoutExtension(), loadTexture(handle));
         }
     }
 
@@ -102,6 +151,7 @@ public class Assets {
     public static void load(final AssetLoader assetLoader) {
         loadMob(assetLoader, playerSprite, "char");
         loadMob(assetLoader, pigSprite, "pig");
+        loadBlockDamage(assetLoader);
         loadJSON(assetLoader);
         loadBlocks(assetLoader);
         loadItems(assetLoader);