DEADSOFTWARE

Refactor
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / Assets.java
index 0b0b109face3f64befb701007e032eec19d94a90..30c346b32e7d9fe835e557370cb9e9fa5b51b9c5 100644 (file)
@@ -6,77 +6,59 @@ 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 ru.deadsoftware.cavedroid.CaveGame;
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.utils.ArrayMap;
+import com.badlogic.gdx.utils.JsonReader;
+import com.badlogic.gdx.utils.JsonValue;
 
-public class Assets {
-
-    private static GlyphLayout layout;
-
-    public static BitmapFont mcFont;
-
-    public static Sprite gameLogo;
+import java.util.HashMap;
 
-    public static Texture menuBtnTex;
-    public static TextureRegion[] menuBtn = new TextureRegion[3];
-
-    public static Texture plTex;
-    public static Sprite[][] plSprite = new Sprite[2][4];
+public class Assets {
 
-    public static Texture pigTex;
+    public static JsonReader jsonReader = new JsonReader();
+    public static Sprite[][] playerSprite = new Sprite[2][4];
+    public static HashMap<String, TextureRegion> textureRegions = new HashMap<>();
+    public static ArrayMap<String, Rectangle> guiMap = new ArrayMap<>();
     public static Sprite[][] pigSprite = new Sprite[2][2];
+    static BitmapFont minecraftFont;
+    private static GlyphLayout glyphLayout = new GlyphLayout();
 
-    public static Texture shade;
-
-    public static Texture gui;
-    public static TextureRegion invBar;
-    public static TextureRegion invBarCur;
-    public static TextureRegion guiCur;
-
-    public static Texture wreckTex;
-    public static TextureRegion[] wreck = new TextureRegion[10];
-
-    public static Texture creativeTex;
-    public static TextureRegion creativeInv;
-    public static TextureRegion creativeScr;
-
-    public static Texture touchGui;
-    public static TextureRegion[] touchArrows = new TextureRegion[4];
-    public static TextureRegion touchLMB, touchRMB;
-    public static TextureRegion touchMode;
-    public static TextureRegion touchSpace;
+    private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
+        return new TextureRegion(texture, x, y + height, width, -height);
+    }
 
     private static void loadPlayer() {
-        plTex = new Texture(Gdx.files.internal("mobs/char.png"));
+        Texture plTex = new Texture(Gdx.files.internal("mobs/char.png"));
         //LOOK TO LEFT
         //head
-        plSprite[0][0] = new Sprite(new TextureRegion(plTex, 0, 0, 12, 12));
-        plSprite[0][0].flip(false, true);
+        playerSprite[0][0] = new Sprite(new TextureRegion(plTex, 0, 0, 12, 12));
+        playerSprite[0][0].flip(false, true);
         //body
-        plSprite[0][1] = new Sprite(new TextureRegion(plTex, 0, 13, 12, 12));
-        plSprite[0][1].flip(false, true);
+        playerSprite[0][1] = new Sprite(new TextureRegion(plTex, 0, 13, 12, 12));
+        playerSprite[0][1].flip(false, true);
         //hand
-        plSprite[0][2] = new Sprite(new TextureRegion(plTex, 25, 5, 20, 20));
-        plSprite[0][2].flip(false, true);
+        playerSprite[0][2] = new Sprite(new TextureRegion(plTex, 25, 5, 20, 20));
+        playerSprite[0][2].flip(false, true);
         //leg
-        plSprite[0][3] = new Sprite(new TextureRegion(plTex, 25, 27, 20, 20));
-        plSprite[0][3].flip(false, true);
+        playerSprite[0][3] = new Sprite(new TextureRegion(plTex, 25, 27, 20, 20));
+        playerSprite[0][3].flip(false, true);
         //LOOK TO RIGHT
         //head
-        plSprite[1][0] = new Sprite(new TextureRegion(plTex, 13, 0, 12, 12));
-        plSprite[1][0].flip(false, true);
+        playerSprite[1][0] = new Sprite(new TextureRegion(plTex, 13, 0, 12, 12));
+        playerSprite[1][0].flip(false, true);
         //body
-        plSprite[1][1] = new Sprite(new TextureRegion(plTex, 13, 13, 12, 12));
-        plSprite[1][1].flip(false, true);
+        playerSprite[1][1] = new Sprite(new TextureRegion(plTex, 13, 13, 12, 12));
+        playerSprite[1][1].flip(false, true);
         //hand
-        plSprite[1][2] = new Sprite(new TextureRegion(plTex, 37, 5, 20, 20));
-        plSprite[1][2].flip(false, true);
+        playerSprite[1][2] = new Sprite(new TextureRegion(plTex, 37, 5, 20, 20));
+        playerSprite[1][2].flip(false, true);
         //leg
-        plSprite[1][3] = new Sprite(new TextureRegion(plTex, 37, 27, 20, 20));
-        plSprite[1][3].flip(false, true);
+        playerSprite[1][3] = new Sprite(new TextureRegion(plTex, 37, 27, 20, 20));
+        playerSprite[1][3].flip(false, true);
     }
 
     private static void loadPig() {
-        pigTex = new Texture(Gdx.files.internal("mobs/pig.png"));
+        Texture pigTex = new Texture(Gdx.files.internal("mobs/pig.png"));
         pigSprite[0][0] = new Sprite(new TextureRegion(pigTex, 0, 0, 25, 12));
         pigSprite[0][0].flip(false, true);
         pigSprite[1][0] = new Sprite(new TextureRegion(pigTex, 0, 12, 25, 12));
@@ -87,68 +69,48 @@ public class Assets {
         pigSprite[1][1].flip(false, true);
     }
 
-    public static void load() {
-        mcFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
-        mcFont.getData().setScale(.375f);
-
-        layout = new GlyphLayout();
-
-        gameLogo = new Sprite(new Texture(Gdx.files.internal("gamelogo.png")));
-        gameLogo.flip(false, true);
-
-        menuBtnTex = new Texture(Gdx.files.internal("buttons.png"));
-        for (int i = 0; i < 3; i++) {
-            menuBtn[i] = new TextureRegion(menuBtnTex, 0, 20 * i, 200, 20);
-            menuBtn[i].flip(false, true);
+    /**
+     * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
+     * and puts to {@link #textureRegions} HashMap
+     */
+    private static void loadJSON() {
+        JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/texture_regions.json"));
+        for (JsonValue file = json.child(); file != null; file = file.next()) {
+            Texture texture = new Texture(Gdx.files.internal(file.name() + ".png"));
+            for (JsonValue key = file.child(); key != null; key = key.next()) {
+                int x = key.has("x") ? key.getInt("x") : 0;
+                int y = key.has("y") ? key.getInt("y") : 0;
+                int w = key.has("w") ? key.getInt("w") : texture.getWidth();
+                int h = key.has("h") ? key.getInt("h") : texture.getHeight();
+                textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
+            }
         }
+    }
 
+    public static void load() {
+        minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
+        minecraftFont.getData().setScale(.375f);
         loadPlayer();
         loadPig();
-
-        shade = new Texture(Gdx.files.internal("shade.png"));
-
-        gui = new Texture(Gdx.files.internal("gui.png"));
-        guiCur = new TextureRegion(gui, 0, 0, 16, 16);
-        invBar = new TextureRegion(gui, 0, 16, 182, 22);
-        invBarCur = new TextureRegion(gui, 0, 38, 24, 24);
-
-        creativeTex = new Texture(Gdx.files.internal("allitems.png"));
-        creativeInv = new TextureRegion(creativeTex, 0, 0, 176, 136);
-        creativeInv.flip(false, true);
-        creativeScr = new TextureRegion(creativeTex, 3, 137, 12, 15);
-        creativeScr.flip(false, true);
-
-        wreckTex = new Texture(Gdx.files.internal("break.png"));
-        for (int i = 0; i < 10; i++) {
-            wreck[i] = new TextureRegion(wreckTex, 16 * i, 0, 16, 16);
-        }
-
-        if (CaveGame.TOUCH) {
-            touchGui = new Texture(Gdx.files.internal("touch_gui.png"));
-            for (int i = 0; i < 4; i++) {
-                touchArrows[i] = new TextureRegion(touchGui, i * 26, 0, 26, 26);
-                touchArrows[i].flip(false, true);
-            }
-            touchLMB = new TextureRegion(touchGui, 0, 26, 26, 26);
-            touchLMB.flip(false, true);
-            touchRMB = new TextureRegion(touchGui, 52, 26, 26, 26);
-            touchRMB.flip(false, true);
-            touchMode = new TextureRegion(touchGui, 26, 26, 26, 26);
-            touchMode.flip(false, true);
-            touchSpace = new TextureRegion(touchGui, 0, 52, 104, 26);
-            touchSpace.flip(false, true);
-        }
-
+        loadJSON();
     }
 
+    /**
+     * @param s string whose width you want to know
+     * @return A width of string written in {@link #minecraftFont} in pixels
+     */
     public static int getStringWidth(String s) {
-        layout.setText(mcFont, s);
-        return (int) layout.width;
+        glyphLayout.setText(minecraftFont, s);
+        return (int) glyphLayout.width;
     }
 
+    /**
+     * @param s string whose height you want to know
+     * @return A height of string written in {@link #minecraftFont} in pixels
+     */
     public static int getStringHeight(String s) {
-        layout.setText(mcFont, s);
-        return (int) layout.height;
+        glyphLayout.setText(minecraftFont, s);
+        return (int) glyphLayout.height;
     }
 
 }