DEADSOFTWARE

Add support for external assets
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / Assets.java
1 package ru.deadsoftware.cavedroid.misc;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.graphics.Texture;
5 import com.badlogic.gdx.graphics.g2d.BitmapFont;
6 import com.badlogic.gdx.graphics.g2d.GlyphLayout;
7 import com.badlogic.gdx.graphics.g2d.Sprite;
8 import com.badlogic.gdx.graphics.g2d.TextureRegion;
9 import com.badlogic.gdx.utils.ArrayMap;
10 import com.badlogic.gdx.utils.JsonReader;
11 import com.badlogic.gdx.utils.JsonValue;
12 import ru.deadsoftware.cavedroid.game.objects.TouchButton;
13 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
15 import java.util.HashMap;
17 public class Assets {
19 public static final JsonReader jsonReader = new JsonReader();
20 public static final Sprite[][] playerSprite = new Sprite[2][4];
21 public static final Sprite[][] pigSprite = new Sprite[2][2];
22 public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
23 public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
24 private static final GlyphLayout glyphLayout = new GlyphLayout();
25 static BitmapFont minecraftFont;
27 private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
28 return new TextureRegion(texture, x, y + height, width, -height);
29 }
31 private static Sprite flippedSprite(Texture texture) {
32 Sprite sprite = new Sprite(texture);
33 sprite.flip(false, true);
34 return sprite;
35 }
37 private static Sprite flippedSprite(TextureRegion texture) {
38 Sprite sprite = new Sprite(texture);
39 sprite.flip(false, true);
40 return sprite;
41 }
43 private static void loadMob(AssetLoader assetLoader, Sprite[][] sprite, String mob) {
44 for (int i = 0; i < sprite.length; i++) {
45 for (int j = 0; j < sprite[i].length; j++) {
46 sprite[i][j] = flippedSprite(new Texture(
47 assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png")));
48 sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0);
49 }
50 }
51 }
53 private static void setPlayerHeadOrigin() {
54 for (Sprite[] sprites : playerSprite) {
55 sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
56 }
57 }
59 /**
60 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
61 * and puts to {@link #textureRegions} HashMap
62 */
63 private static void loadJSON(AssetLoader assetLoader) {
64 JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json"));
65 for (JsonValue file = json.child(); file != null; file = file.next()) {
66 Texture texture = new Texture(assetLoader.getAssetHandle(file.name() + ".png"));
67 if (file.size == 0) {
68 textureRegions.put(file.name(),
69 flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
70 } else {
71 for (JsonValue key = file.child(); key != null; key = key.next()) {
72 int x = getIntFromJson(key, "x", 0);
73 int y = getIntFromJson(key, "y", 0);
74 int w = getIntFromJson(key, "w", texture.getWidth());
75 int h = getIntFromJson(key, "h", texture.getHeight());
76 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
77 }
78 }
79 }
80 }
82 public static void load(final AssetLoader assetLoader) {
83 loadMob(assetLoader, playerSprite, "char");
84 loadMob(assetLoader, pigSprite, "pig");
85 loadJSON(assetLoader);
86 setPlayerHeadOrigin();
87 minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true);
88 minecraftFont.getData().setScale(.375f);
89 }
91 /**
92 * @param s string whose width you want to know
93 * @return A width of string written in {@link #minecraftFont} in pixels
94 */
95 public static int getStringWidth(String s) {
96 glyphLayout.setText(minecraftFont, s);
97 return (int) glyphLayout.width;
98 }
100 /**
101 * @param s string whose height you want to know
102 * @return A height of string written in {@link #minecraftFont} in pixels
103 */
104 public static int getStringHeight(String s) {
105 glyphLayout.setText(minecraftFont, s);
106 return (int) glyphLayout.height;
109 public static int getIntFromJson(JsonValue json, String name, int defaultValue) {
110 return json.has(name) ? json.getInt(name) : defaultValue;
113 public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
114 return json.has(name) ? json.getString(name) : defaultValue;
117 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
118 return json.has(name) ? json.getBoolean(name) : defaultValue;