DEADSOFTWARE

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