DEADSOFTWARE

cf87a0859cd666f3adda10c11ab57ec10c247ea1
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / Assets.java
1 package ru.deadsoftware.cavedroid.misc;
3 import com.badlogic.gdx.files.FileHandle;
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;
16 import java.util.Map;
18 public class Assets {
20 public static final JsonReader jsonReader = new JsonReader();
21 public static final Sprite[][] playerSprite = new Sprite[2][4];
22 public static final Sprite[][] pigSprite = new Sprite[2][2];
23 public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
24 public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
25 private static final GlyphLayout glyphLayout = new GlyphLayout();
26 public static BitmapFont minecraftFont;
28 public static Map<String, Texture> blockTextures = new HashMap<>();
29 public static Map<String, Texture> itemTextures = new HashMap<>();
31 private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
32 return new TextureRegion(texture, x, y + height, width, -height);
33 }
35 private static Sprite flippedSprite(Texture texture) {
36 Sprite sprite = new Sprite(texture);
37 sprite.flip(false, true);
38 return sprite;
39 }
41 private static Sprite flippedSprite(TextureRegion texture) {
42 Sprite sprite = new Sprite(texture);
43 sprite.flip(false, true);
44 return sprite;
45 }
47 private static void loadMob(AssetLoader assetLoader, Sprite[][] sprite, String mob) {
48 for (int i = 0; i < sprite.length; i++) {
49 for (int j = 0; j < sprite[i].length; j++) {
50 sprite[i][j] = flippedSprite(new Texture(
51 assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png")));
52 sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0);
53 }
54 }
55 }
57 private static void setPlayerHeadOrigin() {
58 for (Sprite[] sprites : playerSprite) {
59 sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
60 }
61 }
63 /**
64 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
65 * and puts to {@link #textureRegions} HashMap
66 */
67 private static void loadJSON(AssetLoader assetLoader) {
68 JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json"));
69 for (JsonValue file = json.child(); file != null; file = file.next()) {
70 Texture texture = new Texture(assetLoader.getAssetHandle(file.name() + ".png"));
71 if (file.size == 0) {
72 textureRegions.put(file.name(),
73 flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
74 } else {
75 for (JsonValue key = file.child(); key != null; key = key.next()) {
76 int x = getIntFromJson(key, "x", 0);
77 int y = getIntFromJson(key, "y", 0);
78 int w = getIntFromJson(key, "w", texture.getWidth());
79 int h = getIntFromJson(key, "h", texture.getHeight());
80 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
81 }
82 }
83 }
84 }
86 private static void loadAllPngsFromDirInto(FileHandle dir, Map<String, Texture> loadInto) {
87 for (FileHandle handle : dir.list((d, name) -> name.endsWith(".png"))) {
88 loadInto.put(handle.nameWithoutExtension(), new Texture(handle));
89 }
90 }
92 private static void loadItems(AssetLoader assetLoader) {
93 final FileHandle itemsDir = assetLoader.getAssetHandle("textures/items");
94 loadAllPngsFromDirInto(itemsDir, itemTextures);
95 }
97 private static void loadBlocks(AssetLoader assetLoader) {
98 final FileHandle blocksDir = assetLoader.getAssetHandle("textures/blocks");
99 loadAllPngsFromDirInto(blocksDir, blockTextures);
102 public static void load(final AssetLoader assetLoader) {
103 loadMob(assetLoader, playerSprite, "char");
104 loadMob(assetLoader, pigSprite, "pig");
105 loadJSON(assetLoader);
106 loadBlocks(assetLoader);
107 loadItems(assetLoader);
108 setPlayerHeadOrigin();
109 minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true);
110 minecraftFont.getData().setScale(.375f);
113 /**
114 * @param s string whose width you want to know
115 * @return A width of string written in {@link #minecraftFont} in pixels
116 */
117 public static int getStringWidth(String s) {
118 glyphLayout.setText(minecraftFont, s);
119 return (int) glyphLayout.width;
122 /**
123 * @param s string whose height you want to know
124 * @return A height of string written in {@link #minecraftFont} in pixels
125 */
126 public static int getStringHeight(String s) {
127 glyphLayout.setText(minecraftFont, s);
128 return (int) glyphLayout.height;
131 public static int getIntFromJson(JsonValue json, String name, int defaultValue) {
132 return json.has(name) ? json.getInt(name) : defaultValue;
135 public static float getFloatFromJson(JsonValue json, String name, float defaultValue) {
136 return json.has(name) ? json.getFloat(name) : defaultValue;
139 public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
140 return json.has(name) ? json.getString(name) : defaultValue;
143 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
144 return json.has(name) ? json.getBoolean(name) : defaultValue;