71cad7a2d69eac9226114ef607735d099bd579ca
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 Sprite furnaceBurn
;
48 public static Sprite furnaceProgress
;
50 public static void dispose() {
51 minecraftFont
.dispose();
52 loadedTextures
.forEach(Texture
::dispose
);
53 loadedTextures
.clear();
56 private static Texture
loadTexture(FileHandle fileHandle
) {
57 Texture texture
= new Texture(fileHandle
);
58 loadedTextures
.add(texture
);
62 private static TextureRegion
flippedRegion(Texture texture
, int x
, int y
, int width
, int height
) {
63 return new TextureRegion(texture
, x
, y
+ height
, width
, -height
);
66 private static Sprite
flippedSprite(Texture texture
) {
67 Sprite sprite
= new Sprite(texture
);
68 sprite
.flip(false, true);
72 private static Sprite
flippedSprite(TextureRegion texture
) {
73 Sprite sprite
= new Sprite(texture
);
74 sprite
.flip(false, true);
78 private static void loadMob(AssetLoader assetLoader
, Sprite
[][] sprite
, String mob
) {
79 for (int i
= 0; i
< sprite
.length
; i
++) {
80 for (int j
= 0; j
< sprite
[i
].length
; j
++) {
81 sprite
[i
][j
] = flippedSprite(loadTexture(
82 assetLoader
.getAssetHandle("pp/mobs/" + mob
+ "/" + i
+ "_" + j
+ ".png")));
83 sprite
[i
][j
].setOrigin(sprite
[i
][j
].getWidth() / 2, 0);
88 private static void loadBlockDamage(AssetLoader assetLoader
) {
89 final Texture blockDamageTexture
= loadTexture(assetLoader
.getAssetHandle("pp/break.png"));
90 for (int i
= 0; i
< BLOCK_DAMAGE_STAGES
; i
++) {
91 blockDamageSprites
[i
] = new Sprite(flippedRegion(blockDamageTexture
, i
* 16, 0, 16, 16));
95 private static void setPlayerHeadOrigin() {
96 for (Sprite
[] sprites
: playerSprite
) {
97 sprites
[0].setOrigin(sprites
[0].getWidth() / 2, sprites
[0].getHeight());
102 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
103 * and puts to {@link #textureRegions} HashMap
105 private static void loadJSON(AssetLoader assetLoader
) {
106 JsonValue json
= jsonReader
.parse(assetLoader
.getAssetHandle("json/texture_regions.json"));
107 for (JsonValue file
= json
.child(); file
!= null; file
= file
.next()) {
108 Texture texture
= loadTexture(assetLoader
.getAssetHandle(file
.name() + ".png"));
109 final String
[] pathSegments
= file
.name().split("/");
110 final String name
= pathSegments
[pathSegments
.length
- 1];
111 if (file
.size
== 0) {
112 textureRegions
.put(name
, flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
114 for (JsonValue key
= file
.child(); key
!= null; key
= key
.next()) {
115 int x
= getIntFromJson(key
, "x", 0);
116 int y
= getIntFromJson(key
, "y", 0);
117 int w
= getIntFromJson(key
, "w", texture
.getWidth());
118 int h
= getIntFromJson(key
, "h", texture
.getHeight());
119 textureRegions
.put(key
.name(), flippedRegion(texture
, x
, y
, w
, h
));
125 private static int getMouseKey(String name
) {
128 return Input
.Buttons
.LEFT
;
130 return Input
.Buttons
.RIGHT
;
132 return Input
.Buttons
.MIDDLE
;
134 return Input
.Buttons
.BACK
;
136 return Input
.Buttons
.FORWARD
;
142 private static void loadTouchButtonsFromJSON(AssetLoader assetLoader
) {
143 JsonValue json
= Assets
.jsonReader
.parse(assetLoader
.getAssetHandle("json/touch_buttons.json"));
144 for (JsonValue key
= json
.child(); key
!= null; key
= key
.next()) {
145 float x
= key
.getFloat("x");
146 float y
= key
.getFloat("y");
147 float w
= key
.getFloat("w");
148 float h
= key
.getFloat("h");
149 boolean mouse
= Assets
.getBooleanFromJson(key
, "mouse", false);
150 String name
= key
.getString("key");
151 int code
= mouse ?
getMouseKey(name
) : Input
.Keys
.valueOf(name
);
153 x
= assetLoader
.getGameRendererWidth() + x
;
156 y
= assetLoader
.getGameRendererHeight() + y
;
158 Assets
.guiMap
.put(key
.name(), new TouchButton(new Rectangle(x
, y
, w
, h
), code
, mouse
));
163 private static Texture
resolveTexture(AssetLoader assetLoader
, String textureName
, String lookUpPath
, Map
<String
, Texture
> cache
) {
164 if (cache
.containsKey(textureName
)) {
165 return cache
.get(textureName
);
168 final Texture texture
= loadTexture(assetLoader
.getAssetHandle(lookUpPath
+ File
.separator
+ textureName
+ ".png"));
169 cache
.put(textureName
, texture
);
174 public static Texture
resolveItemTexture(AssetLoader assetLoader
, String textureName
) {
175 return resolveTexture(assetLoader
, textureName
, "pp/textures/items", itemTextures
);
178 public static Texture
resolveBlockTexture(AssetLoader assetLoader
, String textureName
) {
179 return resolveTexture(assetLoader
, textureName
, "pp/textures/blocks", blockTextures
);
182 private static void loadAllPngsFromDirInto(FileHandle dir
, Map
<String
, Texture
> loadInto
) {
183 for (FileHandle handle
: dir
.list((d
, name
) -> name
.endsWith(".png"))) {
184 loadInto
.put(handle
.nameWithoutExtension(), loadTexture(handle
));
188 private static void loadItems(AssetLoader assetLoader
) {
189 final FileHandle itemsDir
= assetLoader
.getAssetHandle("pp/textures/items");
190 loadAllPngsFromDirInto(itemsDir
, itemTextures
);
193 private static void loadBlocks(AssetLoader assetLoader
) {
194 final FileHandle blocksDir
= assetLoader
.getAssetHandle("pp/textures/blocks");
195 loadAllPngsFromDirInto(blocksDir
, blockTextures
);
198 private static void loadJoystick(AssetLoader assetLoader
) {
199 joyStick
= new Sprite(loadTexture(assetLoader
.getAssetHandle("joy_stick.png")));
200 joyBackground
= new Sprite(loadTexture(assetLoader
.getAssetHandle("joy_background.png")));
203 private static void loadFurnace(AssetLoader assetLoader
) {
204 furnaceBurn
= new Sprite(textureRegions
.get("furnace_burn"));
205 furnaceProgress
= new Sprite(textureRegions
.get("furnace_progress"));
208 public static void load(final AssetLoader assetLoader
) {
209 loadMob(assetLoader
, playerSprite
, "char");
210 loadMob(assetLoader
, pigSprite
, "pig");
211 loadBlockDamage(assetLoader
);
212 loadJSON(assetLoader
);
213 loadBlocks(assetLoader
);
214 loadItems(assetLoader
);
215 loadTouchButtonsFromJSON(assetLoader
);
216 loadJoystick(assetLoader
);
217 loadFurnace(assetLoader
);
218 setPlayerHeadOrigin();
219 minecraftFont
= new BitmapFont(assetLoader
.getAssetHandle("font.fnt"), true);
220 minecraftFont
.getData().setScale(.375f);
221 minecraftFont
.setUseIntegerPositions(false);
225 * @param s string whose width you want to know
226 * @return A width of string written in {@link #minecraftFont} in pixels
228 public static int getStringWidth(String s
) {
229 glyphLayout
.setText(minecraftFont
, s
);
230 return (int) glyphLayout
.width
;
234 * @param s string whose height you want to know
235 * @return A height of string written in {@link #minecraftFont} in pixels
237 public static int getStringHeight(String s
) {
238 glyphLayout
.setText(minecraftFont
, s
);
239 return (int) glyphLayout
.height
;
242 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
243 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
246 public static float getFloatFromJson(JsonValue json
, String name
, float defaultValue
) {
247 return json
.has(name
) ? json
.getFloat(name
) : defaultValue
;
250 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
251 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
254 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
255 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;