From 213d66fcddbc54faf262c3136be61fad9c35ffb0 Mon Sep 17 00:00:00 2001 From: fredboy Date: Tue, 16 Apr 2024 23:03:16 +0700 Subject: [PATCH] Add support for external assets --- .../cavedroid/AndroidLauncher.java | 2 +- .../ru/deadsoftware/cavedroid/CaveGame.java | 19 +++++++++--- .../deadsoftware/cavedroid/MainComponent.java | 3 ++ .../ru/deadsoftware/cavedroid/MainConfig.java | 13 ++++++++ .../cavedroid/game/GameInputProcessor.java | 8 +++-- .../cavedroid/game/GameItems.java | 9 +++--- .../deadsoftware/cavedroid/game/mobs/Pig.java | 10 ------ .../deadsoftware/cavedroid/menu/MenuProc.java | 10 ++++-- .../cavedroid/menu/submenus/Menu.java | 10 +++++- .../cavedroid/menu/submenus/MenuMain.java | 31 +++++++++++++++++-- .../cavedroid/menu/submenus/MenuNewGame.java | 31 ++++++++++++++++--- .../deadsoftware/cavedroid/misc/Assets.java | 21 +++++++------ .../cavedroid/misc/utils/AssetLoader.kt | 24 ++++++++++++++ .../cavedroid/desktop/DesktopLauncher.java | 19 ++++++++++-- 14 files changed, 166 insertions(+), 44 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/misc/utils/AssetLoader.kt diff --git a/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java b/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java index 88b6261..e7aad50 100644 --- a/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java +++ b/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java @@ -18,7 +18,7 @@ public class AndroidLauncher extends AndroidApplication { e.printStackTrace(); exit(); } - CaveGame caveGame = new CaveGame(gameFolder, true); + CaveGame caveGame = new CaveGame(gameFolder, true, null); caveGame.setDebug(BuildConfig.DEBUG); initialize(caveGame, config); } diff --git a/core/src/ru/deadsoftware/cavedroid/CaveGame.java b/core/src/ru/deadsoftware/cavedroid/CaveGame.java index 1f628a9..287b67d 100644 --- a/core/src/ru/deadsoftware/cavedroid/CaveGame.java +++ b/core/src/ru/deadsoftware/cavedroid/CaveGame.java @@ -5,6 +5,9 @@ import com.badlogic.gdx.Gdx; import ru.deadsoftware.cavedroid.game.GameItems; import ru.deadsoftware.cavedroid.game.GameScreen; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; + +import javax.annotation.Nullable; public class CaveGame extends Game { @@ -14,17 +17,24 @@ public class CaveGame extends Game { private final MainConfig mMainConfig; private final MainComponent mMainComponent; + private final AssetLoader mAssetLoader; private final String mGameFolder; private final boolean mTouch; private boolean mDebug; - public CaveGame(String gameFolder, boolean touch) { + @Nullable + private final String mAssetsPackPath; + + public CaveGame(String gameFolder, boolean touch, @Nullable String assetsPackPath) { mGameFolder = gameFolder; mTouch = touch; + mAssetsPackPath = assetsPackPath; mMainComponent = DaggerMainComponent.builder().caveGame(this).build(); + mMainConfig = mMainComponent.getMainConfig(); + mAssetLoader = mMainComponent.getAssetLoader(); } public void setDebug(boolean debug) { @@ -41,6 +51,7 @@ public class CaveGame extends Game { mMainConfig.setWidth(width); mMainConfig.setHeight(height); mMainConfig.setShowInfo(mDebug); + mMainConfig.setAssetsPackPath(mAssetsPackPath); } public void newGame() { @@ -64,11 +75,11 @@ public class CaveGame extends Game { Gdx.app.log(TAG, mGameFolder); Gdx.files.absolute(mGameFolder).mkdirs(); - Assets.load(); - GameItems.load(); - initConfig(); + Assets.load(mAssetLoader); + GameItems.load(mAssetLoader); + setScreen(mMainComponent.getMenuScreen()); } diff --git a/core/src/ru/deadsoftware/cavedroid/MainComponent.java b/core/src/ru/deadsoftware/cavedroid/MainComponent.java index d8a05da..c640fb6 100644 --- a/core/src/ru/deadsoftware/cavedroid/MainComponent.java +++ b/core/src/ru/deadsoftware/cavedroid/MainComponent.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid; import dagger.Component; import ru.deadsoftware.cavedroid.game.GameScreen; import ru.deadsoftware.cavedroid.menu.MenuScreen; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; import javax.inject.Singleton; @@ -14,4 +15,6 @@ public interface MainComponent { MenuScreen getMenuScreen(); MainConfig getMainConfig(); + + AssetLoader getAssetLoader(); } diff --git a/core/src/ru/deadsoftware/cavedroid/MainConfig.java b/core/src/ru/deadsoftware/cavedroid/MainConfig.java index 8cd0ffd..ff077ee 100644 --- a/core/src/ru/deadsoftware/cavedroid/MainConfig.java +++ b/core/src/ru/deadsoftware/cavedroid/MainConfig.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid; import ru.deadsoftware.cavedroid.game.GameUiWindow; import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Singleton; @@ -24,6 +25,9 @@ public class MainConfig { private float mWidth; private float mHeight; + @Nullable + private String mAssetsPackPath = null; + @Inject public MainConfig(CaveGame caveGame) { mCaveGame = caveGame; @@ -104,4 +108,13 @@ public class MainConfig { public void setShowMap(boolean showMap) { mShowMap = showMap; } + + @Nullable + public String getAssetsPackPath() { + return mAssetsPackPath; + } + + public void setAssetsPackPath(@Nullable String assetsPackPath) { + mAssetsPackPath = assetsPackPath; + } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java b/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java index 5d86b2e..7eb35bd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.utils.JsonValue; import ru.deadsoftware.cavedroid.MainConfig; import ru.deadsoftware.cavedroid.game.objects.TouchButton; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; import javax.inject.Inject; @@ -21,14 +22,17 @@ public class GameInputProcessor extends InputAdapter { private final GameInput mGameInput; private final GameRenderer mGameRenderer; private final MainConfig mMainConfig; + private final AssetLoader mAssetLoader; @Inject public GameInputProcessor(GameInput gameInput, GameRenderer gameRenderer, - MainConfig mainConfig) { + MainConfig mainConfig, + AssetLoader assetLoader) { mGameInput = gameInput; mGameRenderer = gameRenderer; mMainConfig = mainConfig; + mAssetLoader = assetLoader; loadTouchButtonsFromJSON(); } @@ -51,7 +55,7 @@ public class GameInputProcessor extends InputAdapter { } private void loadTouchButtonsFromJSON() { - JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json")); + JsonValue json = Assets.jsonReader.parse(mAssetLoader.getAssetHandle("json/touch_buttons.json")); for (JsonValue key = json.child(); key != null; key = key.next()) { float x = key.getFloat("x"); float y = key.getFloat("y"); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java index 1cebf86..7a619b3 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java @@ -9,6 +9,7 @@ import com.badlogic.gdx.utils.JsonValue; import ru.deadsoftware.cavedroid.game.objects.Block; import ru.deadsoftware.cavedroid.game.objects.Item; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; import java.util.HashMap; @@ -104,8 +105,8 @@ public class GameItems { return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture(); } - public static void load() { - JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/game_items.json")); + public static void load(AssetLoader assetLoader) { + JsonValue json = Assets.jsonReader.parse(assetLoader.getAssetHandle("json/game_items.json")); for (JsonValue block = json.get("blocks").child(); block != null; block = block.next()) { try { String key = block.name(); @@ -127,7 +128,7 @@ public class GameItems { String meta = Assets.getStringFromJson(block, "meta", ""); String tex = Assets.getStringFromJson(block, "texture", key); Texture texture = tex.equals("none") ? null : - new Texture(Gdx.files.internal("textures/blocks/" + tex + ".png")); + new Texture(assetLoader.getAssetHandle("textures/blocks/" + tex + ".png")); boolean animated = Assets.getBooleanFromJson(block, "animated", false); int frames = Assets.getIntFromJson(block, "frames", 0); @@ -166,7 +167,7 @@ public class GameItems { String type = Assets.getStringFromJson(item, "type", "item"); String texture = Assets.getStringFromJson(item, "texture", key); Sprite sprite = type.equals("block") ? null : - new Sprite(new Texture(Gdx.files.internal("textures/items/" + texture + ".png"))); + new Sprite(new Texture(assetLoader.getAssetHandle("textures/items/" + texture + ".png"))); itemsIds.put(key, items.size); items.put(key, new Item(name, type, sprite)); } catch (GdxRuntimeException e) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java index 376c68b..2290387 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java @@ -33,16 +33,6 @@ public class Pig extends Mob { changeDir(); } } - - if (mVelocity.x != 0f) { - mAnim += mAnimDelta * delta; - } else { - mAnim = 0; - } - - if (mAnim >= 60 || mAnim <= -60) { - mAnimDelta = -mAnimDelta; - } } @Override diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java index f96d4d4..2b64520 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java @@ -55,15 +55,19 @@ public class MenuProc extends Renderer { private Menu mCurrentMenu; @Inject - public MenuProc(MainConfig mainConfig) { + public MenuProc( + MainConfig mainConfig, + MenuMain.Factory menuMainFactory, + MenuNewGame.Factory menuNewGameFactory + ) { super(mainConfig.getWidth(), mainConfig.getHeight()); mMainConfig = mainConfig; Input menuInput = new Input(); - mMenuMain = new MenuMain(getWidth(), getHeight(), this::drawButton, mainConfig, menuInput); - mMenuNewGame = new MenuNewGame(getWidth(), getHeight(), this::drawButton, mainConfig, menuInput); + mMenuMain = menuMainFactory.get(getWidth(), getHeight(), this::drawButton, menuInput); + mMenuNewGame = menuNewGameFactory.get(getWidth(), getHeight(), this::drawButton, menuInput); mCurrentMenu = mMenuMain; } diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java index 9f8df4c..587d0f6 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.menu.objects.Button; import ru.deadsoftware.cavedroid.menu.objects.ButtonEventListener; import ru.deadsoftware.cavedroid.menu.objects.ButtonRenderer; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; import java.util.HashMap; @@ -19,6 +20,7 @@ public abstract class Menu { protected final MainConfig mMainConfig; protected final MenuProc.Input mMenuInput; + protected final AssetLoader mAssetLoader; private final ButtonRenderer mButtonRenderer; @@ -35,12 +37,18 @@ public abstract class Menu { * @param height Viewport height * @param buttonRenderer {@link ButtonRenderer} that will draw the buttons of this menu */ - Menu(float width, float height, ButtonRenderer buttonRenderer, MainConfig mainConfig, MenuProc.Input menuInput) { + Menu(float width, + float height, + ButtonRenderer buttonRenderer, + MainConfig mainConfig, + MenuProc.Input menuInput, + AssetLoader assetLoader) { mWidth = width; mHeight = height; mButtonRenderer = buttonRenderer; mMainConfig = mainConfig; mMenuInput = menuInput; + mAssetLoader = assetLoader; initButtons(); } diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java index d2dedd2..f71183a 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java @@ -7,13 +7,20 @@ import ru.deadsoftware.cavedroid.menu.MenuProc; import ru.deadsoftware.cavedroid.menu.objects.Button; import ru.deadsoftware.cavedroid.menu.objects.ButtonEventListener; import ru.deadsoftware.cavedroid.menu.objects.ButtonRenderer; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; +import javax.inject.Inject; import java.util.HashMap; public class MenuMain extends Menu { - public MenuMain(float width, float height, ButtonRenderer buttonRenderer, MainConfig mainConfig, MenuProc.Input menuInput) { - super(width, height, buttonRenderer, mainConfig, menuInput); + public MenuMain(float width, + float height, + ButtonRenderer buttonRenderer, + MainConfig mainConfig, + MenuProc.Input menuInput, + AssetLoader assetLoader) { + super(width, height, buttonRenderer, mainConfig, menuInput, assetLoader); } @Override @@ -27,9 +34,27 @@ public class MenuMain extends Menu { @Override protected void initButtons() { - loadButtonsFromJson(Gdx.files.internal("json/menu_main_buttons.json")); + loadButtonsFromJson(mAssetLoader.getAssetHandle("json/menu_main_buttons.json")); if (GameSaver.exists(mMainConfig)) { getButtons().get("load_game").setType(Button.NORMAL); } } + + public static class Factory { + + private final MainConfig mMainConfig; + private final AssetLoader mAssetLoader; + + @Inject + public Factory(MainConfig mainConfig, AssetLoader assetLoader) { + mMainConfig = mainConfig; + mAssetLoader = assetLoader; + } + + public MenuMain get(float width, float height, ButtonRenderer buttonRenderer, MenuProc.Input menuInput) { + return new MenuMain(width, height, buttonRenderer, mMainConfig, menuInput, mAssetLoader); + } + + } + } diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java index 2c3b38b..33985ff 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java @@ -1,17 +1,23 @@ package ru.deadsoftware.cavedroid.menu.submenus; -import com.badlogic.gdx.Gdx; import ru.deadsoftware.cavedroid.MainConfig; import ru.deadsoftware.cavedroid.menu.MenuProc; import ru.deadsoftware.cavedroid.menu.objects.ButtonEventListener; import ru.deadsoftware.cavedroid.menu.objects.ButtonRenderer; +import ru.deadsoftware.cavedroid.misc.utils.AssetLoader; +import javax.inject.Inject; import java.util.HashMap; public class MenuNewGame extends Menu { - public MenuNewGame(float width, float height, ButtonRenderer buttonRenderer, MainConfig mainConfig, MenuProc.Input menuInput) { - super(width, height, buttonRenderer, mainConfig, menuInput); + public MenuNewGame(float width, + float height, + ButtonRenderer buttonRenderer, + MainConfig mainConfig, + MenuProc.Input menuInput, + AssetLoader assetLoader) { + super(width, height, buttonRenderer, mainConfig, menuInput, assetLoader); } @Override @@ -25,6 +31,23 @@ public class MenuNewGame extends Menu { @Override protected void initButtons() { - loadButtonsFromJson(Gdx.files.internal("json/menu_new_game_buttons.json")); + loadButtonsFromJson(mAssetLoader.getAssetHandle("json/menu_new_game_buttons.json")); + } + + public static class Factory { + + private final MainConfig mMainConfig; + private final AssetLoader mAssetLoader; + + @Inject + public Factory(MainConfig mainConfig, AssetLoader assetLoader) { + mMainConfig = mainConfig; + mAssetLoader = assetLoader; + } + + public MenuNewGame get(float width, float height, ButtonRenderer buttonRenderer, MenuProc.Input menuInput) { + return new MenuNewGame(width, height, buttonRenderer, mMainConfig, menuInput, mAssetLoader); + } + } } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java index 90a7ec1..7055a73 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java @@ -10,6 +10,7 @@ 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; @@ -39,11 +40,11 @@ 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"))); + assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png"))); sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0); } } @@ -59,10 +60,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 = new Texture(assetLoader.getAssetHandle(file.name() + ".png")); if (file.size == 0) { textureRegions.put(file.name(), flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight())); @@ -78,12 +79,12 @@ public class Assets { } } - public static void load() { - loadMob(playerSprite, "char"); - loadMob(pigSprite, "pig"); - loadJSON(); + public static void load(final AssetLoader assetLoader) { + loadMob(assetLoader, playerSprite, "char"); + loadMob(assetLoader, pigSprite, "pig"); + loadJSON(assetLoader); setPlayerHeadOrigin(); - minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true); + minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true); minecraftFont.getData().setScale(.375f); } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/AssetLoader.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/AssetLoader.kt new file mode 100644 index 0000000..5b89719 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/AssetLoader.kt @@ -0,0 +1,24 @@ +package ru.deadsoftware.cavedroid.misc.utils + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.files.FileHandle +import ru.deadsoftware.cavedroid.MainConfig +import java.io.File +import javax.inject.Inject + +class AssetLoader @Inject constructor( + private val mainConfig: MainConfig, +) { + + fun getAssetHandle(path: String): FileHandle { + val texturePackPath = + mainConfig.assetsPackPath?.let { if (!it.endsWith(File.separator)) "$it${File.separator}" else it } + + return if (texturePackPath == null) { + Gdx.files.internal(path) + } else { + Gdx.files.absolute("$texturePackPath$path") + } + } + +} diff --git a/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java b/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java index c30a617..025beb4 100644 --- a/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java +++ b/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java @@ -14,12 +14,27 @@ class DesktopLauncher { config.useVsync(true); boolean touch = false; + String assetsPath = null; + for (String anArg : arg) { if (anArg.equals("--touch")) { touch = true; - break; } + + if (anArg.startsWith("--assets")) { + String[] splitArg = anArg.split("="); + if (splitArg.length >= 2) { + assetsPath = splitArg[1]; + } + } } - new Lwjgl3Application(new CaveGame(System.getProperty("user.home") + "/.cavedroid", touch), config); + + new Lwjgl3Application( + new CaveGame( + System.getProperty("user.home") + "/.cavedroid", + touch, + assetsPath), + config + ); } } -- 2.29.2