From 5b3244dceab39900a44eeb4d6108715c56735626 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Wed, 6 May 2020 20:29:26 +0700 Subject: [PATCH] Implement DI for menu and refactor #13 --- .../ru/deadsoftware/cavedroid/CaveGame.java | 43 +++- .../deadsoftware/cavedroid/MainComponent.java | 8 +- .../ru/deadsoftware/cavedroid/MainConfig.java | 13 +- .../cavedroid/game/GameComponent.java | 3 +- .../cavedroid/game/GameFluidsThread.java | 4 +- .../cavedroid/game/GameInput.java | 16 +- .../cavedroid/game/GameInputProcessor.java | 4 +- .../cavedroid/game/GameModule.java | 52 +++++ .../cavedroid/game/GamePhysics.java | 29 ++- .../deadsoftware/cavedroid/game/GameProc.java | 8 +- .../cavedroid/game/GameRenderer.java | 9 +- .../cavedroid/game/GameSaver.java | 213 ++++++++++++------ .../cavedroid/game/GameScreen.java | 32 ++- .../cavedroid/game/GameWorld.java | 45 ++-- .../deadsoftware/cavedroid/game/WorldGen.java | 12 +- .../deadsoftware/cavedroid/game/mobs/Mob.java | 10 +- .../cavedroid/game/mobs/MobsController.java | 3 +- .../game/objects/DropController.java | 8 +- .../cavedroid/menu/MenuComponent.java | 10 + .../deadsoftware/cavedroid/menu/MenuProc.java | 93 +++++--- .../cavedroid/menu/MenuScope.java | 10 + .../cavedroid/menu/MenuScreen.java | 57 +++++ .../cavedroid/menu/submenus/Menu.java | 39 ++-- .../cavedroid/menu/submenus/MenuInput.java | 43 ---- .../cavedroid/menu/submenus/MenuMain.java | 14 +- .../cavedroid/menu/submenus/MenuNewGame.java | 12 +- .../cavedroid/misc/states/AppState.java | 8 - .../cavedroid/misc/states/GameState.java | 7 - .../cavedroid/misc/states/MenuState.java | 8 - .../cavedroid/misc/states/package-info.java | 4 - 30 files changed, 540 insertions(+), 277 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/GameModule.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/MenuComponent.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/MenuScope.java create mode 100644 core/src/ru/deadsoftware/cavedroid/menu/MenuScreen.java delete mode 100644 core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java delete mode 100644 core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java delete mode 100644 core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java delete mode 100644 core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java delete mode 100644 core/src/ru/deadsoftware/cavedroid/misc/states/package-info.java diff --git a/core/src/ru/deadsoftware/cavedroid/CaveGame.java b/core/src/ru/deadsoftware/cavedroid/CaveGame.java index f1fb3ee..031b613 100644 --- a/core/src/ru/deadsoftware/cavedroid/CaveGame.java +++ b/core/src/ru/deadsoftware/cavedroid/CaveGame.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import ru.deadsoftware.cavedroid.game.GameItems; +import ru.deadsoftware.cavedroid.game.GameScreen; import ru.deadsoftware.cavedroid.misc.Assets; public class CaveGame extends Game { @@ -11,6 +12,9 @@ public class CaveGame extends Game { public static final String VERSION = "alpha 0.4"; + private final MainConfig mMainConfig; + private final MainComponent mMainComponent; + private final String mGameFolder; private final boolean mTouch; private boolean mDebug; @@ -18,22 +22,41 @@ public class CaveGame extends Game { public CaveGame(String gameFolder, boolean touch) { mGameFolder = gameFolder; mTouch = touch; + + mMainComponent = DaggerMainComponent.builder().caveGame(this).build(); + mMainConfig = mMainComponent.getMainConfig(); } public void setDebug(boolean debug) { mDebug = debug; } - private void initConfig(MainConfig mainConfig, MainComponent mainComponent) { + private void initConfig() { int width = mTouch ? 320 : 480; int height = (int) (width * ((float) Gdx.graphics.getHeight() / Gdx.graphics.getWidth())); - mainConfig.setMainComponent(mainComponent); - mainConfig.setGameFolder(mGameFolder); - mainConfig.setTouch(mTouch); - mainConfig.setWidth(width); - mainConfig.setHeight(height); - mainConfig.setShowInfo(true); + mMainConfig.setMainComponent(mMainComponent); + mMainConfig.setGameFolder(mGameFolder); + mMainConfig.setTouch(mTouch); + mMainConfig.setWidth(width); + mMainConfig.setHeight(height); + mMainConfig.setShowInfo(mDebug); + } + + public void newGame() { + GameScreen gameScreen = mMainComponent.getGameScreen(); + gameScreen.newGame(); + setScreen(gameScreen); + } + + public void loadGame() { + GameScreen gameScreen = mMainComponent.getGameScreen(); + gameScreen.loadGame(); + setScreen(gameScreen); + } + + public void quitGame() { + setScreen(mMainComponent.getMenuScreen()); } @Override @@ -44,9 +67,9 @@ public class CaveGame extends Game { Assets.load(); GameItems.load(); - MainComponent mainComponent = DaggerMainComponent.create(); - initConfig(mainComponent.getGameConfig(), mainComponent); - setScreen(mainComponent.getGameScreen()); + initConfig(); + + setScreen(mMainComponent.getMenuScreen()); } } diff --git a/core/src/ru/deadsoftware/cavedroid/MainComponent.java b/core/src/ru/deadsoftware/cavedroid/MainComponent.java index 74bb191..d8a05da 100644 --- a/core/src/ru/deadsoftware/cavedroid/MainComponent.java +++ b/core/src/ru/deadsoftware/cavedroid/MainComponent.java @@ -2,12 +2,16 @@ package ru.deadsoftware.cavedroid; import dagger.Component; import ru.deadsoftware.cavedroid.game.GameScreen; +import ru.deadsoftware.cavedroid.menu.MenuScreen; import javax.inject.Singleton; @Singleton -@Component +@Component(dependencies = CaveGame.class) public interface MainComponent { GameScreen getGameScreen(); - MainConfig getGameConfig(); + + MenuScreen getMenuScreen(); + + MainConfig getMainConfig(); } diff --git a/core/src/ru/deadsoftware/cavedroid/MainConfig.java b/core/src/ru/deadsoftware/cavedroid/MainConfig.java index b48b78d..8cd0ffd 100644 --- a/core/src/ru/deadsoftware/cavedroid/MainConfig.java +++ b/core/src/ru/deadsoftware/cavedroid/MainConfig.java @@ -2,12 +2,16 @@ package ru.deadsoftware.cavedroid; import ru.deadsoftware.cavedroid.game.GameUiWindow; +import javax.annotation.CheckForNull; import javax.inject.Inject; import javax.inject.Singleton; @Singleton public class MainConfig { + private final CaveGame mCaveGame; + + @CheckForNull private MainComponent mMainComponent; private GameUiWindow mGameUiWindow; @@ -21,12 +25,19 @@ public class MainConfig { private float mHeight; @Inject - public MainConfig() { + public MainConfig(CaveGame caveGame) { + mCaveGame = caveGame; + mGameUiWindow = GameUiWindow.NONE; mGameFolder = ""; } + public CaveGame getCaveGame() { + return mCaveGame; + } + public MainComponent getMainComponent() { + assert mMainComponent != null; return mMainComponent; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java b/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java index 320f4f6..25779f0 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java @@ -4,8 +4,9 @@ import dagger.Component; import ru.deadsoftware.cavedroid.MainComponent; @GameScope -@Component(dependencies = MainComponent.class) +@Component(dependencies = MainComponent.class, modules = GameModule.class) public interface GameComponent { GameProc getGameProc(); + GameInputProcessor getGameInputProcessor(); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java index 20e95b8..c3cdd47 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java @@ -16,12 +16,12 @@ class GameFluidsThread extends Thread { private static final int[] LAVA_IDS = {9, 64, 65, 66, 67}; private long mFluidLastUpdateTimestamp = 0; - + private final GameWorld mGameWorld; private final MobsController mMobsController; private final Thread mMainThread; - + GameFluidsThread(GameWorld gameWorld, MobsController mobsController, Thread mainThread) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 3a5e3c4..ca60317 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -10,6 +10,7 @@ import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Pig; import ru.deadsoftware.cavedroid.game.mobs.Player; +import ru.deadsoftware.cavedroid.game.objects.DropController; import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.ControlMode; @@ -22,6 +23,7 @@ public class GameInput { private final MainConfig mMainConfig; private final GameWorld mGameWorld; + private final DropController mDropController; private final MobsController mMobsController; private final Player mPlayer; @@ -45,9 +47,11 @@ public class GameInput { @Inject public GameInput(MainConfig mainConfig, GameWorld gameWorld, + DropController dropController, MobsController mobsController) { mMainConfig = mainConfig; mGameWorld = gameWorld; + mDropController = dropController; mMobsController = mobsController; mPlayer = mMobsController.getPlayer(); @@ -243,7 +247,7 @@ public class GameInput { mTouchedDown = false; } else { if (insideHotbar(mTouchDownX, mTouchDownY)) { -// CaveGame.GAME_STATE = GameState.CREATIVE_INV; + mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY); mTouchedDown = false; } } @@ -291,11 +295,11 @@ public class GameInput { mGameWorld.placeToForeground(mCurX, mCurY, 8); break; -// case Input.Keys.ESCAPE: -// case Input.Keys.BACK: -// CaveGame.APP_STATE = AppState.SAVE; -// CaveGame.GAME_STATE = GameState.PAUSE; -// break; + case Input.Keys.ESCAPE: + case Input.Keys.BACK: + GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld); + mMainConfig.getCaveGame().quitGame(); + break; case Input.Keys.F1: mMainConfig.setShowInfo(!mMainConfig.isShowInfo()); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java b/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java index faafd63..51cc897 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java @@ -21,7 +21,7 @@ public class GameInputProcessor extends InputAdapter { private final GameInput mGameInput; private final GameRenderer mGameRenderer; private final MainConfig mMainConfig; - + @Inject public GameInputProcessor(GameInput gameInput, GameRenderer gameRenderer, @@ -29,7 +29,7 @@ public class GameInputProcessor extends InputAdapter { mGameInput = gameInput; mGameRenderer = gameRenderer; mMainConfig = mainConfig; - + loadTouchButtonsFromJSON(); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameModule.java b/core/src/ru/deadsoftware/cavedroid/game/GameModule.java new file mode 100644 index 0000000..d7ccc2d --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/GameModule.java @@ -0,0 +1,52 @@ +package ru.deadsoftware.cavedroid.game; + +import dagger.Module; +import dagger.Provides; +import ru.deadsoftware.cavedroid.MainConfig; +import ru.deadsoftware.cavedroid.game.mobs.MobsController; +import ru.deadsoftware.cavedroid.game.objects.DropController; + +import javax.annotation.CheckForNull; + +@Module +public class GameModule { + + @CheckForNull + private static GameSaver.Data data; + + public static void load(MainConfig mainConfig) { + data = GameSaver.load(mainConfig); + } + + private static void makeDataNullIfEmpty() { + if (data != null && data.isEmpty()) { + data = null; + } + } + + @Provides + @GameScope + public static DropController provideDropController() { + DropController controller = data != null ? data.retrieveDropController() : new DropController(); + makeDataNullIfEmpty(); + return controller; + } + + @Provides + @GameScope + public static MobsController provideMobsController() { + MobsController controller = data != null ? data.retrieveMobsController() : new MobsController(); + makeDataNullIfEmpty(); + return controller; + } + + @Provides + @GameScope + public static GameWorld provideGameWorld(DropController dropController, MobsController mobsController) { + int[][] fm = data != null ? data.retrieveForeMap() : null; + int[][] bm = data != null ? data.retrieveBackMap() : null; + makeDataNullIfEmpty(); + return new GameWorld(dropController, mobsController, fm, bm); + } + +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 6695d6e..d6b5a9a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -9,6 +9,7 @@ import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Player; import ru.deadsoftware.cavedroid.game.objects.Drop; +import ru.deadsoftware.cavedroid.game.objects.DropController; import javax.inject.Inject; import java.util.Iterator; @@ -24,14 +25,17 @@ class GamePhysics { private final GameWorld mGameWorld; private final MainConfig mMainConfig; private final MobsController mMobsController; + private final DropController mDropController; @Inject public GamePhysics(GameWorld gameWorld, MainConfig mainConfig, - MobsController mobsController) { + MobsController mobsController, + DropController dropController) { mGameWorld = gameWorld; mMainConfig = mainConfig; mMobsController = mobsController; + mDropController = dropController; } /** @@ -253,16 +257,18 @@ class GamePhysics { } void update() { -// for (Iterator it = GP.drops.iterator(); it.hasNext(); ) { -// Drop drop = it.next(); -// dropPhy(drop); -// if (Intersector.overlaps(drop, GP.player)) { -// drop.pickUpDrop(GP.player); -// } -// if (drop.isPickedUp()) { -// it.remove(); -// } -// } + Player player = mMobsController.getPlayer(); + + for (Iterator it = mDropController.getIterator(); it.hasNext(); ) { + Drop drop = it.next(); + dropPhy(drop); + if (Intersector.overlaps(drop, player)) { + drop.pickUpDrop(player); + } + if (drop.isPickedUp()) { + it.remove(); + } + } for (Iterator it = mMobsController.getIterator(); it.hasNext(); ) { Mob mob = it.next(); @@ -273,7 +279,6 @@ class GamePhysics { } } - Player player = mMobsController.getPlayer(); playerPhy(player); if (player.isDead()) { player.respawn(mGameWorld); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index ef08211..44a4687 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -1,9 +1,11 @@ package ru.deadsoftware.cavedroid.game; +import com.badlogic.gdx.utils.Disposable; + import javax.inject.Inject; @GameScope -public class GameProc { +public class GameProc implements Disposable { public static final int MAX_CREATIVE_SCROLL = GameItems.getItemsSize() / 8; @@ -32,4 +34,8 @@ public class GameProc { mGameRenderer.render(delta); } + @Override + public void dispose() { + mGameWorld.dispose(); + } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 018fe1b..864938d 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -23,8 +23,6 @@ import static ru.deadsoftware.cavedroid.misc.Assets.textureRegions; @GameScope public class GameRenderer extends Renderer { - private static final String TAG = "GameRenderer"; - private final MainConfig mMainConfig; private final GameInput mGameInput; private final GameWorld mGameWorld; @@ -286,16 +284,17 @@ public class GameRenderer extends Renderer { if (mMainConfig.isShowInfo()) { spriter.begin(); + Player player = mMobsController.getPlayer(); drawString("FPS: " + fps, 0, 0); - drawString("X: " + mMobsController.getPlayer().getMapX(), 0, 10); - drawString("Y: " + mMobsController.getPlayer().getUpperMapY(), 0, 20); + drawString("X: " + player.getMapX(), 0, 10); + drawString("Y: " + player.getUpperMapY(), 0, 20); drawString("CurX: " + mGameInput.getCurX(), 0, 30); drawString("CurY: " + mGameInput.getCurY(), 0, 40); drawString("Mobs: " + mMobsController.getSize(), 0, 50); drawString("Drops: " + mDropController.getSize(), 0, 60); drawString("Block: " + GameItems.getBlockKey(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY())), 0, 70); drawString("Hand: " + GameItems.getItemKey(mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot]), 0, 80); - drawString("Game mode: " + mMobsController.getPlayer().gameMode, 0, 90); + drawString("Game mode: " + player.gameMode, 0, 90); spriter.end(); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java index 26281f6..1ff2bad 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java @@ -1,123 +1,194 @@ package ru.deadsoftware.cavedroid.game; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; +import ru.deadsoftware.cavedroid.MainConfig; +import ru.deadsoftware.cavedroid.game.mobs.MobsController; +import ru.deadsoftware.cavedroid.game.objects.DropController; -import java.io.BufferedOutputStream; -import java.io.DataInputStream; -import java.io.IOException; +import javax.annotation.CheckForNull; +import java.io.*; import java.nio.ByteBuffer; -//TODO rewrite saver public class GameSaver { - private static final int VERSION = 0; + public static class Data { + @CheckForNull + private MobsController mMobsController; + @CheckForNull + private DropController mDropController; + @CheckForNull + private int[][] mForeMap, mBackMap; + public Data(MobsController mobsController, DropController dropController, int[][] foreMap, int[][] backMap) { + mMobsController = mobsController; + mDropController = dropController; + mForeMap = foreMap; + mBackMap = backMap; + } + + public MobsController retrieveMobsController() { + assert mMobsController != null; + MobsController mobsController = mMobsController; + mMobsController = null; + return mobsController; + } + + public DropController retrieveDropController() { + assert mDropController != null; + DropController dropController = mDropController; + mDropController = null; + return dropController; + } + + public int[][] retrieveForeMap() { + assert mForeMap != null; + int[][] foreMap = mForeMap; + mForeMap = null; + return foreMap; + } + + public int[][] retrieveBackMap() { + assert mBackMap != null; + int[][] backMap = mBackMap; + mBackMap = null; + return backMap; + } + + public boolean isEmpty() { + return mMobsController == null && mDropController == null && mForeMap == null && mBackMap == null; + } + } + + private static final int SAVE_VERSION = 1; private static byte[] intToBytes(int i) { return ByteBuffer.allocate(4).putInt(i).array(); } private static void saveMap(FileHandle file, int[][] map) throws IOException { - int rl, bl; + int run, block; int width = map.length; int height = map[0].length; + BufferedOutputStream out = new BufferedOutputStream(file.write(false)); - out.write(intToBytes(VERSION)); + + out.write(intToBytes(SAVE_VERSION)); out.write(intToBytes(width)); out.write(intToBytes(height)); + for (int y = 0; y < height; y++) { - bl = map[0][y]; - rl = 0; + block = map[0][y]; + run = 0; for (int[] ints : map) { - if (ints[y] != bl) { - out.write(intToBytes(rl)); - out.write(intToBytes(bl)); - rl = 0; - bl = ints[y]; + if (ints[y] != block) { + out.write(intToBytes(run)); + out.write(intToBytes(block)); + run = 0; + block = ints[y]; } - rl++; + run++; } - out.write(intToBytes(rl)); - out.write(intToBytes(bl)); + out.write(intToBytes(run)); + out.write(intToBytes(block)); } + out.flush(); out.close(); } - private static int[][] loadMap(FileHandle file) throws Exception { int[][] map; - int ver, width, height; - int rl, bl; + int version, width, height; + int run, block; + DataInputStream in = new DataInputStream(file.read()); - ver = in.readInt(); - if (VERSION == ver) { + + version = in.readInt(); + + if (SAVE_VERSION == version) { width = in.readInt(); height = in.readInt(); map = new int[width][height]; for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x += rl) { - rl = in.readInt(); - bl = in.readInt(); - for (int i = x; i < x + rl; i++) { - map[i][y] = bl; + for (int x = 0; x < width; x += run) { + run = in.readInt(); + block = in.readInt(); + for (int i = x; i < x + run; i++) { + map[i][y] = block; } } } - in.close(); } else { throw new Exception("version mismatch"); } + + in.close(); return map; } - public static GameProc load() { -// FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav"); -// GameProc gameProc = null; -// try { -// ObjectInputStream in = new ObjectInputStream(file.read()); -// int ver = in.readInt(); -// if (VERSION == ver) { -// gameProc = (GameProc) in.readObject(); -// } else { -// throw new Exception("version mismatch"); -// } -// in.close(); -// gameProc.world = new GameWorld( -// loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")), -// loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav")) -// ); -// gameProc.physics = new GamePhysics(); -// gameProc.input = new GameInput(); -// } catch (Exception e) { -// Gdx.app.error("GameSaver", e.getMessage(), e); -// Gdx.app.exit(); -// } -// return gameProc; + @CheckForNull + public static Data load(MainConfig mainConfig) { + String folder = mainConfig.getGameFolder(); + FileHandle file = Gdx.files.absolute(folder + "/saves/game.sav"); + + try { + ObjectInputStream in = new ObjectInputStream(file.read()); + int version = in.readInt(); + DropController dropController; + MobsController mobsController; + + if (SAVE_VERSION == version) { + dropController = (DropController) in.readObject(); + mobsController = (MobsController) in.readObject(); + } else { + throw new Exception("version mismatch"); + } + + in.close(); + + int[][] foreMap = loadMap(Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/foremap.sav")); + int[][] backMap = loadMap(Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/backmap.sav")); + + if (dropController == null || mobsController == null) { + throw new Exception("couldn't load"); + } + + return new Data(mobsController, dropController, foreMap, backMap); + } catch (Exception e) { + Gdx.app.error("GameSaver", e.getMessage()); + } + return null; } - public static void save(GameProc gp) { -// FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/"); -// file.mkdirs(); -// file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav"); -// try { -// ObjectOutputStream out = new ObjectOutputStream(file.write(false)); -// out.writeInt(VERSION); -// out.writeObject(gp); -// out.close(); -// saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap()); -// saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap()); -// } catch (Exception e) { -// e.printStackTrace(); -// } - } + public static void save(MainConfig mainConfig, + DropController dropController, + MobsController mobsController, + GameWorld gameWorld) { + + String folder = mainConfig.getGameFolder(); + FileHandle file = Gdx.files.absolute(folder + "/saves/"); + file.mkdirs(); + file = Gdx.files.absolute(folder + "/saves/game.sav"); - public static boolean exists() { -// return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() && -// Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() && -// Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists()); - return false; + try { + ObjectOutputStream out = new ObjectOutputStream(file.write(false)); + out.writeInt(SAVE_VERSION); + out.writeObject(dropController); + out.writeObject(mobsController); + out.close(); + saveMap(Gdx.files.absolute(folder + "/saves/foremap.sav"), gameWorld.getFullForeMap()); + saveMap(Gdx.files.absolute(folder + "/saves/backmap.sav"), gameWorld.getFullBackMap()); + } catch (Exception e) { + e.printStackTrace(); + } } + public static boolean exists(MainConfig mainConfig) { + String folder = mainConfig.getGameFolder(); + return (Gdx.files.absolute(folder + "/saves/game.sav").exists() && + Gdx.files.absolute(folder + "/saves/foremap.sav").exists() && + Gdx.files.absolute(folder + "/saves/backmap.sav").exists()); + } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameScreen.java b/core/src/ru/deadsoftware/cavedroid/game/GameScreen.java index 00cfd3e..370d8bd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameScreen.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameScreen.java @@ -4,28 +4,52 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import ru.deadsoftware.cavedroid.MainConfig; +import javax.annotation.CheckForNull; import javax.inject.Inject; import javax.inject.Singleton; @Singleton public class GameScreen implements Screen { - private static final String TAG = "GameScreen"; - private final MainConfig mMainConfig; + @CheckForNull private GameProc mGameProc; + @CheckForNull + private GameInputProcessor mGameInputProcessor; @Inject public GameScreen(MainConfig mainConfig) { mMainConfig = mainConfig; - newGame(); } public void newGame() { + if (mGameProc != null) { + mGameProc.dispose(); + } + GameComponent gameComponent = DaggerGameComponent.builder() .mainComponent(mMainConfig.getMainComponent()).build(); + mGameProc = gameComponent.getGameProc(); + mGameInputProcessor = gameComponent.getGameInputProcessor(); + + Gdx.input.setInputProcessor(gameComponent.getGameInputProcessor()); + } + + public void loadGame() { + if (mGameProc != null) { + mGameProc.dispose(); + } + + GameModule.load(mMainConfig); + + GameComponent gameComponent = DaggerGameComponent.builder() + .mainComponent(mMainConfig.getMainComponent()).build(); + + mGameProc = gameComponent.getGameProc(); + mGameInputProcessor = gameComponent.getGameInputProcessor(); + Gdx.input.setInputProcessor(gameComponent.getGameInputProcessor()); } @@ -36,7 +60,7 @@ public class GameScreen implements Screen { @Override public void show() { - + Gdx.input.setInputProcessor(mGameInputProcessor); } @Override diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java index 5aecce2..cbf970c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java @@ -1,15 +1,17 @@ package ru.deadsoftware.cavedroid.game; +import com.badlogic.gdx.utils.Disposable; import ru.deadsoftware.cavedroid.game.mobs.FallingGravel; import ru.deadsoftware.cavedroid.game.mobs.FallingSand; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.objects.Block; import ru.deadsoftware.cavedroid.game.objects.DropController; +import javax.annotation.CheckForNull; import javax.inject.Inject; @GameScope -public class GameWorld { +public class GameWorld implements Disposable { private static final int DEFAULT_WIDTH = 1024; private static final int DEFAULT_HEIGHT = 256; @@ -29,28 +31,33 @@ public class GameWorld { private int mUpdateY; @Inject - GameWorld(DropController dropController, - MobsController mobsController) { + public GameWorld(DropController dropController, + MobsController mobsController, + @CheckForNull int[][] foreMap, + @CheckForNull int[][] backMap) { mDropController = dropController; mMobsController = mobsController; - mWidth = DEFAULT_WIDTH; - mHeight = DEFAULT_HEIGHT; - WorldGen.genWorld(mWidth, mHeight); - mForeMap = WorldGen.getForeMap(); - mBackMap = WorldGen.getBackMap(); - WorldGen.clear(); + boolean isNewGame = foreMap == null || backMap == null; + + if (isNewGame) { + mWidth = DEFAULT_WIDTH; + mHeight = DEFAULT_HEIGHT; + WorldGen.genWorld(mWidth, mHeight); + mForeMap = WorldGen.getForeMap(); + mBackMap = WorldGen.getBackMap(); + WorldGen.clear(); + mMobsController.getPlayer().respawn(this); + } else { + mForeMap = foreMap; + mBackMap = backMap; + mWidth = mForeMap.length; + mHeight = mForeMap[0].length; + } mGameFluidsThread = new GameFluidsThread(this, mMobsController, Thread.currentThread()); } -// GameWorld(int[][] foreMap, int[][] backMap) { -// this.foreMap = foreMap.clone(); -// this.backMap = backMap.clone(); -// WIDTH = foreMap.length; -// HEIGHT = foreMap[0].length; -// } - public int getWidth() { return mWidth; } @@ -200,7 +207,7 @@ public class GameWorld { setForeMap(x, y, 0); mMobsController.addMob(FallingSand.class, x * 16, y * 16); updateBlock(x, y - 1); - } + } } if (getForeMap(x, y) == 11) { @@ -245,4 +252,8 @@ public class GameWorld { mGameFluidsThread.start(); } + @Override + public void dispose() { + mGameFluidsThread.interrupt(); + } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/WorldGen.java b/core/src/ru/deadsoftware/cavedroid/game/WorldGen.java index 4c9b9b2..cc02aea 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/WorldGen.java +++ b/core/src/ru/deadsoftware/cavedroid/game/WorldGen.java @@ -3,13 +3,19 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.math.RandomXS128; import com.badlogic.gdx.utils.TimeUtils; +import javax.annotation.CheckForNull; + class WorldGen { + @CheckForNull private static RandomXS128 rand; private static long seed; + @CheckForNull private static int[][] foreMap, backMap; + @CheckForNull private static int[] hMap; + @CheckForNull private static int[] bMap; //biomes, 0-plains, 1-desert public static long getSeed() { @@ -57,12 +63,14 @@ class WorldGen { } private static void genCactus(int x, int y) { + assert foreMap != null; foreMap[x][y] = 59; foreMap[x][y - 1] = 59; foreMap[x][y - 2] = 59; } private static void genOak(int x, int y) { + assert foreMap != null && backMap != null; backMap[x][y] = 15; backMap[x][y - 1] = 15; backMap[x][y - 2] = 15; @@ -183,16 +191,16 @@ class WorldGen { } static int[][] getForeMap() { + assert foreMap != null; return foreMap; } static int[][] getBackMap() { + assert backMap != null; return backMap; } static void clear() { - foreMap = null; - backMap = null; hMap = null; bMap = null; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 91dfae5..0f76592 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -35,11 +35,11 @@ public abstract class Mob extends Rectangle implements Serializable { private boolean mFlyMode; /** - * @param x in pixels - * @param y in pixels - * @param width in pixels - * @param height in pixels - * @param mDirection Direction in which mob is looking + * @param x in pixels + * @param y in pixels + * @param width in pixels + * @param height in pixels + * @param mDirection Direction in which mob is looking */ protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) { super(x, y, width, height); diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java index 915ce25..cda54ee 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java @@ -4,11 +4,12 @@ import com.badlogic.gdx.Gdx; import ru.deadsoftware.cavedroid.game.GameScope; import javax.inject.Inject; +import java.io.Serializable; import java.util.Iterator; import java.util.LinkedList; @GameScope -public class MobsController { +public class MobsController implements Serializable { public interface Callback { void run(Mob mob); diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java b/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java index c71a37e..0e4c659 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java @@ -3,10 +3,12 @@ package ru.deadsoftware.cavedroid.game.objects; import ru.deadsoftware.cavedroid.game.GameScope; import javax.inject.Inject; +import java.io.Serializable; +import java.util.Iterator; import java.util.LinkedList; @GameScope -public class DropController { +public class DropController implements Serializable { public interface Callback { void run(Drop drop); @@ -30,4 +32,8 @@ public class DropController { mDrops.forEach(callback::run); } + public Iterator getIterator() { + return mDrops.iterator(); + } + } diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuComponent.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuComponent.java new file mode 100644 index 0000000..5674368 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuComponent.java @@ -0,0 +1,10 @@ +package ru.deadsoftware.cavedroid.menu; + +import dagger.Component; +import ru.deadsoftware.cavedroid.MainComponent; + +@MenuScope +@Component(dependencies = MainComponent.class) +public interface MenuComponent { + MenuProc getMenuProc(); +} diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java index 8bc9dd6..f96d4d4 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java @@ -3,30 +3,69 @@ package ru.deadsoftware.cavedroid.menu; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.utils.ObjectMap; import ru.deadsoftware.cavedroid.CaveGame; -import ru.deadsoftware.cavedroid.game.GameScreen; +import ru.deadsoftware.cavedroid.MainConfig; import ru.deadsoftware.cavedroid.menu.objects.Button; import ru.deadsoftware.cavedroid.menu.submenus.Menu; import ru.deadsoftware.cavedroid.menu.submenus.MenuMain; import ru.deadsoftware.cavedroid.menu.submenus.MenuNewGame; import ru.deadsoftware.cavedroid.misc.Renderer; -import ru.deadsoftware.cavedroid.misc.states.AppState; -import ru.deadsoftware.cavedroid.misc.states.GameState; -import ru.deadsoftware.cavedroid.misc.states.MenuState; + +import javax.inject.Inject; import static ru.deadsoftware.cavedroid.misc.Assets.*; +@MenuScope public class MenuProc extends Renderer { - private MenuMain mainMenu; - private MenuNewGame newGameMenu; + public class Input { + private void startNewGame(int gameMode) { + mMainConfig.getCaveGame().newGame(); + } + + public void newGameClicked() { + mCurrentMenu = mMenuNewGame; + } - private Menu currentMenu; + public void loadGameClicked() { + mMainConfig.getCaveGame().loadGame(); + } + + public void quitClicked() { + Gdx.app.exit(); + } - public MenuProc(int width) { - super(width, width * Gdx.graphics.getHeight() / Gdx.graphics.getWidth()); - mainMenu = new MenuMain(getWidth(), getHeight(), this::drawButton); - newGameMenu = new MenuNewGame(getWidth(), getHeight(), this::drawButton); - currentMenu = mainMenu; + public void survivalClicked() { + startNewGame(0); + } + + public void creativeClicked() { + startNewGame(1); + } + + public void backClicked() { + mCurrentMenu = mMenuMain; + } + } + + private final MainConfig mMainConfig; + + private final MenuMain mMenuMain; + private final MenuNewGame mMenuNewGame; + + private Menu mCurrentMenu; + + @Inject + public MenuProc(MainConfig mainConfig) { + 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); + + mCurrentMenu = mMenuMain; } private void drawButton(Button button) { @@ -37,32 +76,11 @@ public class MenuProc extends Renderer { (button.getY() + button.getHeight() / 2) - (float) getStringHeight(button.getLabel()) / 2); } - private void update() { -// switch (CaveGame.MENU_STATE) { -// case MAIN: -// currentMenu = mainMenu; -// break; -// case NEW_GAME: -// currentMenu = newGameMenu; -// break; -// case LOADING: -// drawString("Generating World..."); -// CaveGame.APP_STATE = AppState.GAME; -// CaveGame.GAME_STATE = GameState.PLAY; -// break; -// case SAVING: -// drawString("Saving Game..."); -// CaveGame.APP_STATE = AppState.MENU; -// CaveGame.MENU_STATE = MenuState.MAIN; -// break; -// } - } - @Override public boolean touchUp(int screenX, int screenY, int pointer, int mb) { screenX *= getWidth() / Gdx.graphics.getWidth(); screenY *= getHeight() / Gdx.graphics.getHeight(); - for (ObjectMap.Entry entry : currentMenu.getButtons()) { + for (ObjectMap.Entry entry : mCurrentMenu.getButtons()) { Button button = entry.value; if (button.getRect().contains(screenX, screenY)) { if (button.getType() > 0) { @@ -76,11 +94,14 @@ public class MenuProc extends Renderer { @Override public void render(float delta) { - update(); spriter.begin(); - currentMenu.draw(spriter); + mCurrentMenu.draw(spriter); drawString("CaveDroid " + CaveGame.VERSION, 0, getHeight() - getStringHeight("CaveDroid " + CaveGame.VERSION) * 1.5f); spriter.end(); } + + public void reset() { + mCurrentMenu = mMenuMain; + } } diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuScope.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuScope.java new file mode 100644 index 0000000..3198af6 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuScope.java @@ -0,0 +1,10 @@ +package ru.deadsoftware.cavedroid.menu; + +import javax.inject.Scope; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Scope +@Retention(RetentionPolicy.RUNTIME) +public @interface MenuScope { +} diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuScreen.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuScreen.java new file mode 100644 index 0000000..4b9c5e3 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuScreen.java @@ -0,0 +1,57 @@ +package ru.deadsoftware.cavedroid.menu; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Screen; +import ru.deadsoftware.cavedroid.MainConfig; + +import javax.inject.Inject; +import javax.inject.Singleton; + +@Singleton +public class MenuScreen implements Screen { + + private final MenuProc mMenuProc; + + @Inject + public MenuScreen(MainConfig mainConfig) { + MenuComponent menuComponent = DaggerMenuComponent.builder() + .mainComponent(mainConfig.getMainComponent()).build(); + mMenuProc = menuComponent.getMenuProc(); + } + + @Override + public void show() { + mMenuProc.reset(); + Gdx.input.setInputProcessor(mMenuProc); + } + + @Override + public void render(float delta) { + mMenuProc.render(delta); + } + + @Override + public void resize(int width, int height) { + + } + + @Override + public void pause() { + + } + + @Override + public void resume() { + + } + + @Override + public void hide() { + + } + + @Override + public void dispose() { + + } +} diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java index 2a89e04..9f8df4c 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java @@ -6,8 +6,8 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.utils.ArrayMap; import com.badlogic.gdx.utils.JsonValue; -import ru.deadsoftware.cavedroid.CaveGame; -import ru.deadsoftware.cavedroid.game.GameScreen; +import ru.deadsoftware.cavedroid.MainConfig; +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; @@ -17,10 +17,13 @@ import java.util.HashMap; public abstract class Menu { - private ButtonRenderer buttonRenderer; + protected final MainConfig mMainConfig; + protected final MenuProc.Input mMenuInput; - private float width; - private float height; + private final ButtonRenderer mButtonRenderer; + + private final float mWidth; + private final float mHeight; /** * {@link ArrayMap} of {@link Button Buttons} of this menu screen @@ -32,10 +35,12 @@ 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) { - this.width = width; - this.height = height; - this.buttonRenderer = buttonRenderer; + Menu(float width, float height, ButtonRenderer buttonRenderer, MainConfig mainConfig, MenuProc.Input menuInput) { + mWidth = width; + mHeight = height; + mButtonRenderer = buttonRenderer; + mMainConfig = mainConfig; + mMenuInput = menuInput; initButtons(); } @@ -61,11 +66,11 @@ public abstract class Menu { } HashMap eventListeners = getButtonEventListeners(); JsonValue json = Assets.jsonReader.parse(jsonFile); - int y = (int) height / 4; + int y = (int) mHeight / 4; for (JsonValue key = json.child(); key != null; key = key.next(), y += Button.HEIGHT + 10) { buttons.put(key.name(), new Button(Assets.getStringFromJson(key, "label", ""), - (int) width / 2 - Button.WIDTH / 2, + (int) mWidth / 2 - Button.WIDTH / 2, Assets.getIntFromJson(key, "y", y), Assets.getIntFromJson(key, "type", Button.NORMAL), eventListeners.containsKey(key.name()) ? eventListeners.get(key.name()) : () -> { @@ -82,15 +87,15 @@ public abstract class Menu { TextureRegion background = Assets.textureRegions.get("background"); TextureRegion gamelogo = Assets.textureRegions.get("gamelogo"); - for (int x = 0; x <= width / 16; x++) { - for (int y = 0; y <= height / 16; y++) { + for (int x = 0; x <= mWidth / 16; x++) { + for (int y = 0; y <= mHeight / 16; y++) { spriter.draw(background, x * 16, y * 16); } } - spriter.draw(gamelogo, width / 2 - (float) gamelogo.getRegionWidth() / 2, 8); + spriter.draw(gamelogo, mWidth / 2 - (float) gamelogo.getRegionWidth() / 2, 8); - float inputX = Gdx.input.getX() * width / Gdx.graphics.getWidth(); - float inputY = Gdx.input.getY() * height / Gdx.graphics.getHeight(); + float inputX = Gdx.input.getX() * mWidth / Gdx.graphics.getWidth(); + float inputY = Gdx.input.getY() * mHeight / Gdx.graphics.getHeight(); for (Button button : buttons.values()) { if (button.getType() > 0) { if (button.getRect().contains(inputX, inputY) && (/*!CaveGame.TOUCH || */Gdx.input.isTouched())) { @@ -99,7 +104,7 @@ public abstract class Menu { button.setType(1); } } - button.draw(buttonRenderer); + button.draw(mButtonRenderer); } } diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java deleted file mode 100644 index eccf9af..0000000 --- a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java +++ /dev/null @@ -1,43 +0,0 @@ -package ru.deadsoftware.cavedroid.menu.submenus; - -import com.badlogic.gdx.Gdx; -import ru.deadsoftware.cavedroid.CaveGame; -import ru.deadsoftware.cavedroid.game.GameSaver; -import ru.deadsoftware.cavedroid.misc.states.AppState; -import ru.deadsoftware.cavedroid.misc.states.MenuState; - -class MenuInput { - - private static void startNewGame(int gameMode) { -// GP = new GameProc(); -// GP.player.respawn(); -// GameSaver.save(GP); -// CaveGame.APP_STATE = AppState.LOAD; - } - - static void newGameClicked() { -// CaveGame.MENU_STATE = MenuState.NEW_GAME; - } - - static void loadGameClicked() { -// CaveGame.APP_STATE = AppState.LOAD; -// GP = GameSaver.load(); - } - - static void quitClicked() { - Gdx.app.exit(); - } - - static void survivalClicked() { - startNewGame(0); - } - - static void creativeClicked() { - startNewGame(1); - } - - static void backClicked() { -// CaveGame.MENU_STATE = MenuState.MAIN; - } - -} diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java index 10db012..d2dedd2 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuMain.java @@ -1,7 +1,9 @@ package ru.deadsoftware.cavedroid.menu.submenus; import com.badlogic.gdx.Gdx; +import ru.deadsoftware.cavedroid.MainConfig; import ru.deadsoftware.cavedroid.game.GameSaver; +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; @@ -10,23 +12,23 @@ import java.util.HashMap; public class MenuMain extends Menu { - public MenuMain(float width, float height, ButtonRenderer buttonRenderer) { - super(width, height, buttonRenderer); + public MenuMain(float width, float height, ButtonRenderer buttonRenderer, MainConfig mainConfig, MenuProc.Input menuInput) { + super(width, height, buttonRenderer, mainConfig, menuInput); } @Override protected HashMap getButtonEventListeners() { HashMap map = new HashMap<>(); - map.put("new_game", MenuInput::newGameClicked); - map.put("load_game", MenuInput::loadGameClicked); - map.put("quit", MenuInput::quitClicked); + map.put("new_game", mMenuInput::newGameClicked); + map.put("load_game", mMenuInput::loadGameClicked); + map.put("quit", mMenuInput::quitClicked); return map; } @Override protected void initButtons() { loadButtonsFromJson(Gdx.files.internal("json/menu_main_buttons.json")); - if (GameSaver.exists()) { + if (GameSaver.exists(mMainConfig)) { getButtons().get("load_game").setType(Button.NORMAL); } } diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java index 6e6b666..2c3b38b 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuNewGame.java @@ -1,6 +1,8 @@ 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; @@ -8,16 +10,16 @@ import java.util.HashMap; public class MenuNewGame extends Menu { - public MenuNewGame(float width, float height, ButtonRenderer buttonRenderer) { - super(width, height, buttonRenderer); + public MenuNewGame(float width, float height, ButtonRenderer buttonRenderer, MainConfig mainConfig, MenuProc.Input menuInput) { + super(width, height, buttonRenderer, mainConfig, menuInput); } @Override protected HashMap getButtonEventListeners() { HashMap map = new HashMap<>(); - map.put("survival", MenuInput::survivalClicked); - map.put("creative", MenuInput::creativeClicked); - map.put("back", MenuInput::backClicked); + map.put("survival", mMenuInput::survivalClicked); + map.put("creative", mMenuInput::creativeClicked); + map.put("back", mMenuInput::backClicked); return map; } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java b/core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java deleted file mode 100644 index e706403..0000000 --- a/core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java +++ /dev/null @@ -1,8 +0,0 @@ -package ru.deadsoftware.cavedroid.misc.states; - -public enum AppState { - MENU, - GAME, - SAVE, - LOAD -} diff --git a/core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java b/core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java deleted file mode 100644 index f2ceb44..0000000 --- a/core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java +++ /dev/null @@ -1,7 +0,0 @@ -package ru.deadsoftware.cavedroid.misc.states; - -public enum GameState { - PLAY, - CREATIVE_INV, - PAUSE -} diff --git a/core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java b/core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java deleted file mode 100644 index 69c39c4..0000000 --- a/core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java +++ /dev/null @@ -1,8 +0,0 @@ -package ru.deadsoftware.cavedroid.misc.states; - -public enum MenuState { - MAIN, - NEW_GAME, - LOADING, - SAVING -} diff --git a/core/src/ru/deadsoftware/cavedroid/misc/states/package-info.java b/core/src/ru/deadsoftware/cavedroid/misc/states/package-info.java deleted file mode 100644 index 23fc406..0000000 --- a/core/src/ru/deadsoftware/cavedroid/misc/states/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -@NonnullByDefault -package ru.deadsoftware.cavedroid.misc.states; - -import ru.deadsoftware.cavedroid.misc.annotations.NonnullByDefault; \ No newline at end of file -- 2.29.2