1 package ru
.deadsoftware
.cavedroid
.misc
;
3 import com
.badlogic
.gdx
.Input
;
4 import com
.badlogic
.gdx
.files
.FileHandle
;
5 import com
.badlogic
.gdx
.graphics
.Texture
;
6 import com
.badlogic
.gdx
.graphics
.g2d
.BitmapFont
;
7 import com
.badlogic
.gdx
.graphics
.g2d
.GlyphLayout
;
8 import com
.badlogic
.gdx
.graphics
.g2d
.Sprite
;
9 import com
.badlogic
.gdx
.graphics
.g2d
.TextureRegion
;
10 import com
.badlogic
.gdx
.math
.Rectangle
;
11 import com
.badlogic
.gdx
.utils
.ArrayMap
;
12 import com
.badlogic
.gdx
.utils
.JsonReader
;
13 import com
.badlogic
.gdx
.utils
.JsonValue
;
14 import ru
.deadsoftware
.cavedroid
.game
.objects
.TouchButton
;
15 import ru
.deadsoftware
.cavedroid
.misc
.utils
.AssetLoader
;
18 import java
.util
.HashMap
;
19 import java
.util
.LinkedList
;
20 import java
.util
.List
;
25 private static final int BLOCK_DAMAGE_STAGES
= 10;
27 public static final JsonReader jsonReader
= new JsonReader();
29 private static final List
<Texture
> loadedTextures
= new LinkedList
<>();
31 public static final Sprite
[][] playerSprite
= new Sprite
[2][4];
32 public static final Sprite
[][] pigSprite
= new Sprite
[2][2];
34 public static final Sprite
[] blockDamageSprites
= new Sprite
[10];
36 public static final HashMap
<String
, TextureRegion
> textureRegions
= new HashMap
<>();
37 public static final ArrayMap
<String
, TouchButton
> guiMap
= new ArrayMap
<>();
38 private static final GlyphLayout glyphLayout
= new GlyphLayout();
39 public static BitmapFont minecraftFont
;
41 public static Map
<String
, Texture
> blockTextures
= new HashMap
<>();
42 public static Map
<String
, Texture
> itemTextures
= new HashMap
<>();
44 public static void dispose() {
45 minecraftFont
.dispose();
46 loadedTextures
.forEach(Texture
::dispose
);
47 loadedTextures
.clear();
50 private static Texture
loadTexture(FileHandle fileHandle
) {
51 Texture texture
= new Texture(fileHandle
);
52 loadedTextures
.add(texture
);
56 private static TextureRegion
flippedRegion(Texture texture
, int x
, int y
, int width
, int height
) {
57 return new TextureRegion(texture
, x
, y
+ height
, width
, -height
);
60 private static Sprite
flippedSprite(Texture texture
) {
61 Sprite sprite
= new Sprite(texture
);
62 sprite
.flip(false, true);
66 private static Sprite
flippedSprite(TextureRegion texture
) {
67 Sprite sprite
= new Sprite(texture
);
68 sprite
.flip(false, true);
72 private static void loadMob(AssetLoader assetLoader
, Sprite
[][] sprite
, String mob
) {
73 for (int i
= 0; i
< sprite
.length
; i
++) {
74 for (int j
= 0; j
< sprite
[i
].length
; j
++) {
75 sprite
[i
][j
] = flippedSprite(loadTexture(
76 assetLoader
.getAssetHandle("mobs/" + mob
+ "/" + i
+ "_" + j
+ ".png")));
77 sprite
[i
][j
].setOrigin(sprite
[i
][j
].getWidth() / 2, 0);
82 private static void loadBlockDamage(AssetLoader assetLoader
) {
83 final Texture blockDamageTexture
= loadTexture(assetLoader
.getAssetHandle("break.png"));
84 for (int i
= 0; i
< BLOCK_DAMAGE_STAGES
; i
++) {
85 blockDamageSprites
[i
] = new Sprite(flippedRegion(blockDamageTexture
, i
* 16, 0, 16, 16));
89 private static void setPlayerHeadOrigin() {
90 for (Sprite
[] sprites
: playerSprite
) {
91 sprites
[0].setOrigin(sprites
[0].getWidth() / 2, sprites
[0].getHeight());
96 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
97 * and puts to {@link #textureRegions} HashMap
99 private static void loadJSON(AssetLoader assetLoader
) {
100 JsonValue json
= jsonReader
.parse(assetLoader
.getAssetHandle("json/texture_regions.json"));
101 for (JsonValue file
= json
.child(); file
!= null; file
= file
.next()) {
102 Texture texture
= loadTexture(assetLoader
.getAssetHandle(file
.name() + ".png"));
103 if (file
.size
== 0) {
104 textureRegions
.put(file
.name(),
105 flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
107 for (JsonValue key
= file
.child(); key
!= null; key
= key
.next()) {
108 int x
= getIntFromJson(key
, "x", 0);
109 int y
= getIntFromJson(key
, "y", 0);
110 int w
= getIntFromJson(key
, "w", texture
.getWidth());
111 int h
= getIntFromJson(key
, "h", texture
.getHeight());
112 textureRegions
.put(key
.name(), flippedRegion(texture
, x
, y
, w
, h
));
118 private static int getMouseKey(String name
) {
121 return Input
.Buttons
.LEFT
;
123 return Input
.Buttons
.RIGHT
;
125 return Input
.Buttons
.MIDDLE
;
127 return Input
.Buttons
.BACK
;
129 return Input
.Buttons
.FORWARD
;
135 private static void loadTouchButtonsFromJSON(AssetLoader assetLoader
) {
136 JsonValue json
= Assets
.jsonReader
.parse(assetLoader
.getAssetHandle("json/touch_buttons.json"));
137 for (JsonValue key
= json
.child(); key
!= null; key
= key
.next()) {
138 float x
= key
.getFloat("x");
139 float y
= key
.getFloat("y");
140 float w
= key
.getFloat("w");
141 float h
= key
.getFloat("h");
142 boolean mouse
= Assets
.getBooleanFromJson(key
, "mouse", false);
143 String name
= key
.getString("key");
144 int code
= mouse ?
getMouseKey(name
) : Input
.Keys
.valueOf(name
);
146 x
= assetLoader
.getGameRendererWidth() + x
;
149 y
= assetLoader
.getGameRendererHeight() + y
;
151 Assets
.guiMap
.put(key
.name(), new TouchButton(new Rectangle(x
, y
, w
, h
), code
, mouse
));
156 private static Texture
resolveTexture(AssetLoader assetLoader
, String textureName
, String lookUpPath
, Map
<String
, Texture
> cache
) {
157 if (cache
.containsKey(textureName
)) {
158 return cache
.get(textureName
);
161 final Texture texture
= loadTexture(assetLoader
.getAssetHandle(lookUpPath
+ File
.separator
+ textureName
+ ".png"));
162 cache
.put(textureName
, texture
);
167 public static Texture
resolveItemTexture(AssetLoader assetLoader
, String textureName
) {
168 return resolveTexture(assetLoader
, textureName
, "textures/items", itemTextures
);
171 public static Texture
resolveBlockTexture(AssetLoader assetLoader
, String textureName
) {
172 return resolveTexture(assetLoader
, textureName
, "textures/blocks", blockTextures
);
175 private static void loadAllPngsFromDirInto(FileHandle dir
, Map
<String
, Texture
> loadInto
) {
176 for (FileHandle handle
: dir
.list((d
, name
) -> name
.endsWith(".png"))) {
177 loadInto
.put(handle
.nameWithoutExtension(), loadTexture(handle
));
181 private static void loadItems(AssetLoader assetLoader
) {
182 final FileHandle itemsDir
= assetLoader
.getAssetHandle("textures/items");
183 loadAllPngsFromDirInto(itemsDir
, itemTextures
);
186 private static void loadBlocks(AssetLoader assetLoader
) {
187 final FileHandle blocksDir
= assetLoader
.getAssetHandle("textures/blocks");
188 loadAllPngsFromDirInto(blocksDir
, blockTextures
);
191 public static void load(final AssetLoader assetLoader
) {
192 loadMob(assetLoader
, playerSprite
, "char");
193 loadMob(assetLoader
, pigSprite
, "pig");
194 loadBlockDamage(assetLoader
);
195 loadJSON(assetLoader
);
196 loadBlocks(assetLoader
);
197 loadItems(assetLoader
);
198 loadTouchButtonsFromJSON(assetLoader
);
199 setPlayerHeadOrigin();
200 minecraftFont
= new BitmapFont(assetLoader
.getAssetHandle("font.fnt"), true);
201 minecraftFont
.getData().setScale(.375f);
205 * @param s string whose width you want to know
206 * @return A width of string written in {@link #minecraftFont} in pixels
208 public static int getStringWidth(String s
) {
209 glyphLayout
.setText(minecraftFont
, s
);
210 return (int) glyphLayout
.width
;
214 * @param s string whose height you want to know
215 * @return A height of string written in {@link #minecraftFont} in pixels
217 public static int getStringHeight(String s
) {
218 glyphLayout
.setText(minecraftFont
, s
);
219 return (int) glyphLayout
.height
;
222 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
223 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
226 public static float getFloatFromJson(JsonValue json
, String name
, float defaultValue
) {
227 return json
.has(name
) ? json
.getFloat(name
) : defaultValue
;
230 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
231 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
234 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
235 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;