DEADSOFTWARE

Refactor
[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 JsonReader jsonReader = new JsonReader();
19 public static Sprite[][] playerSprite = new Sprite[2][4];
20 public static HashMap<String, TextureRegion> textureRegions = new HashMap<>();
21 public static ArrayMap<String, Rectangle> guiMap = new ArrayMap<>();
22 public static Sprite[][] pigSprite = new Sprite[2][2];
23 static BitmapFont minecraftFont;
24 private static GlyphLayout glyphLayout = new GlyphLayout();
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 void loadPlayer() {
31 Texture plTex = new Texture(Gdx.files.internal("mobs/char.png"));
32 //LOOK TO LEFT
33 //head
34 playerSprite[0][0] = new Sprite(new TextureRegion(plTex, 0, 0, 12, 12));
35 playerSprite[0][0].flip(false, true);
36 //body
37 playerSprite[0][1] = new Sprite(new TextureRegion(plTex, 0, 13, 12, 12));
38 playerSprite[0][1].flip(false, true);
39 //hand
40 playerSprite[0][2] = new Sprite(new TextureRegion(plTex, 25, 5, 20, 20));
41 playerSprite[0][2].flip(false, true);
42 //leg
43 playerSprite[0][3] = new Sprite(new TextureRegion(plTex, 25, 27, 20, 20));
44 playerSprite[0][3].flip(false, true);
45 //LOOK TO RIGHT
46 //head
47 playerSprite[1][0] = new Sprite(new TextureRegion(plTex, 13, 0, 12, 12));
48 playerSprite[1][0].flip(false, true);
49 //body
50 playerSprite[1][1] = new Sprite(new TextureRegion(plTex, 13, 13, 12, 12));
51 playerSprite[1][1].flip(false, true);
52 //hand
53 playerSprite[1][2] = new Sprite(new TextureRegion(plTex, 37, 5, 20, 20));
54 playerSprite[1][2].flip(false, true);
55 //leg
56 playerSprite[1][3] = new Sprite(new TextureRegion(plTex, 37, 27, 20, 20));
57 playerSprite[1][3].flip(false, true);
58 }
60 private static void loadPig() {
61 Texture pigTex = new Texture(Gdx.files.internal("mobs/pig.png"));
62 pigSprite[0][0] = new Sprite(new TextureRegion(pigTex, 0, 0, 25, 12));
63 pigSprite[0][0].flip(false, true);
64 pigSprite[1][0] = new Sprite(new TextureRegion(pigTex, 0, 12, 25, 12));
65 pigSprite[1][0].flip(false, true);
66 pigSprite[0][1] = new Sprite(new TextureRegion(pigTex, 4, 26, 12, 12));
67 pigSprite[0][1].flip(false, true);
68 pigSprite[1][1] = new Sprite(new TextureRegion(pigTex, 16, 26, 12, 12));
69 pigSprite[1][1].flip(false, true);
70 }
72 /**
73 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
74 * and puts to {@link #textureRegions} HashMap
75 */
76 private static void loadJSON() {
77 JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/texture_regions.json"));
78 for (JsonValue file = json.child(); file != null; file = file.next()) {
79 Texture texture = new Texture(Gdx.files.internal(file.name() + ".png"));
80 for (JsonValue key = file.child(); key != null; key = key.next()) {
81 int x = key.has("x") ? key.getInt("x") : 0;
82 int y = key.has("y") ? key.getInt("y") : 0;
83 int w = key.has("w") ? key.getInt("w") : texture.getWidth();
84 int h = key.has("h") ? key.getInt("h") : texture.getHeight();
85 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
86 }
87 }
88 }
90 public static void load() {
91 minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
92 minecraftFont.getData().setScale(.375f);
93 loadPlayer();
94 loadPig();
95 loadJSON();
96 }
98 /**
99 * @param s string whose width you want to know
100 * @return A width of string written in {@link #minecraftFont} in pixels
101 */
102 public static int getStringWidth(String s) {
103 glyphLayout.setText(minecraftFont, s);
104 return (int) glyphLayout.width;
107 /**
108 * @param s string whose height you want to know
109 * @return A height of string written in {@link #minecraftFont} in pixels
110 */
111 public static int getStringHeight(String s) {
112 glyphLayout.setText(minecraftFont, s);
113 return (int) glyphLayout.height;