DEADSOFTWARE

CaveGame in kotlin
authorfredboy <fredboy@protonmail.com>
Wed, 22 May 2024 13:46:11 +0000 (20:46 +0700)
committerfredboy <fredboy@protonmail.com>
Wed, 22 May 2024 13:46:11 +0000 (20:46 +0700)
android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java
core/src/ru/deadsoftware/cavedroid/CaveGame.java [deleted file]
core/src/ru/deadsoftware/cavedroid/CaveGame.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/misc/utils/GdxExtensions.kt [new file with mode: 0644]
desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java

index 3f38ad401d37bd40cefdf1f78b1aeddb04254d6a..714c8df376cb75a903808730a66c037efd4a6edb 100644 (file)
@@ -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 (file)
index 219f5b4..0000000
+++ /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 (file)
index 0000000..225dd3a
--- /dev/null
@@ -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 (file)
index 0000000..1c80048
--- /dev/null
@@ -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
index 590392063167b8a599e634082c421671f32ded63..713749703d6ebe52ea59120850f4598164da414e 100644 (file)
@@ -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);
     }