DEADSOFTWARE

Add my repo for automultibind
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / CaveGame.java
index 1f628a92fe68a6a960cfa4cc955a5633ecdfd8ed..9ca34b8e0507a5a42523accf769469847185661a 100644 (file)
@@ -1,30 +1,49 @@
 package ru.deadsoftware.cavedroid;
 
+import com.badlogic.gdx.Application;
 import com.badlogic.gdx.Game;
 import com.badlogic.gdx.Gdx;
-import ru.deadsoftware.cavedroid.game.GameItems;
+import com.badlogic.gdx.Graphics;
 import ru.deadsoftware.cavedroid.game.GameScreen;
 import ru.deadsoftware.cavedroid.misc.Assets;
+import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
+import ru.deadsoftware.cavedroid.prefs.PreferencesStore;
+
+import javax.annotation.Nullable;
 
 public class CaveGame extends Game {
 
     private static final String TAG = "CaveGame";
 
-    public static final String VERSION = "alpha 0.4.1";
+    public static final String VERSION = "alpha 0.9.2";
 
     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,
+                    PreferencesStore preferencesStore,
+                    @Nullable String assetsPackPath) {
         mGameFolder = gameFolder;
         mTouch = touch;
+        mAssetsPackPath = assetsPackPath;
+
+        mMainComponent = DaggerMainComponent
+                .builder()
+                .caveGame(this)
+                .preferencesStore(preferencesStore)
+                .build();
 
-        mMainComponent = DaggerMainComponent.builder().caveGame(this).build();
         mMainConfig = mMainComponent.getMainConfig();
+        mAssetLoader = mMainComponent.getAssetLoader();
     }
 
     public void setDebug(boolean debug) {
@@ -32,7 +51,7 @@ public class CaveGame extends Game {
     }
 
     private void initConfig() {
-        int width = mTouch ? 320 : 480;
+        int width = 480;
         int height = (int) (width * ((float) Gdx.graphics.getHeight() / Gdx.graphics.getWidth()));
 
         mMainConfig.setMainComponent(mMainComponent);
@@ -41,11 +60,26 @@ public class CaveGame extends Game {
         mMainConfig.setWidth(width);
         mMainConfig.setHeight(height);
         mMainConfig.setShowInfo(mDebug);
+        mMainConfig.setAssetsPackPath(mAssetsPackPath);
+
+        if (mDebug) {
+            Gdx.app.setLogLevel(Application.LOG_DEBUG);
+        } else {
+            Gdx.app.setLogLevel(Application.LOG_ERROR);
+        }
+
+        mMainConfig.setFullscreenToggleListener((value) -> {
+            if (value) {
+                Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());
+            } else {
+                Gdx.graphics.setWindowedMode(width, height);
+            }
+        });
     }
 
-    public void newGame() {
+    public void newGame(int gameMode) {
         GameScreen gameScreen = mMainComponent.getGameScreen();
-        gameScreen.newGame();
+        gameScreen.newGame(gameMode);
         setScreen(gameScreen);
     }
 
@@ -56,20 +90,27 @@ public class CaveGame extends Game {
     }
 
     public void quitGame() {
+        if (screen != null) {
+            screen.dispose();
+        }
         setScreen(mMainComponent.getMenuScreen());
     }
 
     @Override
     public void create() {
-        Gdx.app.log(TAG, mGameFolder);
         Gdx.files.absolute(mGameFolder).mkdirs();
-
-        Assets.load();
-        GameItems.load();
-
         initConfig();
 
+        Gdx.app.debug(TAG, mGameFolder);
+        Assets.load(mAssetLoader);
         setScreen(mMainComponent.getMenuScreen());
     }
 
+    @Override
+    public void dispose() {
+        if (screen != null) {
+            screen.dispose();
+        }
+        Assets.dispose();
+    }
 }