DEADSOFTWARE

Remove ternary reading from json
[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.math.Rectangle;
10 import com.badlogic.gdx.utils.ArrayMap;
11 import com.badlogic.gdx.utils.JsonReader;
12 import com.badlogic.gdx.utils.JsonValue;
14 import java.util.HashMap;
16 public class Assets {
18 public static final JsonReader jsonReader = new JsonReader();
20 private static final GlyphLayout glyphLayout = new GlyphLayout();
22 static BitmapFont minecraftFont;
24 public static final Sprite[][] playerSprite = new Sprite[2][4];
25 public static final Sprite[][] pigSprite = new Sprite[2][2];
26 public static Sprite fallingSandSprite;
27 public static Sprite fallingGravelSprite;
28 public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
29 public static final ArrayMap<String, Rectangle> guiMap = new ArrayMap<>();
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 loadPlayer() {
48 Texture plTex = new Texture(Gdx.files.internal("mobs/char.png"));
49 //LOOK TO LEFT
50 //head
51 playerSprite[0][0] = flippedSprite(new TextureRegion(plTex, 0, 0, 12, 12));
52 //body
53 playerSprite[0][1] = flippedSprite(new TextureRegion(plTex, 0, 13, 12, 12));
54 //hand
55 playerSprite[0][2] = flippedSprite(new TextureRegion(plTex, 25, 5, 20, 20));
56 //leg
57 playerSprite[0][3] = flippedSprite(new TextureRegion(plTex, 25, 27, 20, 20));
58 //LOOK TO RIGHT
59 //head
60 playerSprite[1][0] = flippedSprite(new TextureRegion(plTex, 13, 0, 12, 12));
61 //body
62 playerSprite[1][1] = flippedSprite(new TextureRegion(plTex, 13, 13, 12, 12));
63 //hand
64 playerSprite[1][2] = flippedSprite(new TextureRegion(plTex, 37, 5, 20, 20));
65 //leg
66 playerSprite[1][3] = flippedSprite(new TextureRegion(plTex, 37, 27, 20, 20));
67 }
69 private static void loadPig() {
70 Texture pigTex = new Texture(Gdx.files.internal("mobs/pig.png"));
71 pigSprite[0][0] = flippedSprite(new TextureRegion(pigTex, 0, 0, 25, 12));
72 pigSprite[1][0] = flippedSprite(new TextureRegion(pigTex, 0, 12, 25, 12));
73 pigSprite[0][1] = flippedSprite(new TextureRegion(pigTex, 4, 26, 12, 12));
74 pigSprite[1][1] = flippedSprite(new TextureRegion(pigTex, 16, 26, 12, 12));
75 }
77 private static void loadFallingBlocks() {
78 fallingSandSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/sand.png"))));
79 fallingGravelSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/gravel.png"))));
80 }
82 /**
83 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
84 * and puts to {@link #textureRegions} HashMap
85 */
86 private static void loadJSON() {
87 JsonValue json = jsonReader.parse(Gdx.files.internal("json/texture_regions.json"));
88 for (JsonValue file = json.child(); file != null; file = file.next()) {
89 Texture texture = new Texture(Gdx.files.internal(file.name() + ".png"));
90 if (file.size == 0) {
91 textureRegions.put(file.name(), flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
92 } else {
93 for (JsonValue key = file.child(); key != null; key = key.next()) {
94 int x = getIntFromJson(key, "x", 0);
95 int y = getIntFromJson(key, "y", 0);
96 int w = getIntFromJson(key, "w", texture.getWidth());
97 int h = getIntFromJson(key, "h", texture.getHeight());
98 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
99 }
104 public static void load() {
105 minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
106 minecraftFont.getData().setScale(.375f);
107 loadPlayer();
108 loadPig();
109 loadFallingBlocks();
110 loadJSON();
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 String getStringFromJson(JsonValue json, String name, String defaultValue) {
136 return json.has(name) ? json.getString(name) : defaultValue;
139 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
140 return json.has(name) ? json.getBoolean(name) : defaultValue;