X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fmisc%2FAssets.java;h=abec0a0b2f5f0a4ecac1a6128b1a3f622445f1a1;hb=8b34480aaaf0112671d319accff573030079c7b6;hp=90a7ec1bec4c818a12286161603d751152586f7e;hpb=e7a1e15a93abaafa8e2e0435336483a198cc697c;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index 90a7ec1..abec0a0 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -1,6 +1,6 @@ package ru.deadsoftware.cavedroid.misc; -import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.GlyphLayout; @@ -10,18 +10,45 @@ import com.badlogic.gdx.utils.ArrayMap; import com.badlogic.gdx.utils.JsonReader; import com.badlogic.gdx.utils.JsonValue; import ru.deadsoftware.cavedroid.game.objects.TouchButton; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; public class Assets { + private static final int BLOCK_DAMAGE_STAGES = 10; + public static final JsonReader jsonReader = new JsonReader(); + + private static final List loadedTextures = new LinkedList<>(); + public static final Sprite[][] playerSprite = new Sprite[2][4]; public static final Sprite[][] pigSprite = new Sprite[2][2]; + + public static final Sprite[] blockDamageSprites = new Sprite[10]; + public static final HashMap textureRegions = new HashMap<>(); public static final ArrayMap guiMap = new ArrayMap<>(); private static final GlyphLayout glyphLayout = new GlyphLayout(); - static BitmapFont minecraftFont; + public static BitmapFont minecraftFont; + + public static Map blockTextures = new HashMap<>(); + public static Map itemTextures = new HashMap<>(); + + public static void dispose() { + minecraftFont.dispose(); + loadedTextures.forEach(Texture::dispose); + loadedTextures.clear(); + } + + private static Texture loadTexture(FileHandle fileHandle) { + Texture texture = new Texture(fileHandle); + loadedTextures.add(texture); + return texture; + } private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) { return new TextureRegion(texture, x, y + height, width, -height); @@ -39,16 +66,23 @@ public class Assets { return sprite; } - private static void loadMob(Sprite[][] sprite, String mob) { + private static void loadMob(AssetLoader assetLoader, Sprite[][] sprite, String mob) { for (int i = 0; i < sprite.length; i++) { for (int j = 0; j < sprite[i].length; j++) { - sprite[i][j] = flippedSprite(new Texture( - Gdx.files.internal("mobs/" + mob + "/" + i + "_" + j + ".png"))); + sprite[i][j] = flippedSprite(loadTexture( + assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png"))); sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0); } } } + private static void loadBlockDamage(AssetLoader assetLoader) { + final Texture blockDamageTexture = loadTexture(assetLoader.getAssetHandle("break.png")); + for (int i = 0; i < BLOCK_DAMAGE_STAGES; i++) { + blockDamageSprites[i] = new Sprite(flippedRegion(blockDamageTexture, i * 16, 0, 16, 16)); + } + } + private static void setPlayerHeadOrigin() { for (Sprite[] sprites : playerSprite) { sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight()); @@ -59,10 +93,10 @@ public class Assets { * Loads texture names and sizes from json/texture_regions.json, cuts them to TextureRegions * and puts to {@link #textureRegions} HashMap */ - private static void loadJSON() { - JsonValue json = jsonReader.parse(Gdx.files.internal("json/texture_regions.json")); + private static void loadJSON(AssetLoader assetLoader) { + JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json")); for (JsonValue file = json.child(); file != null; file = file.next()) { - Texture texture = new Texture(Gdx.files.internal(file.name() + ".png")); + Texture texture = loadTexture(assetLoader.getAssetHandle(file.name() + ".png")); if (file.size == 0) { textureRegions.put(file.name(), flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight())); @@ -78,12 +112,31 @@ public class Assets { } } - public static void load() { - loadMob(playerSprite, "char"); - loadMob(pigSprite, "pig"); - loadJSON(); + private static void loadAllPngsFromDirInto(FileHandle dir, Map loadInto) { + for (FileHandle handle : dir.list((d, name) -> name.endsWith(".png"))) { + loadInto.put(handle.nameWithoutExtension(), loadTexture(handle)); + } + } + + private static void loadItems(AssetLoader assetLoader) { + final FileHandle itemsDir = assetLoader.getAssetHandle("textures/items"); + loadAllPngsFromDirInto(itemsDir, itemTextures); + } + + private static void loadBlocks(AssetLoader assetLoader) { + final FileHandle blocksDir = assetLoader.getAssetHandle("textures/blocks"); + loadAllPngsFromDirInto(blocksDir, blockTextures); + } + + public static void load(final AssetLoader assetLoader) { + loadMob(assetLoader, playerSprite, "char"); + loadMob(assetLoader, pigSprite, "pig"); + loadBlockDamage(assetLoader); + loadJSON(assetLoader); + loadBlocks(assetLoader); + loadItems(assetLoader); setPlayerHeadOrigin(); - minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true); + minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true); minecraftFont.getData().setScale(.375f); } @@ -109,6 +162,10 @@ public class Assets { return json.has(name) ? json.getInt(name) : defaultValue; } + public static float getFloatFromJson(JsonValue json, String name, float defaultValue) { + return json.has(name) ? json.getFloat(name) : defaultValue; + } + public static String getStringFromJson(JsonValue json, String name, String defaultValue) { return json.has(name) ? json.getString(name) : defaultValue; }