summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/zproject/ZProject.java
blob: 12dc9822ae48bc1b04762d1a66978b8c3104b40a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ch.asynk.zproject;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;

import ch.asynk.zproject.screens.GameScreen;

public class ZProject extends Game
{
    public static final String DOM = "ZProject";

    private enum State
    {
        NONE,
        GAME
    }
    private State state;

    public final Assets assets = new Assets();

    @Override public void create()
    {
        this.state = State.NONE;
        Gdx.app.setLogLevel(Gdx.app.LOG_DEBUG);
        debug(String.format("create() [%d;%d] %f", Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Gdx.graphics.getDensity()));
        switchToGame();
    }

    @Override public void dispose()
    {
        switchToNone();
        assets.dispose();
    }

    public static void error(String msg)
    {
        Gdx.app.error(DOM, msg);
    }

    public static void debug(String msg)
    {
        Gdx.app.debug(DOM, msg);
    }

    public static void debug(String from, String msg)
    {
        Gdx.app.debug(DOM, String.format("%s : %s", from, msg));
    }

    private void switchTo(Screen nextScreen, State nextState)
    {
        if (state == nextState) {
            error("switch from and to " + state);
            return;
        }
        switch(state) {
            case GAME: assets.unloadGame(); break;
        }
        if (state != State.NONE) {
            getScreen().dispose();
        }
        setScreen(nextScreen);
        this.state = nextState;
    }

    public void switchToNone()
    {
        switchTo(null, State.NONE);
    }

    public void switchToGame()
    {
        assets.loadGame();
        assets.finishLoading();
        switchTo(new GameScreen(this), State.GAME);
    }
}