From 4a1f204c0cbe56e4c85f29f0d9f89ccd907f5f3b Mon Sep 17 00:00:00 2001
From: fredboy <fredboy@protonmail.com>
Date: Wed, 22 May 2024 20:46:11 +0700
Subject: [PATCH] CaveGame in kotlin

---
 .../cavedroid/AndroidLauncher.java            |   5 +-
 .../ru/deadsoftware/cavedroid/CaveGame.java   | 114 ------------------
 .../src/ru/deadsoftware/cavedroid/CaveGame.kt |  89 ++++++++++++++
 .../cavedroid/misc/utils/GdxExtensions.kt     |   5 +
 .../cavedroid/desktop/DesktopLauncher.java    |   9 +-
 5 files changed, 102 insertions(+), 120 deletions(-)
 delete mode 100644 core/src/ru/deadsoftware/cavedroid/CaveGame.java
 create mode 100644 core/src/ru/deadsoftware/cavedroid/CaveGame.kt
 create mode 100644 core/src/ru/deadsoftware/cavedroid/misc/utils/GdxExtensions.kt

diff --git a/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java b/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java
index 3f38ad4..714c8df 100644
--- a/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java
+++ b/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java
@@ -18,9 +18,8 @@ public class AndroidLauncher extends AndroidApplication {
             e.printStackTrace();
             exit();
         }
-        CaveGame caveGame = new CaveGame(gameFolder, true,
-                new AndroidPreferencesStore(getApplicationContext()), null);
-        caveGame.setDebug(BuildConfig.DEBUG);
+        CaveGame caveGame = new CaveGame(gameFolder, true, BuildConfig.DEBUG,
+                new AndroidPreferencesStore(getApplicationContext()));
         initialize(caveGame, config);
     }
 
