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
;
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
);
32 private static Sprite
flippedSprite(Texture texture
) {
33 Sprite sprite
= new Sprite(texture
);
34 sprite
.flip(false, true);
38 private static Sprite
flippedSprite(TextureRegion texture
) {
39 Sprite sprite
= new Sprite(texture
);
40 sprite
.flip(false, true);
44 private static void loadPlayer() {
45 Texture plTex
= new Texture(Gdx
.files
.internal("mobs/char.png"));
48 playerSprite
[0][0] = flippedSprite(new TextureRegion(plTex
, 0, 0, 12, 12));
50 playerSprite
[0][1] = flippedSprite(new TextureRegion(plTex
, 0, 13, 12, 12));
52 playerSprite
[0][2] = flippedSprite(new TextureRegion(plTex
, 25, 5, 20, 20));
54 playerSprite
[0][3] = flippedSprite(new TextureRegion(plTex
, 25, 27, 20, 20));
57 playerSprite
[1][0] = flippedSprite(new TextureRegion(plTex
, 13, 0, 12, 12));
59 playerSprite
[1][1] = flippedSprite(new TextureRegion(plTex
, 13, 13, 12, 12));
61 playerSprite
[1][2] = flippedSprite(new TextureRegion(plTex
, 37, 5, 20, 20));
63 playerSprite
[1][3] = flippedSprite(new TextureRegion(plTex
, 37, 27, 20, 20));
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));
75 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
76 * and puts to {@link #textureRegions} HashMap
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"));
83 textureRegions
.put(file
.name(),
84 flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
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
));
97 public static void load() {
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"))));
108 * @param s string whose width you want to know
109 * @return A width of string written in {@link #minecraftFont} in pixels
111 public static int getStringWidth(String s
) {
112 glyphLayout
.setText(minecraftFont
, s
);
113 return (int) glyphLayout
.width
;
117 * @param s string whose height you want to know
118 * @return A height of string written in {@link #minecraftFont} in pixels
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
;