Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package com.archmage.toolbox;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Timer {
  6.  
  7. private float duration;
  8. private double startTimestamp;
  9.  
  10. private int elapseCount;
  11. private final boolean loops;
  12. private boolean elapsedThisFrame;
  13.  
  14. private double lastUpdated;
  15.  
  16. private static final float LAST_UPDATED_THRESHOLD = 0.5f;
  17.  
  18. private static final double START_TIMESTAMP = getTime();
  19.  
  20. /** not implemented */
  21. private boolean reversed;
  22.  
  23. private enum State {
  24. STOPPED,
  25. RUNNING,
  26. PAUSED,
  27. FINISHED
  28. }
  29.  
  30. private State state;
  31.  
  32. private Runnable onStart = () -> {};
  33. private Runnable onUpdate = () -> {};
  34. private Runnable onElapse = () -> {};
  35.  
  36. private static ArrayList<Timer> timers = new ArrayList<>();
  37.  
  38. protected Timer(float duration, boolean loops) {
  39. this.duration = duration;
  40. this.loops = loops;
  41.  
  42. elapseCount = 0;
  43. startTimestamp = -1;
  44.  
  45. state = State.STOPPED;
  46.  
  47. reversed = false;
  48. }
  49.  
  50. public static Timer make(float duration, boolean loops) {
  51. Timer timer = new Timer(duration, loops);
  52. add(timer);
  53. return timer;
  54. }
  55.  
  56. protected static final void add(Timer timer) {
  57. timers.add(timer);
  58. }
  59.  
  60. public boolean start() {
  61. if(state != State.STOPPED && state != State.FINISHED) return false;
  62. state = State.RUNNING;
  63. startTimestamp = getUptimeSeconds();
  64. lastUpdated = getUptimeSeconds();
  65. onStart.run();
  66. return true;
  67. }
  68.  
  69. private boolean start(float excess) {
  70. if(!start()) return false;
  71. if(state == State.RUNNING) {
  72. startTimestamp -= excess;
  73. }
  74. return true;
  75. }
  76.  
  77. public void stop() {
  78. state = State.STOPPED;
  79. elapseCount = 0;
  80. startTimestamp = -1;
  81. }
  82.  
  83. public void pause() {
  84. if(state != State.RUNNING) return;
  85. state = State.PAUSED;
  86. }
  87.  
  88. public void resume() {
  89. if(state != State.PAUSED) return;
  90. state = State.RUNNING;
  91. // startTimestamp += T.getUptimeSeconds() - pauseTimestamp;
  92. }
  93.  
  94. public void update() {
  95. elapsedThisFrame = false;
  96. onUpdate.run();
  97. if(state == State.PAUSED) {
  98. startTimestamp += getUptimeSeconds() - lastUpdated;
  99. }
  100. else if(state == State.RUNNING) {
  101. // counteract no-update compensation
  102. if(getUptimeSeconds() - lastUpdated > LAST_UPDATED_THRESHOLD) {
  103. startTimestamp += getUptimeSeconds() - lastUpdated;
  104. }
  105. if(getPercentage() >= 1) {
  106. state = State.FINISHED;
  107. elapseCount++;
  108. elapsedThisFrame = true;
  109. onElapse.run();
  110. if(loops) {
  111. start((float)(getUptimeSeconds() - startTimestamp - duration));
  112. }
  113. }
  114. }
  115.  
  116. lastUpdated = getUptimeSeconds();
  117. }
  118.  
  119. public void onStart(Runnable onStart) {
  120. if(onStart == null) this.onStart = () -> {};
  121. this.onStart = onStart;
  122. }
  123.  
  124. public void onUpdate(Runnable onUpdate) {
  125. if(onUpdate == null) this.onUpdate = () -> {};
  126. this.onUpdate = onUpdate;
  127. }
  128.  
  129. public void onElapse(Runnable onElapse) {
  130. if(onElapse == null) this.onElapse = () -> {};
  131. this.onElapse = onElapse;
  132. }
  133.  
  134. public void setDuration(float duration) {
  135. this.duration = duration;
  136. }
  137.  
  138. public void setReversed(boolean reversed) {
  139. this.reversed = reversed;
  140. }
  141.  
  142. public void reverse() {
  143. reversed = !reversed;
  144. }
  145.  
  146. public boolean hasElapsed() {
  147. return elapseCount > 0;
  148. }
  149.  
  150. public int getElapseCount() {
  151. return elapseCount;
  152. }
  153.  
  154. public float getPercentage() {
  155. return (float)MathsTools.clamp((getUptimeSeconds() - startTimestamp) / duration, 0, 1);
  156. }
  157.  
  158. public boolean getElapsedThisFrame() {
  159. return elapsedThisFrame;
  160. }
  161.  
  162. public float getDuration() {
  163. return duration;
  164. }
  165.  
  166. public boolean getIsPaused() {
  167. return state == State.PAUSED;
  168. }
  169.  
  170. public boolean getIsRunning() {
  171. return state == State.RUNNING;
  172. }
  173.  
  174. public static double getTime() {
  175. return System.nanoTime() / 1000000000.0;
  176. }
  177.  
  178. public static double getUptimeSeconds() {
  179. return getTime() - START_TIMESTAMP;
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement