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

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.utils.Disposable;

import sk.neuromancer.sphaera.interf.Tickable;

public class SoundPlayer implements Tickable, Disposable{

    private Sound[] base = new Sound[4];
    private Sound explosion;

    public static final String BASE_SOUND_NAME = "base";

    private boolean isLoaded = false;
    private boolean isPlaying = false;

    public static final int BPM = 100;
    public static final float TPB = (SphaeraGame.TPS * 60) / BPM;

    public static final float VOLUME = 0.2f;
    
    private int[] order = {3,2,0,1};

    private int s = 0;

    public SoundPlayer() {
        
    }

    @Override
    public void tick(long tickCount) {
        if(!isPlaying)
            return;
        if(!isLoaded)
            return;
        if(tickCount % TPB == 0){
            base[order[s]].play(VOLUME);
            s++;
            s%=base.length;
        }
    }

    public void load() {
        for(int i = 0; i < base.length; i++){
            base[i] = Gdx.audio.newSound(Gdx.files.internal(BASE_SOUND_NAME + i + ".wav"));
        }
        explosion = Gdx.audio.newSound(Gdx.files.internal("explosion.wav"));
        isLoaded = true;
    }

    public void play(){
        isPlaying = true;
    }

    public void pause(){
        isPlaying = false;
    }


    public void playExplosion(){
        explosion.play();
    }

    @Override
    public void dispose() {
        for(Sound s: base){
            s.dispose();
        }
        explosion.dispose();
    }
}