summaryrefslogtreecommitdiff
path: root/core/src/sk/neuromancer/sphaera/rewrite/SphaeraGame.java
blob: beb5a5af0f93b87e5282fcda6871853bfffbd37f (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
package sk.neuromancer.sphaera.rewrite;

import sk.neuromancer.sphaera.interf.Tickable;

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

public class SphaeraGame extends Game implements Tickable{
    
    private Splash splash = null;
    private Menu menu = null;
    private World world = null;
    private SoundPlayer soundPlayer;
    
    public static final int DEFAULT_WIDTH = 900;
    public static final int DEFAULT_HEIGHT = 650;
    
    public static final int TPS = 30;
    public static final int FOV = 75;
    
    private long lastTickTime;
    private long ticks, frames;
    
    @Override
    public void create(){
        Gdx.graphics.setTitle("Sphaera");
        
        splash = new Splash();
        setScreen(splash);
        
        Gdx.input.setInputProcessor(splash);
        
        soundPlayer = new SoundPlayer();
        soundPlayer.load();
        //soundPlayer.play();
        
        ticks = frames = 0;
        lastTickTime = System.currentTimeMillis();
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
    }

    @Override
    public void render() {
        long now = System.currentTimeMillis();
        if(now - lastTickTime > (1/TPS) * 1000){
            tick(ticks);
            lastTickTime = now;
        }
        draw();
    }
    
    @Override
    public void tick(long tickCount) {
        ticks++;
        GameState state = getScreen();
        state.tick(tickCount);
        soundPlayer.tick(tickCount);
        
        if(ticks % 30 == 0){
            System.out.println(Gdx.graphics.getFramesPerSecond());
        }
        
        if(state.isFinished()){
            if(state instanceof World){
                soundPlayer.playExplosion();
            }
            
            state = state.next();
            Gdx.input.setInputProcessor(state);
            setScreen(state);
            
            if(state instanceof Menu){
                menu = (Menu) state;
            }else if(state instanceof World){
                world = (World) state;
            }else if(state instanceof Exit){
                this.dispose();
                state.dispose();
                Gdx.app.exit();
            }
        }
    }
    
    public void draw(){
        frames++;
        super.render();
    }
    
    @Override
    public void pause() {
        super.pause();
    }

    @Override
    public void resume() {
        super.resume();
    }

    @Override
    public void dispose() {
        if(splash != null)
            splash.dispose();
        if(menu != null)
            menu.dispose();
        if(world != null)
            world.dispose();
        if(soundPlayer != null)
            soundPlayer.dispose();
    }

    @Override
    public GameState getScreen() {
        return (GameState) super.getScreen();
    }
}