summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/screens/LoadScreen.java
blob: 1b4b401716dc57c395bb664cec3908649bae19e5 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ch.asynk.tankontank.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.utils.viewport.FitViewport;

import ch.asynk.tankontank.TankOnTank;
import ch.asynk.tankontank.loading.LoadingBar;

public class LoadScreen implements Screen
{
    private final TankOnTank game;

    private float percent;
    private float delay = 0.0f;
    private float[] xPath = { 68, 164, 260, 356, 452, 404, 356, 452, 548, 596, 692};
    private float[] yPath = { 148,148, 148, 148, 148, 231, 314, 314, 314, 397, 397};

    private boolean ready;
    private boolean assetsLoaded;
    private Texture bg;
    private Texture unit;
    private float dx;
    private float dy;

    private final Camera camera;
    private final SpriteBatch batch;
    private final FitViewport viewport;

    public LoadScreen(final TankOnTank game)
    {
        this.game = game;
        this.batch = new SpriteBatch();

        float width = Gdx.graphics.getWidth();
        float height = Gdx.graphics.getHeight();
        this.camera = new OrthographicCamera(width, height);
        this.viewport = new FitViewport(width, height, camera);
        this.ready = false;
        this.assetsLoaded = false;
        this.dx = ((width - 800) / 2f);
        this.dy = ((height - 600) / 4f);
    }

    @Override
    public void render(float delta)
    {
        if (game.manager.update()) {
            delay += delta;
            if (delay >= 0.6f)
                assetsLoadingCompleted();
        }

        if (!ready) return;

        percent = Interpolation.linear.apply(percent, game.manager.getProgress(), 0.1f);
        int idx = (int) (percent * 10);
        float fraction = ((percent * 100 ) % 10 / 10);
        float x = (xPath[idx] + ((xPath[idx + 1] - xPath[idx]) * fraction));
        float y = (yPath[idx] + ((yPath[idx + 1] - yPath[idx]) * fraction));

        camera.update();

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(bg, dx, dy);
        batch.draw(unit, (x + dx), (y + dy));
        batch.end();
    }

    @Override
    public void show()
    {
        viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
        load();
        game.loadAssets();
    }

    @Override
    public void resize(int width, int height)
    {
        viewport.update(width, height, true);
    }

    @Override
    public void dispose()
    {
        unload();
    }

    @Override
    public void hide()
    {
        unload();
    }

    @Override
    public void pause()
    {
        unload();
    }

    @Override
    public void resume()
    {
        load();
    }

    private void load()
    {
        game.manager.load("data/unit.png", Texture.class);
        game.manager.load("data/loading.png", Texture.class);
        game.manager.finishLoading();
        bg = game.manager.get("data/loading.png", Texture.class);
        unit = game.manager.get("data/unit.png", Texture.class);
        ready = true;
    }

    private void unload()
    {
        if (!ready) return;
        bg.dispose();
        unit.dispose();
        game.manager.unload("data/loading.png");
        game.manager.unload("data/unit.png");
        ready = false;
    }

    private void assetsLoadingCompleted()
    {
        if (assetsLoaded) return;
        assetsLoaded = true;
        TankOnTank.debug("LoadScreen", "assets ready : " + (Gdx.app.getJavaHeap()/1024.0f) + "KB");
        game.switchToOptions();
        dispose();
    }
}