b01153a4c6f697c9d3251b921ea494977e0ef3f2
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
;
19 public static final JsonReader jsonReader
= new JsonReader();
21 private static final GlyphLayout glyphLayout
= new GlyphLayout();
22 static final BitmapFont minecraftFont
= new BitmapFont(Gdx
.files
.internal("font.fnt"), true);
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 final Sprite sandSprite
= flippedSprite(new Texture((Gdx
.files
.internal("textures/blocks/sand.png"))));
29 public static final Sprite gravelSprite
= flippedSprite(new Texture((Gdx
.files
.internal("textures/blocks/gravel.png"))));
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
);
35 private static Sprite
flippedSprite(Texture texture
) {
36 Sprite sprite
= new Sprite(texture
);
37 sprite
.flip(false, true);
41 private static Sprite
flippedSprite(TextureRegion texture
) {
42 Sprite sprite
= new Sprite(texture
);
43 sprite
.flip(false, true);
47 private static void loadPlayer() {
48 Texture plTex
= new Texture(Gdx
.files
.internal("mobs/char.png"));
51 playerSprite
[0][0] = flippedSprite(new TextureRegion(plTex
, 0, 0, 12, 12));
53 playerSprite
[0][1] = flippedSprite(new TextureRegion(plTex
, 0, 13, 12, 12));
55 playerSprite
[0][2] = flippedSprite(new TextureRegion(plTex
, 25, 5, 20, 20));
57 playerSprite
[0][3] = flippedSprite(new TextureRegion(plTex
, 25, 27, 20, 20));
60 playerSprite
[1][0] = flippedSprite(new TextureRegion(plTex
, 13, 0, 12, 12));
62 playerSprite
[1][1] = flippedSprite(new TextureRegion(plTex
, 13, 13, 12, 12));
64 playerSprite
[1][2] = flippedSprite(new TextureRegion(plTex
, 37, 5, 20, 20));
66 playerSprite
[1][3] = flippedSprite(new TextureRegion(plTex
, 37, 27, 20, 20));
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));
78 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
79 * and puts to {@link #textureRegions} HashMap
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"));
86 textureRegions
.put(file
.name(), flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
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
));
99 public static void load() {
100 minecraftFont
.getData().setScale(.375f);
107 * @param s string whose width you want to know
108 * @return A width of string written in {@link #minecraftFont} in pixels
110 public static int getStringWidth(String s
) {
111 glyphLayout
.setText(minecraftFont
, s
);
112 return (int) glyphLayout
.width
;
116 * @param s string whose height you want to know
117 * @return A height of string written in {@link #minecraftFont} in pixels
119 public static int getStringHeight(String s
) {
120 glyphLayout
.setText(minecraftFont
, s
);
121 return (int) glyphLayout
.height
;
124 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
125 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
128 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
129 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
132 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
133 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;