DEADSOFTWARE

Add furnace, more craft and items
[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 loadedTextures.forEach(Texture::dispose);
53 loadedTextures.clear();
54 }
56 private static Texture loadTexture(FileHandle fileHandle) {
57 Texture texture = new Texture(fileHandle);
58 loadedTextures.add(texture);
59 return texture;
60 }
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);
64 }
66 private static Sprite flippedSprite(Texture texture) {
67 Sprite sprite = new Sprite(texture);
68 sprite.flip(false, true);
69 return sprite;
70 }
72 private static Sprite flippedSprite(TextureRegion texture) {
73 Sprite sprite = new Sprite(texture);
74 sprite.flip(false, true);
75 return sprite;
76 }
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("mobs/" + mob + "/" + i + "_" + j + ".png")));
83 sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0);
84 }
85 }
86 }
88 private static void loadBlockDamage(AssetLoader assetLoader) {
89 final Texture blockDamageTexture = loadTexture(assetLoader.getAssetHandle("break.png"));
90 for (int i = 0; i < BLOCK_DAMAGE_STAGES; i++) {
91 blockDamageSprites[i] = new Sprite(flippedRegion(blockDamageTexture, i * 16, 0, 16, 16));
92 }
93 }
95 private static void setPlayerHeadOrigin() {
96 for (Sprite[] sprites : playerSprite) {
97 sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
98 }
99 }
101 /**
102 * Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
103 * and puts to {@link #textureRegions} HashMap
104 */
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 if (file.size == 0) {
110 textureRegions.put(file.name(),
111 flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
112 } else {
113 for (JsonValue key = file.child(); key != null; key = key.next()) {
114 int x = getIntFromJson(key, "x", 0);
115 int y = getIntFromJson(key, "y", 0);
116 int w = getIntFromJson(key, "w", texture.getWidth());
117 int h = getIntFromJson(key, "h", texture.getHeight());
118 textureRegions.put(key.name(), flippedRegion(texture, x, y, w, h));
124 private static int getMouseKey(String name) {
125 switch (name) {
126 case "Left":
127 return Input.Buttons.LEFT;
128 case "Right":
129 return Input.Buttons.RIGHT;
130 case "Middle":
131 return Input.Buttons.MIDDLE;
132 case "Back":
133 return Input.Buttons.BACK;
134 case "Forward":
135 return Input.Buttons.FORWARD;
136 default:
137 return -1;
141 private static void loadTouchButtonsFromJSON(AssetLoader assetLoader) {
142 JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/touch_buttons.json"));
143 for (JsonValue key = json.child(); key != null; key = key.next()) {
144 float x = key.getFloat("x");
145 float y = key.getFloat("y");
146 float w = key.getFloat("w");
147 float h = key.getFloat("h");
148 boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
149 String name = key.getString("key");
150 int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
151 if (x < 0) {
152 x = assetLoader.getGameRendererWidth() + x;
154 if (y < 0) {
155 y = assetLoader.getGameRendererHeight() + y;
157 Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
162 private static Texture resolveTexture(AssetLoader assetLoader, String textureName, String lookUpPath, Map<String, Texture> cache) {
163 if (cache.containsKey(textureName)) {
164 return cache.get(textureName);
167 final Texture texture = loadTexture(assetLoader.getAssetHandle(lookUpPath + File.separator + textureName + ".png"));
168 cache.put(textureName, texture);
170 return texture;
173 public static Texture resolveItemTexture(AssetLoader assetLoader, String textureName) {
174 return resolveTexture(assetLoader, textureName, "textures/items", itemTextures);
177 public static Texture resolveBlockTexture(AssetLoader assetLoader, String textureName) {
178 return resolveTexture(assetLoader, textureName, "textures/blocks", blockTextures);
181 private static void loadAllPngsFromDirInto(FileHandle dir, Map<String, Texture> loadInto) {
182 for (FileHandle handle : dir.list((d, name) -> name.endsWith(".png"))) {
183 loadInto.put(handle.nameWithoutExtension(), loadTexture(handle));
187 private static void loadItems(AssetLoader assetLoader) {
188 final FileHandle itemsDir = assetLoader.getAssetHandle("textures/items");
189 loadAllPngsFromDirInto(itemsDir, itemTextures);
192 private static void loadBlocks(AssetLoader assetLoader) {
193 final FileHandle blocksDir = assetLoader.getAssetHandle("textures/blocks");
194 loadAllPngsFromDirInto(blocksDir, blockTextures);
197 private static void loadJoystick(AssetLoader assetLoader) {
198 joyStick = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_stick.png")));
199 joyBackground = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_background.png")));
202 private static void loadFurnace(AssetLoader assetLoader) {
203 furnaceBurn = new Sprite(textureRegions.get("furnace_burn"));
204 furnaceProgress = new Sprite(textureRegions.get("furnace_progress"));
207 public static void load(final AssetLoader assetLoader) {
208 loadMob(assetLoader, playerSprite, "char");
209 loadMob(assetLoader, pigSprite, "pig");
210 loadBlockDamage(assetLoader);
211 loadJSON(assetLoader);
212 loadBlocks(assetLoader);
213 loadItems(assetLoader);
214 loadTouchButtonsFromJSON(assetLoader);
215 loadJoystick(assetLoader);
216 loadFurnace(assetLoader);
217 setPlayerHeadOrigin();
218 minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true);
219 minecraftFont.getData().setScale(.375f);
220 minecraftFont.setUseIntegerPositions(false);
223 /**
224 * @param s string whose width you want to know
225 * @return A width of string written in {@link #minecraftFont} in pixels
226 */
227 public static int getStringWidth(String s) {
228 glyphLayout.setText(minecraftFont, s);
229 return (int) glyphLayout.width;
232 /**
233 * @param s string whose height you want to know
234 * @return A height of string written in {@link #minecraftFont} in pixels
235 */
236 public static int getStringHeight(String s) {
237 glyphLayout.setText(minecraftFont, s);
238 return (int) glyphLayout.height;
241 public static int getIntFromJson(JsonValue json, String name, int defaultValue) {
242 return json.has(name) ? json.getInt(name) : defaultValue;
245 public static float getFloatFromJson(JsonValue json, String name, float defaultValue) {
246 return json.has(name) ? json.getFloat(name) : defaultValue;
249 public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
250 return json.has(name) ? json.getString(name) : defaultValue;
253 public static boolean getBooleanFromJson(JsonValue json, String name, boolean defaultValue) {
254 return json.has(name) ? json.getBoolean(name) : defaultValue;