From db1fd8d33aefa55b63a792c1b2f4ec108125ac5d Mon Sep 17 00:00:00 2001 From: s-ballw Date: Tue, 3 Apr 2018 12:26:17 -0700 Subject: [PATCH] tried to fix latency by using the clip position --- src/game/RhythmState.java | 87 ++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/src/game/RhythmState.java b/src/game/RhythmState.java index c88c78e..9556a08 100644 --- a/src/game/RhythmState.java +++ b/src/game/RhythmState.java @@ -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 hitobjects = new CopyOnWriteArrayList<>(); private LinkedList beatmap = new LinkedList<>(); @@ -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()); @@ -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(); @@ -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