Skip to content

Commit

Permalink
tried to fix latency by using the clip position
Browse files Browse the repository at this point in the history
  • Loading branch information
Althoumb committed Apr 3, 2018
1 parent 4eef834 commit db1fd8d
Showing 1 changed file with 41 additions and 46 deletions.
87 changes: 41 additions & 46 deletions src/game/RhythmState.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public class RhythmState extends DefaultGameState {
// circles
private float innerRadius = 20f; // radius of the inner hit circles which you click

private Clip clip;
private long starttime = System.currentTimeMillis(); // initial time
private long songtime = 0; // time since beginning of the song, in ms
private boolean musicstarted = false;

private CopyOnWriteArrayList<HitObject> hitobjects = new CopyOnWriteArrayList<>();
private LinkedList<Beat> beatmap = new LinkedList<>();
Expand Down Expand Up @@ -104,8 +104,7 @@ public void enter(GameContainer gc, StateBasedGame arg1) throws SlickException {
.get(BeatmapParser.DEFAULT_BEATMAP_FOLDER, beatmapzipfilename)
.toString();
MusicDecoder.convertAllAudioToWav(extractedSongFilename);
Clip clip = AudioSystem.getClip();

clip = AudioSystem.getClip();
// load audio from file into stream
AudioInputStream audioStream = AudioSystem.getAudioInputStream(
Paths.get(extractedSongFilename, "audio.wav").toFile());
Expand All @@ -116,7 +115,6 @@ public void enter(GameContainer gc, StateBasedGame arg1) throws SlickException {
.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-25f);
clip.start();
musicstarted = true;
} catch (JavaLayerException | LineUnavailableException
| UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -207,52 +205,49 @@ public void render(GameContainer gc, StateBasedGame arg1, Graphics g)
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
if (musicstarted) { // only update if the music has actually started
songtime += delta;

percentcompletion = (float) Math
.floor(10000 * songtime / beatmap.getLast().time)
/ 100;

// puts objects in the beatmap into the hitobjects list when needed
if (beatmapindex <= beatmap.size() - 1
&& beatmap.get(beatmapindex).time <= CIRCLE_TIME + songtime) {
Beat currentbeat = beatmap.get(beatmapindex);
HitObject hitobject = new HitObject(currentbeat.x, currentbeat.y,
maxRadius,
CIRCLE_TIME, false);
hitobjects.add(hitobject);
beatmapindex++;
}
songtime = clip.getMicrosecondPosition() / 1000;

percentcompletion = (float) Math
.floor(10000 * songtime / beatmap.getLast().time)
/ 100;

// puts objects in the beatmap into the hitobjects list when needed
if (beatmapindex <= beatmap.size() - 1
&& beatmap.get(beatmapindex).time <= CIRCLE_TIME + songtime) {
Beat currentbeat = beatmap.get(beatmapindex);
HitObject hitobject = new HitObject(currentbeat.x, currentbeat.y,
maxRadius,
CIRCLE_TIME, false);
hitobjects.add(hitobject);
beatmapindex++;
}

for (HitObject hitobject : hitobjects) {
int index = hitobjects.indexOf(hitobject);

// shrinks approach circle and reduces remaining time on screen
hitobjects.set(index,
new HitObject(hitobject.x, hitobject.y,
maxRadius * (hitobject.duration / CIRCLE_TIME),
hitobject.duration - delta, hitobject.clicked));

// removes hit circle if time left is less than or equal to the lenience
// time, or the circle has already been clicked and it's time left is less
// than zero
if (hitobject.duration <= -LENIENCE_TIME
|| hitobject.clicked && hitobject.duration <= 0) {
if (!hitobject.clicked) {
combo = 0;
}
hitobjects.remove(index);
hitobjectscompleted++;
hitpercent = (float) (Math.floor(10000 * points / hitobjectscompleted)
/ 100);
perfectionpercent = (float) (Math
.floor(10000 * perfection / hitobjectscompleted)
/ 100);
for (HitObject hitobject : hitobjects) {
int index = hitobjects.indexOf(hitobject);

// shrinks approach circle and reduces remaining time on screen
hitobjects.set(index,
new HitObject(hitobject.x, hitobject.y,
maxRadius * (hitobject.duration / CIRCLE_TIME),
hitobject.duration - delta, hitobject.clicked));

// removes hit circle if time left is less than or equal to the lenience
// time, or the circle has already been clicked and it's time left is less
// than zero
if (hitobject.duration <= -LENIENCE_TIME
|| hitobject.clicked && hitobject.duration <= 0) {
if (!hitobject.clicked) {
combo = 0;
}
hitobjects.remove(index);
hitobjectscompleted++;
hitpercent = (float) (Math.floor(10000 * points / hitobjectscompleted)
/ 100);
perfectionpercent = (float) (Math
.floor(10000 * perfection / hitobjectscompleted)
/ 100);
}
}

}

@Override
Expand Down

0 comments on commit db1fd8d

Please sign in to comment.