Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.archmage.toolbox;
- import java.util.ArrayList;
- public class Timer {
- private float duration;
- private double startTimestamp;
- private int elapseCount;
- private final boolean loops;
- private boolean elapsedThisFrame;
- private double lastUpdated;
- private static final float LAST_UPDATED_THRESHOLD = 0.5f;
- private static final double START_TIMESTAMP = getTime();
- /** not implemented */
- private boolean reversed;
- private enum State {
- STOPPED,
- RUNNING,
- PAUSED,
- FINISHED
- }
- private State state;
- private Runnable onStart = () -> {};
- private Runnable onUpdate = () -> {};
- private Runnable onElapse = () -> {};
- private static ArrayList<Timer> timers = new ArrayList<>();
- protected Timer(float duration, boolean loops) {
- this.duration = duration;
- this.loops = loops;
- elapseCount = 0;
- startTimestamp = -1;
- state = State.STOPPED;
- reversed = false;
- }
- public static Timer make(float duration, boolean loops) {
- Timer timer = new Timer(duration, loops);
- add(timer);
- return timer;
- }
- protected static final void add(Timer timer) {
- timers.add(timer);
- }
- public boolean start() {
- if(state != State.STOPPED && state != State.FINISHED) return false;
- state = State.RUNNING;
- startTimestamp = getUptimeSeconds();
- lastUpdated = getUptimeSeconds();
- onStart.run();
- return true;
- }
- private boolean start(float excess) {
- if(!start()) return false;
- if(state == State.RUNNING) {
- startTimestamp -= excess;
- }
- return true;
- }
- public void stop() {
- state = State.STOPPED;
- elapseCount = 0;
- startTimestamp = -1;
- }
- public void pause() {
- if(state != State.RUNNING) return;
- state = State.PAUSED;
- }
- public void resume() {
- if(state != State.PAUSED) return;
- state = State.RUNNING;
- // startTimestamp += T.getUptimeSeconds() - pauseTimestamp;
- }
- public void update() {
- elapsedThisFrame = false;
- onUpdate.run();
- if(state == State.PAUSED) {
- startTimestamp += getUptimeSeconds() - lastUpdated;
- }
- else if(state == State.RUNNING) {
- // counteract no-update compensation
- if(getUptimeSeconds() - lastUpdated > LAST_UPDATED_THRESHOLD) {
- startTimestamp += getUptimeSeconds() - lastUpdated;
- }
- if(getPercentage() >= 1) {
- state = State.FINISHED;
- elapseCount++;
- elapsedThisFrame = true;
- onElapse.run();
- if(loops) {
- start((float)(getUptimeSeconds() - startTimestamp - duration));
- }
- }
- }
- lastUpdated = getUptimeSeconds();
- }
- public void onStart(Runnable onStart) {
- if(onStart == null) this.onStart = () -> {};
- this.onStart = onStart;
- }
- public void onUpdate(Runnable onUpdate) {
- if(onUpdate == null) this.onUpdate = () -> {};
- this.onUpdate = onUpdate;
- }
- public void onElapse(Runnable onElapse) {
- if(onElapse == null) this.onElapse = () -> {};
- this.onElapse = onElapse;
- }
- public void setDuration(float duration) {
- this.duration = duration;
- }
- public void setReversed(boolean reversed) {
- this.reversed = reversed;
- }
- public void reverse() {
- reversed = !reversed;
- }
- public boolean hasElapsed() {
- return elapseCount > 0;
- }
- public int getElapseCount() {
- return elapseCount;
- }
- public float getPercentage() {
- return (float)MathsTools.clamp((getUptimeSeconds() - startTimestamp) / duration, 0, 1);
- }
- public boolean getElapsedThisFrame() {
- return elapsedThisFrame;
- }
- public float getDuration() {
- return duration;
- }
- public boolean getIsPaused() {
- return state == State.PAUSED;
- }
- public boolean getIsRunning() {
- return state == State.RUNNING;
- }
- public static double getTime() {
- return System.nanoTime() / 1000000000.0;
- }
- public static double getUptimeSeconds() {
- return getTime() - START_TIMESTAMP;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement