abec0a0b2f5f0a4ecac1a6128b1a3f622445f1a1
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 private static final int BLOCK_DAMAGE_STAGES
= 10;
24 public static final JsonReader jsonReader
= new JsonReader();
26 private static final List
<Texture
> loadedTextures
= new LinkedList
<>();
28 public static final Sprite
[][] playerSprite
= new Sprite
[2][4];
29 public static final Sprite
[][] pigSprite
= new Sprite
[2][2];
31 public static final Sprite
[] blockDamageSprites
= new Sprite
[10];
33 public static final HashMap
<String
, TextureRegion
> textureRegions
= new HashMap
<>();
34 public static final ArrayMap
<String
, TouchButton
> guiMap
= new ArrayMap
<>();
35 private static final GlyphLayout glyphLayout
= new GlyphLayout();
36 public static BitmapFont minecraftFont
;
38 public static Map
<String
, Texture
> blockTextures
= new HashMap
<>();
39 public static Map
<String
, Texture
> itemTextures
= new HashMap
<>();
41 public static void dispose() {
42 minecraftFont
.dispose();
43 loadedTextures
.forEach(Texture
::dispose
);
44 loadedTextures
.clear();
47 private static Texture
loadTexture(FileHandle fileHandle
) {
48 Texture texture
= new Texture(fileHandle
);
49 loadedTextures
.add(texture
);
53 private static TextureRegion
flippedRegion(Texture texture
, int x
, int y
, int width
, int height
) {
54 return new TextureRegion(texture
, x
, y
+ height
, width
, -height
);
57 private static Sprite
flippedSprite(Texture texture
) {
58 Sprite sprite
= new Sprite(texture
);
59 sprite
.flip(false, true);
63 private static Sprite
flippedSprite(TextureRegion texture
) {
64 Sprite sprite
= new Sprite(texture
);
65 sprite
.flip(false, true);
69 private static void loadMob(AssetLoader assetLoader
, Sprite
[][] sprite
, String mob
) {
70 for (int i
= 0; i
< sprite
.length
; i
++) {
71 for (int j
= 0; j
< sprite
[i
].length
; j
++) {
72 sprite
[i
][j
] = flippedSprite(loadTexture(
73 assetLoader
.getAssetHandle("mobs/" + mob
+ "/" + i
+ "_" + j
+ ".png")));
74 sprite
[i
][j
].setOrigin(sprite
[i
][j
].getWidth() / 2, 0);
79 private static void loadBlockDamage(AssetLoader assetLoader
) {
80 final Texture blockDamageTexture
= loadTexture(assetLoader
.getAssetHandle("break.png"));
81 for (int i
= 0; i
< BLOCK_DAMAGE_STAGES
; i
++) {
82 blockDamageSprites
[i
] = new Sprite(flippedRegion(blockDamageTexture
, i
* 16, 0, 16, 16));
86 private static void setPlayerHeadOrigin() {
87 for (Sprite
[] sprites
: playerSprite
) {
88 sprites
[0].setOrigin(sprites
[0].getWidth() / 2, sprites
[0].getHeight());
93 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
94 * and puts to {@link #textureRegions} HashMap
96 private static void loadJSON(AssetLoader assetLoader
) {
97 JsonValue json
= jsonReader
.parse(assetLoader
.getAssetHandle("json/texture_regions.json"));
98 for (JsonValue file
= json
.child(); file
!= null; file
= file
.next()) {
99 Texture texture
= loadTexture(assetLoader
.getAssetHandle(file
.name() + ".png"));
100 if (file
.size
== 0) {
101 textureRegions
.put(file
.name(),
102 flippedRegion(texture
, 0, 0, texture
.getWidth(), texture
.getHeight()));
104 for (JsonValue key
= file
.child(); key
!= null; key
= key
.next()) {
105 int x
= getIntFromJson(key
, "x", 0);
106 int y
= getIntFromJson(key
, "y", 0);
107 int w
= getIntFromJson(key
, "w", texture
.getWidth());
108 int h
= getIntFromJson(key
, "h", texture
.getHeight());
109 textureRegions
.put(key
.name(), flippedRegion(texture
, x
, y
, w
, h
));
115 private static void loadAllPngsFromDirInto(FileHandle dir
, Map
<String
, Texture
> loadInto
) {
116 for (FileHandle handle
: dir
.list((d
, name
) -> name
.endsWith(".png"))) {
117 loadInto
.put(handle
.nameWithoutExtension(), loadTexture(handle
));
121 private static void loadItems(AssetLoader assetLoader
) {
122 final FileHandle itemsDir
= assetLoader
.getAssetHandle("textures/items");
123 loadAllPngsFromDirInto(itemsDir
, itemTextures
);
126 private static void loadBlocks(AssetLoader assetLoader
) {
127 final FileHandle blocksDir
= assetLoader
.getAssetHandle("textures/blocks");
128 loadAllPngsFromDirInto(blocksDir
, blockTextures
);
131 public static void load(final AssetLoader assetLoader
) {
132 loadMob(assetLoader
, playerSprite
, "char");
133 loadMob(assetLoader
, pigSprite
, "pig");
134 loadBlockDamage(assetLoader
);
135 loadJSON(assetLoader
);
136 loadBlocks(assetLoader
);
137 loadItems(assetLoader
);
138 setPlayerHeadOrigin();
139 minecraftFont
= new BitmapFont(assetLoader
.getAssetHandle("font.fnt"), true);
140 minecraftFont
.getData().setScale(.375f);
144 * @param s string whose width you want to know
145 * @return A width of string written in {@link #minecraftFont} in pixels
147 public static int getStringWidth(String s
) {
148 glyphLayout
.setText(minecraftFont
, s
);
149 return (int) glyphLayout
.width
;
153 * @param s string whose height you want to know
154 * @return A height of string written in {@link #minecraftFont} in pixels
156 public static int getStringHeight(String s
) {
157 glyphLayout
.setText(minecraftFont
, s
);
158 return (int) glyphLayout
.height
;
161 public static int getIntFromJson(JsonValue json
, String name
, int defaultValue
) {
162 return json
.has(name
) ? json
.getInt(name
) : defaultValue
;
165 public static float getFloatFromJson(JsonValue json
, String name
, float defaultValue
) {
166 return json
.has(name
) ? json
.getFloat(name
) : defaultValue
;
169 public static String
getStringFromJson(JsonValue json
, String name
, String defaultValue
) {
170 return json
.has(name
) ? json
.getString(name
) : defaultValue
;
173 public static boolean getBooleanFromJson(JsonValue json
, String name
, boolean defaultValue
) {
174 return json
.has(name
) ? json
.getBoolean(name
) : defaultValue
;