a6318d71e75cb86c5788be83b1937a5f89c90cbf
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
;
15 import java
.util
.HashMap
;
16 import java
.util
.LinkedList
;
17 import java
.util
.List
;
22 public static final JsonReader jsonReader
= new JsonReader();
24 private static final List
<Texture
> loadedTextures
= new LinkedList
<>();
26 public static final Sprite
[][] playerSprite
= new Sprite
[2][4];
27 public static final Sprite
[][] pigSprite
= new Sprite
[2][2];
28 public static final HashMap
<String
, TextureRegion
> textureRegions
= new HashMap
<>();
29 public static final ArrayMap
<String
, TouchButton
> guiMap
= new ArrayMap
<>();
30 private static final GlyphLayout glyphLayout
= new GlyphLayout();
31 public static BitmapFont minecraftFont
;
33 public static Map
<String
, Texture
> blockTextures
= new HashMap
<>();
34 public static Map
<String
, Texture
> itemTextures
= new HashMap
<>();
36 public static void dispose() {
37 minecraftFont
.dispose();
38 loadedTextures
.forEach(Texture
::dispose
);
39 loadedTextures
.clear();
42 private static Texture
loadTexture(FileHandle fileHandle
) {
43 Texture texture
= new Texture(fileHandle
);
44 loadedTextures
.add(texture
);
48 private static TextureRegion
flippedRegion(Texture texture
, int x
, int y
, int width
, int height
) {
49 return new TextureRegion(texture
, x
, y
+ height
, width
, -height
);
52 private static Sprite
flippedSprite(Texture texture
) {
53 Sprite sprite
= new Sprite(texture
);
54 sprite
.flip(false, true);
58 private static Sprite
flippedSprite(TextureRegion texture
) {
59 Sprite sprite
= new Sprite(texture
);
60 sprite
.flip(false, true);
64 private static void loadMob(AssetLoader assetLoader
, Sprite
[][] sprite
, String mob
) {
65 for (int i
= 0; i
< sprite
.length
; i
++) {
66 for (int j
= 0; j
< sprite
[i
].length
; j
++) {
67 sprite
[i
][j
] = flippedSprite(loadTexture(
68 assetLoader
.getAssetHandle("mobs/" + mob
+ "/" + i
+ "_" + j
+ ".png")));
69 sprite
[i
][j
].setOrigin(sprite
[i
][j
].getWidth() / 2, 0);
74 private static void setPlayerHeadOrigin() {
75 for (Sprite
[] sprites
: playerSprite
) {
76 sprites
[0].setOrigin(sprites
[0].getWidth() / 2, sprites
[0].getHeight());
81 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
82 * and puts to {@link #textureRegions} HashMap
84 private static void loadJSON(AssetLoader assetLoader
) {
85 JsonValue json
= jsonReader
.parse(assetLoader
.getAssetHandle("json/texture_regions.json"));
86 for (JsonValue file
= json
.child(); file
!= null; file
= file
.next()) {
87 Texture texture
= loadTexture(assetLoader
.getAssetHandle(file
.name() + ".png"));
89 textureRegions
.put(file
.name(),
90 flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
92 for (JsonValue key
= file
.child(); key
!= null; key
= key
.next()) {
93 int x
= getIntFromJson(key
, "x", 0);
94 int y
= getIntFromJson(key
, "y", 0);
95 int w
= getIntFromJson(key
, "w", texture
.getWidth());
96 int h
= getIntFromJson(key
, "h", texture
.getHeight());
97 textureRegions
.put(key
.name(), flippedRegion(texture
, x
, y
, w
, h
));
103 private static void loadAllPngsFromDirInto(FileHandle dir
, Map
<String
, Texture
> loadInto
) {
104 for (FileHandle handle
: dir
.list((d
, name
) -> name
.endsWith(".png"))) {
105 loadInto
.put(handle
.nameWithoutExtension(), loadTexture(handle
));
109 private static void loadItems(AssetLoader assetLoader
) {
110 final FileHandle itemsDir
= assetLoader
.getAssetHandle("textures/items");
111 loadAllPngsFromDirInto(itemsDir
, itemTextures
);
114 private static void loadBlocks(AssetLoader assetLoader
) {
115 final FileHandle blocksDir
= assetLoader
.getAssetHandle("textures/blocks");
116 loadAllPngsFromDirInto(blocksDir
, blockTextures
);
119 public static void load(final AssetLoader assetLoader
) {
120 loadMob(assetLoader
, playerSprite
, "char");
121 loadMob(assetLoader
, pigSprite
, "pig");
122 loadJSON(assetLoader
);
123 loadBlocks(assetLoader
);
124 loadItems(assetLoader
);
125 setPlayerHeadOrigin();
126 minecraftFont
= new BitmapFont(assetLoader
.getAssetHandle("font.fnt"), true);
127 minecraftFont
.getData().setScale(.375f);
131 * @param s string whose width you want to know
132 * @return A width of string written in {@link #minecraftFont} in pixels
134 public static int getStringWidth(String s
) {
135 glyphLayout
.setText(minecraftFont
, s
);
136 return (int) glyphLayout
.width
;
140 * @param s string whose height you want to know
141 * @return A height of string written in {@link #minecraftFont} in pixels
143 public static int getStringHeight(String s
) {
144 glyphLayout
.setText(minecraftFont
, s
);
145 return (int) glyphLayout
.height
;
148 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
149 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
152 public static float getFloatFromJson(JsonValue json
, String name
, float defaultValue
) {
153 return json
.has(name
) ? json
.getFloat(name
) : defaultValue
;
156 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
157 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
160 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
161 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;