DEADSOFTWARE

Fix weird behavior on android
[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;
13 import ru.deadsoftware.cavedroid.game.objects.TouchButton;
15 import java.util.HashMap;
17 public class Assets {
19 public static final JsonReader jsonReader = new JsonReader();
21 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 final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
27 public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
28 public static Sprite sandSprite;
29 public static Sprite gravelSprite;
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 /**
78 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
79 * and puts to {@link #textureRegions} HashMap
80 */
81 private static void loadJSON() {
82 JsonValue json = jsonReader.parse(Gdx.files.internal("json/texture_regions.json"));
83 for (JsonValue file = json.child(); file != null; file = file.next()) {
84 Texture texture = new Texture(Gdx.files.internal(file.name() + ".png"));
85 if (file.size == 0) {
86 textureRegions.put(file.name(), flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
87 } else {
88 for (JsonValue key = file.child(); key != null; key = key.next()) {
89 int x = getIntFromJson(key, "x", 0);
90 int y = getIntFromJson(key, "y", 0);
91 int w = getIntFromJson(key, "w", texture.getWidth());
92 int h = getIntFromJson(key, "h", texture.getHeight());
93 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
94 }
95 }
96 }
97 }
99 public static void load() {
100 loadPlayer();
101 loadPig();
102 loadJSON();
103 minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
104 minecraftFont.getData().setScale(.375f);
105 sandSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/sand.png"))));
106 gravelSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/gravel.png"))));
109 /**
110 * @param s string whose width you want to know
111 * @return A width of string written in {@link #minecraftFont} in pixels
112 */
113 public static int getStringWidth(String s) {
114 glyphLayout.setText(minecraftFont, s);
115 return (int) glyphLayout.width;
118 /**
119 * @param s string whose height you want to know
120 * @return A height of string written in {@link #minecraftFont} in pixels
121 */
122 public static int getStringHeight(String s) {
123 glyphLayout.setText(minecraftFont, s);
124 return (int) glyphLayout.height;
127 public static int getIntFromJson(JsonValue json, String name, int defaultValue) {
128 return json.has(name) ? json.getInt(name) : defaultValue;
131 public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
132 return json.has(name) ? json.getString(name) : defaultValue;
135 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
136 return json.has(name) ? json.getBoolean(name) : defaultValue;