DEADSOFTWARE

CaveGame in kotlin
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / Assets.java
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;
17 import java.io.File;
18 import java.util.HashMap;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Map;
23 public class Assets {
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 for (Texture texture : loadedTextures) {
53 texture.dispose();
54 }
55 loadedTextures.clear();
56 }
58 private static Texture loadTexture(FileHandle fileHandle) {
59 Texture texture = new Texture(fileHandle);
60 loadedTextures.add(texture);
61 return texture;
62 }
64 private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
65 return new TextureRegion(texture, x, y + height, width, -height);
66 }
68 private static Sprite flippedSprite(Texture texture) {
69 Sprite sprite = new Sprite(texture);
70 sprite.flip(false, true);
71 return sprite;
72 }
74 private static Sprite flippedSprite(TextureRegion texture) {
75 Sprite sprite = new Sprite(texture);
76 sprite.flip(false, true);
77 return sprite;
78 }
80 private static void loadMob(AssetLoader assetLoader, Sprite[][] sprite, String mob) {
81 for (int i = 0; i < sprite.length; i++) {
82 for (int j = 0; j < sprite[i].length; j++) {
83 sprite[i][j] = flippedSprite(loadTexture(
84 assetLoader.getAssetHandle("pp/mobs/" + mob + "/" + i + "_" + j + ".png")));
85 sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0);
86 }
87 }
88 }
90 private static void loadBlockDamage(AssetLoader assetLoader) {
91 final Texture blockDamageTexture = loadTexture(assetLoader.getAssetHandle("pp/break.png"));
92 for (int i = 0; i < BLOCK_DAMAGE_STAGES; i++) {
93 blockDamageSprites[i] = new Sprite(flippedRegion(blockDamageTexture, i * 16, 0, 16, 16));
94 }
95 }
97 private static void setPlayerHeadOrigin() {
98 for (Sprite[] sprites : playerSprite) {
99 sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
103 /**
104 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
105 * and puts to {@link #textureRegions} HashMap
106 */
107 private static void loadJSON(AssetLoader assetLoader) {
108 JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json"));
109 for (JsonValue file = json.child(); file != null; file = file.next()) {
110 Texture texture = loadTexture(assetLoader.getAssetHandle(file.name() + ".png"));
111 final String[] pathSegments = file.name().split("/");
112 final String name = pathSegments[pathSegments.length - 1];
113 if (file.size == 0) {
114 textureRegions.put(name, flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
115 } else {
116 for (JsonValue key = file.child(); key != null; key = key.next()) {
117 int x = getIntFromJson(key, "x", 0);
118 int y = getIntFromJson(key, "y", 0);
119 int w = getIntFromJson(key, "w", texture.getWidth());
120 int h = getIntFromJson(key, "h", texture.getHeight());
121 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
127 private static int getMouseKey(String name) {
128 switch (name) {
129 case "Left":
130 return Input.Buttons.LEFT;
131 case "Right":
132 return Input.Buttons.RIGHT;
133 case "Middle":
134 return Input.Buttons.MIDDLE;
135 case "Back":
136 return Input.Buttons.BACK;
137 case "Forward":
138 return Input.Buttons.FORWARD;
139 default:
140 return -1;
144 private static void loadTouchButtonsFromJSON(AssetLoader assetLoader) {
145 JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/touch_buttons.json"));
146 for (JsonValue key = json.child(); key != null; key = key.next()) {
147 float x = key.getFloat("x");
148 float y = key.getFloat("y");
149 float w = key.getFloat("w");
150 float h = key.getFloat("h");
151 boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
152 String name = key.getString("key");
153 int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
154 if (x < 0) {
155 x = assetLoader.getGameRendererWidth() + x;
157 if (y < 0) {
158 y = assetLoader.getGameRendererHeight() + y;
160 Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
165 private static Texture resolveTexture(AssetLoader assetLoader, String textureName, String lookUpPath, Map<String, Texture> cache) {
166 if (cache.containsKey(textureName)) {
167 return cache.get(textureName);
170 final Texture texture = loadTexture(assetLoader.getAssetHandle(lookUpPath + File.separator + textureName + ".png"));
171 cache.put(textureName, texture);
173 return texture;
176 public static Texture resolveItemTexture(AssetLoader assetLoader, String textureName) {
177 return resolveTexture(assetLoader, textureName, "pp/textures/items", itemTextures);
180 public static Texture resolveBlockTexture(AssetLoader assetLoader, String textureName) {
181 return resolveTexture(assetLoader, textureName, "pp/textures/blocks", blockTextures);
184 private static void loadAllPngsFromDirInto(FileHandle dir, Map<String, Texture> loadInto) {
185 for (FileHandle handle : dir.list((d, name) -> name.endsWith(".png"))) {
186 loadInto.put(handle.nameWithoutExtension(), loadTexture(handle));
190 private static void loadItems(AssetLoader assetLoader) {
191 final FileHandle itemsDir = assetLoader.getAssetHandle("pp/textures/items");
192 loadAllPngsFromDirInto(itemsDir, itemTextures);
195 private static void loadBlocks(AssetLoader assetLoader) {
196 final FileHandle blocksDir = assetLoader.getAssetHandle("pp/textures/blocks");
197 loadAllPngsFromDirInto(blocksDir, blockTextures);
200 private static void loadJoystick(AssetLoader assetLoader) {
201 joyStick = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_stick.png")));
202 joyBackground = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_background.png")));
205 private static void loadFurnace(AssetLoader assetLoader) {
206 furnaceBurn = new Sprite(textureRegions.get("furnace_burn"));
207 furnaceProgress = new Sprite(textureRegions.get("furnace_progress"));
210 public static void load(final AssetLoader assetLoader) {
211 loadMob(assetLoader, playerSprite, "char");
212 loadMob(assetLoader, pigSprite, "pig");
213 loadBlockDamage(assetLoader);
214 loadJSON(assetLoader);
215 loadBlocks(assetLoader);
216 loadItems(assetLoader);
217 loadTouchButtonsFromJSON(assetLoader);
218 loadJoystick(assetLoader);
219 loadFurnace(assetLoader);
220 setPlayerHeadOrigin();
221 minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true);
222 minecraftFont.getData().setScale(.375f);
223 minecraftFont.setUseIntegerPositions(false);
226 /**
227 * @param s string whose width you want to know
228 * @return A width of string written in {@link #minecraftFont} in pixels
229 */
230 public static int getStringWidth(String s) {
231 glyphLayout.setText(minecraftFont, s);
232 return (int) glyphLayout.width;
235 /**
236 * @param s string whose height you want to know
237 * @return A height of string written in {@link #minecraftFont} in pixels
238 */
239 public static int getStringHeight(String s) {
240 glyphLayout.setText(minecraftFont, s);
241 return (int) glyphLayout.height;
244 public static int getIntFromJson(JsonValue json, String name, int defaultValue) {
245 return json.has(name) ? json.getInt(name) : defaultValue;
248 public static float getFloatFromJson(JsonValue json, String name, float defaultValue) {
249 return json.has(name) ? json.getFloat(name) : defaultValue;
252 public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
253 return json.has(name) ? json.getString(name) : defaultValue;
256 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
257 return json.has(name) ? json.getBoolean(name) : defaultValue;