1 package ru
.deadsoftware
.cavedroid
.misc
;
3 import com
.badlogic
.gdx
.files
.FileHandle
;
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
;
13 import ru
.deadsoftware
.cavedroid
.misc
.utils
.AssetLoader
;
16 import java
.io
.FilenameFilter
;
17 import java
.util
.HashMap
;
22 public static final JsonReader jsonReader
= new JsonReader();
23 public static final Sprite
[][] playerSprite
= new Sprite
[2][4];
24 public static final Sprite
[][] pigSprite
= new Sprite
[2][2];
25 public static final HashMap
<String
, TextureRegion
> textureRegions
= new HashMap
<>();
26 public static final ArrayMap
<String
, TouchButton
> guiMap
= new ArrayMap
<>();
27 private static final GlyphLayout glyphLayout
= new GlyphLayout();
28 static BitmapFont minecraftFont
;
30 public static Map
<String
, Texture
> blockTextures
= new HashMap
<>();
31 public static Map
<String
, Texture
> itemTextures
= new HashMap
<>();
33 private static TextureRegion
flippedRegion(Texture texture
, int x
, int y
, int width
, int height
) {
34 return new TextureRegion(texture
, x
, y
+ height
, width
, -height
);
37 private static Sprite
flippedSprite(Texture texture
) {
38 Sprite sprite
= new Sprite(texture
);
39 sprite
.flip(false, true);
43 private static Sprite
flippedSprite(TextureRegion texture
) {
44 Sprite sprite
= new Sprite(texture
);
45 sprite
.flip(false, true);
49 private static void loadMob(AssetLoader assetLoader
, Sprite
[][] sprite
, String mob
) {
50 for (int i
= 0; i
< sprite
.length
; i
++) {
51 for (int j
= 0; j
< sprite
[i
].length
; j
++) {
52 sprite
[i
][j
] = flippedSprite(new Texture(
53 assetLoader
.getAssetHandle("mobs/" + mob
+ "/" + i
+ "_" + j
+ ".png")));
54 sprite
[i
][j
].setOrigin(sprite
[i
][j
].getWidth() / 2, 0);
59 private static void setPlayerHeadOrigin() {
60 for (Sprite
[] sprites
: playerSprite
) {
61 sprites
[0].setOrigin(sprites
[0].getWidth() / 2, sprites
[0].getHeight());
66 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
67 * and puts to {@link #textureRegions} HashMap
69 private static void loadJSON(AssetLoader assetLoader
) {
70 JsonValue json
= jsonReader
.parse(assetLoader
.getAssetHandle("json/texture_regions.json"));
71 for (JsonValue file
= json
.child(); file
!= null; file
= file
.next()) {
72 Texture texture
= new Texture(assetLoader
.getAssetHandle(file
.name() + ".png"));
74 textureRegions
.put(file
.name(),
75 flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
77 for (JsonValue key
= file
.child(); key
!= null; key
= key
.next()) {
78 int x
= getIntFromJson(key
, "x", 0);
79 int y
= getIntFromJson(key
, "y", 0);
80 int w
= getIntFromJson(key
, "w", texture
.getWidth());
81 int h
= getIntFromJson(key
, "h", texture
.getHeight());
82 textureRegions
.put(key
.name(), flippedRegion(texture
, x
, y
, w
, h
));
88 private static void loadAllPngsFromDirInto(FileHandle dir
, Map
<String
, Texture
> loadInto
) {
89 for (FileHandle handle
: dir
.list((d
, name
) -> name
.endsWith(".png"))) {
90 loadInto
.put(handle
.nameWithoutExtension(), new Texture(handle
));
94 private static void loadItems(AssetLoader assetLoader
) {
95 final FileHandle itemsDir
= assetLoader
.getAssetHandle("textures/items");
96 loadAllPngsFromDirInto(itemsDir
, itemTextures
);
99 private static void loadBlocks(AssetLoader assetLoader
) {
100 final FileHandle blocksDir
= assetLoader
.getAssetHandle("textures/blocks");
101 loadAllPngsFromDirInto(blocksDir
, blockTextures
);
104 public static void load(final AssetLoader assetLoader
) {
105 loadMob(assetLoader
, playerSprite
, "char");
106 loadMob(assetLoader
, pigSprite
, "pig");
107 loadJSON(assetLoader
);
108 loadBlocks(assetLoader
);
109 loadItems(assetLoader
);
110 setPlayerHeadOrigin();
111 minecraftFont
= new BitmapFont(assetLoader
.getAssetHandle("font.fnt"), true);
112 minecraftFont
.getData().setScale(.375f);
116 * @param s string whose width you want to know
117 * @return A width of string written in {@link #minecraftFont} in pixels
119 public static int getStringWidth(String s
) {
120 glyphLayout
.setText(minecraftFont
, s
);
121 return (int) glyphLayout
.width
;
125 * @param s string whose height you want to know
126 * @return A height of string written in {@link #minecraftFont} in pixels
128 public static int getStringHeight(String s
) {
129 glyphLayout
.setText(minecraftFont
, s
);
130 return (int) glyphLayout
.height
;
133 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
134 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
137 public static float getFloatFromJson(JsonValue json
, String name
, float defaultValue
) {
138 return json
.has(name
) ? json
.getFloat(name
) : defaultValue
;
141 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
142 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
145 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
146 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;