DEADSOFTWARE

Fix process not finishing when exited
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / CaveGame.java
1 package ru.deadsoftware.cavedroid;
2
3 import com.badlogic.gdx.Game;
4 import com.badlogic.gdx.Gdx;
5 import ru.deadsoftware.cavedroid.game.GameItems;
6 import ru.deadsoftware.cavedroid.game.GameScreen;
7 import ru.deadsoftware.cavedroid.misc.Assets;
8 import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
9
10 import javax.annotation.Nullable;
11
12 public class CaveGame extends Game {
13
14 private static final String TAG = "CaveGame";
15
16 public static final String VERSION = "alpha 0.4.2";
17
18 private final MainConfig mMainConfig;
19 private final MainComponent mMainComponent;
20 private final AssetLoader mAssetLoader;
21
22 private final String mGameFolder;
23 private final boolean mTouch;
24 private boolean mDebug;
25
26 @Nullable
27 private final String mAssetsPackPath;
28
29 public CaveGame(String gameFolder, boolean touch, @Nullable String assetsPackPath) {
30 mGameFolder = gameFolder;
31 mTouch = touch;
32 mAssetsPackPath = assetsPackPath;
33
34 mMainComponent = DaggerMainComponent.builder().caveGame(this).build();
35
36 mMainConfig = mMainComponent.getMainConfig();
37 mAssetLoader = mMainComponent.getAssetLoader();
38 }
39
40 public void setDebug(boolean debug) {
41 mDebug = debug;
42 }
43
44 private void initConfig() {
45 int width = mTouch ? 320 : 480;
46 int height = (int) (width * ((float) Gdx.graphics.getHeight() / Gdx.graphics.getWidth()));
47
48 mMainConfig.setMainComponent(mMainComponent);
49 mMainConfig.setGameFolder(mGameFolder);
50 mMainConfig.setTouch(mTouch);
51 mMainConfig.setWidth(width);
52 mMainConfig.setHeight(height);
53 mMainConfig.setShowInfo(mDebug);
54 mMainConfig.setAssetsPackPath(mAssetsPackPath);
55 }
56
57 public void newGame() {
58 GameScreen gameScreen = mMainComponent.getGameScreen();
59 gameScreen.newGame();
60 setScreen(gameScreen);
61 }
62
63 public void loadGame() {
64 GameScreen gameScreen = mMainComponent.getGameScreen();
65 gameScreen.loadGame();
66 setScreen(gameScreen);
67 }
68
69 public void quitGame() {
70 if (screen != null) {
71 screen.dispose();
72 }
73 setScreen(mMainComponent.getMenuScreen());
74 }
75
76 @Override
77 public void create() {
78 Gdx.app.log(TAG, mGameFolder);
79 Gdx.files.absolute(mGameFolder).mkdirs();
80
81 initConfig();
82
83 Assets.load(mAssetLoader);
84 GameItems.load(mAssetLoader);
85
86 setScreen(mMainComponent.getMenuScreen());
87 }
88
89 }