DEADSOFTWARE

Make all Assets final
[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();
21 static final BitmapFont minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);;
23 public static final Sprite[][] playerSprite = new Sprite[2][4];
24 public static final Sprite[][] pigSprite = new Sprite[2][2];
25 public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
26 public static final ArrayMap<String, Rectangle> guiMap = new ArrayMap<>();
27 public static final Sprite sandSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/sand.png"))));
28 public static final Sprite gravelSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/gravel.png"))));
30 private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
31 return new TextureRegion(texture, x, y + height, width, -height);
32 }
34 private static Sprite flippedSprite(Texture texture) {
35 Sprite sprite = new Sprite(texture);
36 sprite.flip(false, true);
37 return sprite;
38 }
40 private static Sprite flippedSprite(TextureRegion texture) {
41 Sprite sprite = new Sprite(texture);
42 sprite.flip(false, true);
43 return sprite;
44 }
46 private static void loadPlayer() {
47 Texture plTex = new Texture(Gdx.files.internal("mobs/char.png"));
48 //LOOK TO LEFT
49 //head
50 playerSprite[0][0] = flippedSprite(new TextureRegion(plTex, 0, 0, 12, 12));
51 //body
52 playerSprite[0][1] = flippedSprite(new TextureRegion(plTex, 0, 13, 12, 12));
53 //hand
54 playerSprite[0][2] = flippedSprite(new TextureRegion(plTex, 25, 5, 20, 20));
55 //leg
56 playerSprite[0][3] = flippedSprite(new TextureRegion(plTex, 25, 27, 20, 20));
57 //LOOK TO RIGHT
58 //head
59 playerSprite[1][0] = flippedSprite(new TextureRegion(plTex, 13, 0, 12, 12));
60 //body
61 playerSprite[1][1] = flippedSprite(new TextureRegion(plTex, 13, 13, 12, 12));
62 //hand
63 playerSprite[1][2] = flippedSprite(new TextureRegion(plTex, 37, 5, 20, 20));
64 //leg
65 playerSprite[1][3] = flippedSprite(new TextureRegion(plTex, 37, 27, 20, 20));
66 }
68 private static void loadPig() {
69 Texture pigTex = new Texture(Gdx.files.internal("mobs/pig.png"));
70 pigSprite[0][0] = flippedSprite(new TextureRegion(pigTex, 0, 0, 25, 12));
71 pigSprite[1][0] = flippedSprite(new TextureRegion(pigTex, 0, 12, 25, 12));
72 pigSprite[0][1] = flippedSprite(new TextureRegion(pigTex, 4, 26, 12, 12));
73 pigSprite[1][1] = flippedSprite(new TextureRegion(pigTex, 16, 26, 12, 12));
74 }
76 /**
77 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
78 * and puts to {@link #textureRegions} HashMap
79 */
80 private static void loadJSON() {
81 JsonValue json = jsonReader.parse(Gdx.files.internal("json/texture_regions.json"));
82 for (JsonValue file = json.child(); file != null; file = file.next()) {
83 Texture texture = new Texture(Gdx.files.internal(file.name() + ".png"));
84 if (file.size == 0) {
85 textureRegions.put(file.name(), flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
86 } else {
87 for (JsonValue key = file.child(); key != null; key = key.next()) {
88 int x = getIntFromJson(key, "x", 0);
89 int y = getIntFromJson(key, "y", 0);
90 int w = getIntFromJson(key, "w", texture.getWidth());
91 int h = getIntFromJson(key, "h", texture.getHeight());
92 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
93 }
94 }
95 }
96 }
98 public static void load() {
99 minecraftFont.getData().setScale(.375f);
100 loadPlayer();
101 loadPig();
102 loadJSON();
105 /**
106 * @param s string whose width you want to know
107 * @return A width of string written in {@link #minecraftFont} in pixels
108 */
109 public static int getStringWidth(String s) {
110 glyphLayout.setText(minecraftFont, s);
111 return (int) glyphLayout.width;
114 /**
115 * @param s string whose height you want to know
116 * @return A height of string written in {@link #minecraftFont} in pixels
117 */
118 public static int getStringHeight(String s) {
119 glyphLayout.setText(minecraftFont, s);
120 return (int) glyphLayout.height;
123 public static int getIntFromJson(JsonValue json, String name, int defaultValue) {
124 return json.has(name) ? json.getInt(name) : defaultValue;
127 public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
128 return json.has(name) ? json.getString(name) : defaultValue;
131 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
132 return json.has(name) ? json.getBoolean(name) : defaultValue;