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 Sprite joyBackground
;
45 public static Sprite joyStick
;
47 public static void dispose() {
48 minecraftFont
.dispose();
49 loadedTextures
.forEach(Texture
::dispose
);
50 loadedTextures
.clear();
53 private static Texture
loadTexture(FileHandle fileHandle
) {
54 Texture texture
= new Texture(fileHandle
);
55 loadedTextures
.add(texture
);
59 private static TextureRegion
flippedRegion(Texture texture
, int x
, int y
, int width
, int height
) {
60 return new TextureRegion(texture
, x
, y
+ height
, width
, -height
);
63 private static Sprite
flippedSprite(Texture texture
) {
64 Sprite sprite
= new Sprite(texture
);
65 sprite
.flip(false, true);
69 private static Sprite
flippedSprite(TextureRegion texture
) {
70 Sprite sprite
= new Sprite(texture
);
71 sprite
.flip(false, true);
75 private static void loadMob(AssetLoader assetLoader
, Sprite
[][] sprite
, String mob
) {
76 for (int i
= 0; i
< sprite
.length
; i
++) {
77 for (int j
= 0; j
< sprite
[i
].length
; j
++) {
78 sprite
[i
][j
] = flippedSprite(loadTexture(
79 assetLoader
.getAssetHandle("mobs/" + mob
+ "/" + i
+ "_" + j
+ ".png")));
80 sprite
[i
][j
].setOrigin(sprite
[i
][j
].getWidth() / 2, 0);
85 private static void loadBlockDamage(AssetLoader assetLoader
) {
86 final Texture blockDamageTexture
= loadTexture(assetLoader
.getAssetHandle("break.png"));
87 for (int i
= 0; i
< BLOCK_DAMAGE_STAGES
; i
++) {
88 blockDamageSprites
[i
] = new Sprite(flippedRegion(blockDamageTexture
, i
* 16, 0, 16, 16));
92 private static void setPlayerHeadOrigin() {
93 for (Sprite
[] sprites
: playerSprite
) {
94 sprites
[0].setOrigin(sprites
[0].getWidth() / 2, sprites
[0].getHeight());
99 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
100 * and puts to {@link #textureRegions} HashMap
102 private static void loadJSON(AssetLoader assetLoader
) {
103 JsonValue json
= jsonReader
.parse(assetLoader
.getAssetHandle("json/texture_regions.json"));
104 for (JsonValue file
= json
.child(); file
!= null; file
= file
.next()) {
105 Texture texture
= loadTexture(assetLoader
.getAssetHandle(file
.name() + ".png"));
106 if (file
.size
== 0) {
107 textureRegions
.put(file
.name(),
108 flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
110 for (JsonValue key
= file
.child(); key
!= null; key
= key
.next()) {
111 int x
= getIntFromJson(key
, "x", 0);
112 int y
= getIntFromJson(key
, "y", 0);
113 int w
= getIntFromJson(key
, "w", texture
.getWidth());
114 int h
= getIntFromJson(key
, "h", texture
.getHeight());
115 textureRegions
.put(key
.name(), flippedRegion(texture
, x
, y
, w
, h
));
121 private static int getMouseKey(String name
) {
124 return Input
.Buttons
.LEFT
;
126 return Input
.Buttons
.RIGHT
;
128 return Input
.Buttons
.MIDDLE
;
130 return Input
.Buttons
.BACK
;
132 return Input
.Buttons
.FORWARD
;
138 private static void loadTouchButtonsFromJSON(AssetLoader assetLoader
) {
139 JsonValue json
= Assets
.jsonReader
.parse(assetLoader
.getAssetHandle("json/touch_buttons.json"));
140 for (JsonValue key
= json
.child(); key
!= null; key
= key
.next()) {
141 float x
= key
.getFloat("x");
142 float y
= key
.getFloat("y");
143 float w
= key
.getFloat("w");
144 float h
= key
.getFloat("h");
145 boolean mouse
= Assets
.getBooleanFromJson(key
, "mouse", false);
146 String name
= key
.getString("key");
147 int code
= mouse ?
getMouseKey(name
) : Input
.Keys
.valueOf(name
);
149 x
= assetLoader
.getGameRendererWidth() + x
;
152 y
= assetLoader
.getGameRendererHeight() + y
;
154 Assets
.guiMap
.put(key
.name(), new TouchButton(new Rectangle(x
, y
, w
, h
), code
, mouse
));
159 private static Texture
resolveTexture(AssetLoader assetLoader
, String textureName
, String lookUpPath
, Map
<String
, Texture
> cache
) {
160 if (cache
.containsKey(textureName
)) {
161 return cache
.get(textureName
);
164 final Texture texture
= loadTexture(assetLoader
.getAssetHandle(lookUpPath
+ File
.separator
+ textureName
+ ".png"));
165 cache
.put(textureName
, texture
);
170 public static Texture
resolveItemTexture(AssetLoader assetLoader
, String textureName
) {
171 return resolveTexture(assetLoader
, textureName
, "textures/items", itemTextures
);
174 public static Texture
resolveBlockTexture(AssetLoader assetLoader
, String textureName
) {
175 return resolveTexture(assetLoader
, textureName
, "textures/blocks", blockTextures
);
178 private static void loadAllPngsFromDirInto(FileHandle dir
, Map
<String
, Texture
> loadInto
) {
179 for (FileHandle handle
: dir
.list((d
, name
) -> name
.endsWith(".png"))) {
180 loadInto
.put(handle
.nameWithoutExtension(), loadTexture(handle
));
184 private static void loadItems(AssetLoader assetLoader
) {
185 final FileHandle itemsDir
= assetLoader
.getAssetHandle("textures/items");
186 loadAllPngsFromDirInto(itemsDir
, itemTextures
);
189 private static void loadBlocks(AssetLoader assetLoader
) {
190 final FileHandle blocksDir
= assetLoader
.getAssetHandle("textures/blocks");
191 loadAllPngsFromDirInto(blocksDir
, blockTextures
);
194 private static void loadJoystick(AssetLoader assetLoader
) {
195 joyStick
= new Sprite(loadTexture(assetLoader
.getAssetHandle("joy_stick.png")));
196 joyBackground
= new Sprite(loadTexture(assetLoader
.getAssetHandle("joy_background.png")));
199 public static void load(final AssetLoader assetLoader
) {
200 loadMob(assetLoader
, playerSprite
, "char");
201 loadMob(assetLoader
, pigSprite
, "pig");
202 loadBlockDamage(assetLoader
);
203 loadJSON(assetLoader
);
204 loadBlocks(assetLoader
);
205 loadItems(assetLoader
);
206 loadTouchButtonsFromJSON(assetLoader
);
207 loadJoystick(assetLoader
);
208 setPlayerHeadOrigin();
209 minecraftFont
= new BitmapFont(assetLoader
.getAssetHandle("font.fnt"), true);
210 minecraftFont
.getData().setScale(.375f);
211 minecraftFont
.setUseIntegerPositions(false);
215 * @param s string whose width you want to know
216 * @return A width of string written in {@link #minecraftFont} in pixels
218 public static int getStringWidth(String s
) {
219 glyphLayout
.setText(minecraftFont
, s
);
220 return (int) glyphLayout
.width
;
224 * @param s string whose height you want to know
225 * @return A height of string written in {@link #minecraftFont} in pixels
227 public static int getStringHeight(String s
) {
228 glyphLayout
.setText(minecraftFont
, s
);
229 return (int) glyphLayout
.height
;
232 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
233 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
236 public static float getFloatFromJson(JsonValue json
, String name
, float defaultValue
) {
237 return json
.has(name
) ? json
.getFloat(name
) : defaultValue
;
240 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
241 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
244 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
245 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;