DEADSOFTWARE

90a7ec1bec4c818a12286161603d751152586f7e
[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;
14 import java.util.HashMap;
16 public class Assets {
18 public static final JsonReader jsonReader = new JsonReader();
19 public static final Sprite[][] playerSprite = new Sprite[2][4];
20 public static final Sprite[][] pigSprite = new Sprite[2][2];
21 public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
22 public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
23 private static final GlyphLayout glyphLayout = new GlyphLayout();
24 static BitmapFont minecraftFont;
26 private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
27 return new TextureRegion(texture, x, y + height, width, -height);
28 }
30 private static Sprite flippedSprite(Texture texture) {
31 Sprite sprite = new Sprite(texture);
32 sprite.flip(false, true);
33 return sprite;
34 }
36 private static Sprite flippedSprite(TextureRegion texture) {
37 Sprite sprite = new Sprite(texture);
38 sprite.flip(false, true);
39 return sprite;
40 }
42 private static void loadMob(Sprite[][] sprite, String mob) {
43 for (int i = 0; i < sprite.length; i++) {
44 for (int j = 0; j < sprite[i].length; j++) {
45 sprite[i][j] = flippedSprite(new Texture(
46 Gdx.files.internal("mobs/" + mob + "/" + i + "_" + j + ".png")));
47 sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0);
48 }
49 }
50 }
52 private static void setPlayerHeadOrigin() {
53 for (Sprite[] sprites : playerSprite) {
54 sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
55 }
56 }
58 /**
59 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
60 * and puts to {@link #textureRegions} HashMap
61 */
62 private static void loadJSON() {
63 JsonValue json = jsonReader.parse(Gdx.files.internal("json/texture_regions.json"));
64 for (JsonValue file = json.child(); file != null; file = file.next()) {
65 Texture texture = new Texture(Gdx.files.internal(file.name() + ".png"));
66 if (file.size == 0) {
67 textureRegions.put(file.name(),
68 flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
69 } else {
70 for (JsonValue key = file.child(); key != null; key = key.next()) {
71 int x = getIntFromJson(key, "x", 0);
72 int y = getIntFromJson(key, "y", 0);
73 int w = getIntFromJson(key, "w", texture.getWidth());
74 int h = getIntFromJson(key, "h", texture.getHeight());
75 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
76 }
77 }
78 }
79 }
81 public static void load() {
82 loadMob(playerSprite, "char");
83 loadMob(pigSprite, "pig");
84 loadJSON();
85 setPlayerHeadOrigin();
86 minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
87 minecraftFont.getData().setScale(.375f);
88 }
90 /**
91 * @param s string whose width you want to know
92 * @return A width of string written in {@link #minecraftFont} in pixels
93 */
94 public static int getStringWidth(String s) {
95 glyphLayout.setText(minecraftFont, s);
96 return (int) glyphLayout.width;
97 }
99 /**
100 * @param s string whose height you want to know
101 * @return A height of string written in {@link #minecraftFont} in pixels
102 */
103 public static int getStringHeight(String s) {
104 glyphLayout.setText(minecraftFont, s);
105 return (int) glyphLayout.height;
108 public static int getIntFromJson(JsonValue json, String name, int defaultValue) {
109 return json.has(name) ? json.getInt(name) : defaultValue;
112 public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
113 return json.has(name) ? json.getString(name) : defaultValue;
116 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
117 return json.has(name) ? json.getBoolean(name) : defaultValue;