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 static BitmapFont minecraftFont
;
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
);
30 private static Sprite
flippedSprite(Texture texture
) {
31 Sprite sprite
= new Sprite(texture
);
32 sprite
.flip(false, true);
36 private static Sprite
flippedSprite(TextureRegion texture
) {
37 Sprite sprite
= new Sprite(texture
);
38 sprite
.flip(false, true);
42 private static void loadMob(Sprite
[][] sprite
, String mob
) {
43 for (int i
= 0; i
< sprite
.length
; i
++) {
44 for (int j
= 0; j
< sprite
[i
].length
; j
++) {
45 sprite
[i
][j
] = flippedSprite(new Texture(
46 Gdx
.files
.internal("mobs/" + mob
+ "/" + i
+ "_" + j
+ ".png")));
47 sprite
[i
][j
].setOrigin(sprite
[i
][j
].getWidth() / 2, 0);
53 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
54 * and puts to {@link #textureRegions} HashMap
56 private static void loadJSON() {
57 JsonValue json
= jsonReader
.parse(Gdx
.files
.internal("json/texture_regions.json"));
58 for (JsonValue file
= json
.child(); file
!= null; file
= file
.next()) {
59 Texture texture
= new Texture(Gdx
.files
.internal(file
.name() + ".png"));
61 textureRegions
.put(file
.name(),
62 flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
64 for (JsonValue key
= file
.child(); key
!= null; key
= key
.next()) {
65 int x
= getIntFromJson(key
, "x", 0);
66 int y
= getIntFromJson(key
, "y", 0);
67 int w
= getIntFromJson(key
, "w", texture
.getWidth());
68 int h
= getIntFromJson(key
, "h", texture
.getHeight());
69 textureRegions
.put(key
.name(), flippedRegion(texture
, x
, y
, w
, h
));
75 public static void load() {
76 loadMob(playerSprite
, "char");
77 loadMob(pigSprite
, "pig");
79 minecraftFont
= new BitmapFont(Gdx
.files
.internal("font.fnt"), true);
80 minecraftFont
.getData().setScale(.375f);
84 * @param s string whose width you want to know
85 * @return A width of string written in {@link #minecraftFont} in pixels
87 public static int getStringWidth(String s
) {
88 glyphLayout
.setText(minecraftFont
, s
);
89 return (int) glyphLayout
.width
;
93 * @param s string whose height you want to know
94 * @return A height of string written in {@link #minecraftFont} in pixels
96 public static int getStringHeight(String s
) {
97 glyphLayout
.setText(minecraftFont
, s
);
98 return (int) glyphLayout
.height
;
101 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
102 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
105 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
106 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
109 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
110 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;