DEADSOFTWARE

Add my repo for automultibind
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / Assets.java
index abec0a0b2f5f0a4ecac1a6128b1a3f622445f1a1..893414ba61c3aa3c5ee1700cbcec121aeef457fc 100644 (file)
@@ -1,17 +1,20 @@
 package ru.deadsoftware.cavedroid.misc;
 
+import com.badlogic.gdx.Input;
 import com.badlogic.gdx.files.FileHandle;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.g2d.BitmapFont;
 import com.badlogic.gdx.graphics.g2d.GlyphLayout;
 import com.badlogic.gdx.graphics.g2d.Sprite;
 import com.badlogic.gdx.graphics.g2d.TextureRegion;
+import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.utils.ArrayMap;
 import com.badlogic.gdx.utils.JsonReader;
 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;
@@ -38,9 +41,17 @@ public class Assets {
     public static Map<String, Texture> blockTextures = new HashMap<>();
     public static Map<String, Texture> itemTextures = new HashMap<>();
 
+    public static Sprite joyBackground;
+    public static Sprite joyStick;
+
+    public static Sprite furnaceBurn;
+    public static Sprite furnaceProgress;
+
     public static void dispose() {
         minecraftFont.dispose();
-        loadedTextures.forEach(Texture::dispose);
+        for (Texture texture : loadedTextures) {
+            texture.dispose();
+        }
         loadedTextures.clear();
     }
     
@@ -70,14 +81,14 @@ public class Assets {
         for (int i = 0; i < sprite.length; i++) {
             for (int j = 0; j < sprite[i].length; j++) {
                 sprite[i][j] = flippedSprite(loadTexture(
-                        assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png")));
+                        assetLoader.getAssetHandle("pp/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"));
+        final Texture blockDamageTexture = loadTexture(assetLoader.getAssetHandle("pp/break.png"));
         for (int i = 0; i < BLOCK_DAMAGE_STAGES; i++) {
             blockDamageSprites[i] = new Sprite(flippedRegion(blockDamageTexture, i * 16, 0, 16, 16));
         }
@@ -97,9 +108,10 @@ public class Assets {
         JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json"));
         for (JsonValue file = json.child(); file != null; file = file.next()) {
             Texture texture = loadTexture(assetLoader.getAssetHandle(file.name() + ".png"));
+            final String[] pathSegments = file.name().split("/");
+            final String name = pathSegments[pathSegments.length - 1];
             if (file.size == 0) {
-                textureRegions.put(file.name(),
-                        flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
+                textureRegions.put(name, flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
             } else {
                 for (JsonValue key = file.child(); key != null; key = key.next()) {
                     int x = getIntFromJson(key, "x", 0);
@@ -112,6 +124,63 @@ public class Assets {
         }
     }
 
+    private static int getMouseKey(String name) {
+        switch (name) {
+            case "Left":
+                return Input.Buttons.LEFT;
+            case "Right":
+                return Input.Buttons.RIGHT;
+            case "Middle":
+                return Input.Buttons.MIDDLE;
+            case "Back":
+                return Input.Buttons.BACK;
+            case "Forward":
+                return Input.Buttons.FORWARD;
+            default:
+                return -1;
+        }
+    }
+
+    private static void loadTouchButtonsFromJSON(AssetLoader assetLoader) {
+        JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/touch_buttons.json"));
+        for (JsonValue key = json.child(); key != null; key = key.next()) {
+            float x = key.getFloat("x");
+            float y = key.getFloat("y");
+            float w = key.getFloat("w");
+            float h = key.getFloat("h");
+            boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
+            String name = key.getString("key");
+            int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
+            if (x < 0) {
+                x = assetLoader.getGameRendererWidth() + x;
+            }
+            if (y < 0) {
+                y = assetLoader.getGameRendererHeight() + y;
+            }
+            Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
+        }
+
+    }
+
+    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, "pp/textures/items", itemTextures);
+    }
+
+    public static Texture resolveBlockTexture(AssetLoader assetLoader, String textureName) {
+        return resolveTexture(assetLoader, textureName, "pp/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(), loadTexture(handle));
@@ -119,15 +188,25 @@ public class Assets {
     }
 
     private static void loadItems(AssetLoader assetLoader) {
-        final FileHandle itemsDir = assetLoader.getAssetHandle("textures/items");
+        final FileHandle itemsDir = assetLoader.getAssetHandle("pp/textures/items");
         loadAllPngsFromDirInto(itemsDir, itemTextures);
     }
 
     private static void loadBlocks(AssetLoader assetLoader) {
-        final FileHandle blocksDir = assetLoader.getAssetHandle("textures/blocks");
+        final FileHandle blocksDir = assetLoader.getAssetHandle("pp/textures/blocks");
         loadAllPngsFromDirInto(blocksDir, blockTextures);
     }
 
+    private static void loadJoystick(AssetLoader assetLoader) {
+        joyStick = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_stick.png")));
+        joyBackground = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_background.png")));
+    }
+
+    private static void loadFurnace(AssetLoader assetLoader) {
+        furnaceBurn = new Sprite(textureRegions.get("furnace_burn"));
+        furnaceProgress = new Sprite(textureRegions.get("furnace_progress"));
+    }
+
     public static void load(final AssetLoader assetLoader) {
         loadMob(assetLoader, playerSprite, "char");
         loadMob(assetLoader, pigSprite, "pig");
@@ -135,9 +214,13 @@ public class Assets {
         loadJSON(assetLoader);
         loadBlocks(assetLoader);
         loadItems(assetLoader);
+        loadTouchButtonsFromJSON(assetLoader);
+        loadJoystick(assetLoader);
+        loadFurnace(assetLoader);
         setPlayerHeadOrigin();
         minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true);
         minecraftFont.getData().setScale(.375f);
+        minecraftFont.setUseIntegerPositions(false);
     }
 
     /**