diff --git a/core/src/ru/deadsoftware/cavedroid/CaveGame.java b/core/src/ru/deadsoftware/cavedroid/CaveGame.java
deleted file mode 100644
index 219f5b4..0000000
--- a/core/src/ru/deadsoftware/cavedroid/CaveGame.java
+++ /dev/null
@@ -1,114 +0,0 @@
-package ru.deadsoftware.cavedroid;
-
-import com.badlogic.gdx.Application;
-import com.badlogic.gdx.Game;
-import com.badlogic.gdx.Gdx;
-import org.jetbrains.annotations.Nullable;
-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;
-
-public class CaveGame extends Game {
-
-    private static final String TAG = "CaveGame";
-
-    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;
-
-    @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();
-
-        mMainConfig = mMainComponent.getMainConfig();
-        mAssetLoader = mMainComponent.getAssetLoader();
-    }
-
-    public void setDebug(boolean debug) {
-        mDebug = debug;
-    }
-
-    private void initConfig() {
-        int width = 480;
-        int height = (int) (width * ((float) Gdx.graphics.getHeight() / Gdx.graphics.getWidth()));
-
-        mMainConfig.setMainComponent(mMainComponent);
-        mMainConfig.setGameFolder(mGameFolder);
-        mMainConfig.setTouch(mTouch);
-        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(int gameMode) {
-        GameScreen gameScreen = mMainComponent.getGameScreen();
-        gameScreen.newGame(gameMode);
-        setScreen(gameScreen);
-    }
-
-    public void loadGame() {
-        GameScreen gameScreen = mMainComponent.getGameScreen();
-        gameScreen.loadGame();
-        setScreen(gameScreen);
-    }
-
-    public void quitGame() {
-        if (screen != null) {
-            screen.dispose();
-        }
-        setScreen(mMainComponent.getMenuScreen());
-    }
-
-    @Override
-    public void create() {
-        Gdx.files.absolute(mGameFolder).mkdirs();
-        initConfig();
-
-        Gdx.app.debug(TAG, mGameFolder);
-        Assets.load(mAssetLoader);
-        setScreen(mMainComponent.getMenuScreen());
-    }
-
-    @Override
-    public void dispose() {
-        if (screen != null) {
-            screen.dispose();
-        }
-        Assets.dispose();
-    }
-}
diff --git a/core/src/ru/deadsoftware/cavedroid/CaveGame.kt b/core/src/ru/deadsoftware/cavedroid/CaveGame.kt
new file mode 100644
index 0000000..225dd3a
--- /dev/null
+++ b/core/src/ru/deadsoftware/cavedroid/CaveGame.kt
@@ -0,0 +1,89 @@
+package ru.deadsoftware.cavedroid
+
+import com.badlogic.gdx.Application
+import com.badlogic.gdx.Game
+import com.badlogic.gdx.Gdx
+import ru.deadsoftware.cavedroid.misc.Assets
+import ru.deadsoftware.cavedroid.misc.utils.AssetLoader
+import ru.deadsoftware.cavedroid.misc.utils.ratio
+import ru.deadsoftware.cavedroid.prefs.PreferencesStore
+
+class CaveGame(
+    private val gameDataDirectoryPath: String,
+    private val isTouchScreen: Boolean,
+    private val isDebug: Boolean,
+    private val preferencesStore: PreferencesStore,
+) : Game() {
+
+    private val mainComponent: MainComponent
+    private val mainConfig: MainConfig
+
+    private val assetLoader: AssetLoader
+
+    init {
+        mainComponent = DaggerMainComponent.builder()
+            .caveGame(this)
+            .preferencesStore(preferencesStore)
+            .build()
+
+        mainConfig = mainComponent.mainConfig
+        assetLoader = mainComponent.assetLoader
+    }
+
+    private fun initMainConfig() {
+        val width = DEFAULT_VIEWPORT_WIDTH
+        val height = width / Gdx.graphics.ratio
+
+        mainConfig.mainComponent = mainComponent
+        mainConfig.gameFolder = gameDataDirectoryPath
+        mainConfig.isTouch = isTouchScreen
+        mainConfig.width = width
+        mainConfig.height = height
+        mainConfig.isShowInfo = isDebug
+
+        Gdx.app.logLevel = if (isDebug) Application.LOG_DEBUG else Application.LOG_ERROR
+
+        mainConfig.setFullscreenToggleListener { isFullscreen ->
+            if (isFullscreen) {
+                Gdx.graphics.setFullscreenMode(Gdx.graphics.displayMode);
+            } else {
+                Gdx.graphics.setWindowedMode(width.toInt(), height.toInt());
+            }
+        }
+    }
+
+    fun newGame(gameMode: Int) {
+        setScreen(mainComponent.gameScreen.apply { newGame(gameMode) })
+    }
+
+    fun loadGame() {
+        setScreen(mainComponent.gameScreen.apply { loadGame() })
+    }
+
+    fun quitGame() {
+        screen?.dispose()
+        setScreen(mainComponent.menuScreen)
+    }
+
+    override fun create() {
+        Gdx.files.absolute(gameDataDirectoryPath).mkdirs()
+        initMainConfig()
+
+        Assets.load(assetLoader)
+        setScreen(mainComponent.menuScreen)
+    }
+
+    override fun dispose() {
+        screen?.dispose()
+        Assets.dispose()
+    }
+
+
+    companion object {
+        private const val TAG = "CaveGame"
+        private const val DEFAULT_VIEWPORT_WIDTH = 480f
+
+        const val VERSION = "alpha 0.9.2"
+    }
+
+}
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/GdxExtensions.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/GdxExtensions.kt
new file mode 100644
index 0000000..1c80048
--- /dev/null
+++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/GdxExtensions.kt
@@ -0,0 +1,5 @@
+package ru.deadsoftware.cavedroid.misc.utils
+
+import com.badlogic.gdx.Graphics
+
+val Graphics.ratio get() = width.toFloat() / height.toFloat()
\ No newline at end of file
diff --git a/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java b/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java
index 5903920..7137497 100644
--- a/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java
+++ b/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java
@@ -35,9 +35,12 @@ class DesktopLauncher {
             }
         }
 
-        CaveGame caveGame = new CaveGame(System.getProperty("user.home") + "/.cavedroid", touch,
-                new DesktopPreferencesStore(), assetsPath);
-        caveGame.setDebug(debug);
+        CaveGame caveGame = new CaveGame(
+                System.getProperty("user.home") + "/.cavedroid",
+                touch,
+                debug,
+                new DesktopPreferencesStore()
+        );
 
         new Lwjgl3Application(caveGame, config);
     }
-- 
2.29.2