001package com.fs.starfarer.api.util; 002 003import java.util.Random; 004 005/** 006 * Timer utility that fires at a random interval between minInterval and maxInterval. 007 * <p> 008 * To use, call {@code intervalUtil.advance(float amount);} in an {@code advance(float amount)} method (i.e. every frame). 009 * Then, call {@code intervalUtil.intervalElapsed()} each frame to check if the timer has elapsed. 010 * <p/> 011 * It automatically resets itself after it elapses. 012 * 013 * @param minInterval minimum duration of the timer, in seconds 014 * @param maxInterval maximum duration of the timer, in seconds 015 */ 016public class IntervalUtil { 017 018 private float minInterval; 019 private float maxInterval; 020 021 private float currInterval; 022 private float elapsed = 0; 023 private boolean intervalElapsed = false; 024 025 private Random random; 026 027 /** 028 * Timer utility that fires at a random interval between minInterval and maxInterval. 029 * <p> 030 * To use, call {@code intervalUtil.advance(float amount);} in an {@code advance(float amount)} method (i.e. every frame). 031 * Then, call {@code intervalUtil.intervalElapsed()} each frame to check if the timer has elapsed. 032 * <p/> 033 * It automatically resets itself after it elapses. 034 * 035 * @param minInterval minimum duration of the timer, in seconds 036 * @param maxInterval maximum duration of the timer, in seconds 037 */ 038 public IntervalUtil(float minInterval, float maxInterval) { 039 setInterval(minInterval, maxInterval); 040 } 041 042 public void forceCurrInterval(float value) { 043 currInterval = value; 044 } 045 046 public Random getRandom() { 047 return random; 048 } 049 050 public void setRandom(Random random) { 051 this.random = random; 052 } 053 054 public void randomize() { 055 if (random != null) { 056 advance(random.nextFloat() * minInterval); 057 } else { 058 advance((float) Math.random() * minInterval); 059 } 060 } 061 062 public void forceIntervalElapsed() { 063 elapsed = currInterval; 064 } 065 066 067 public float getElapsed() { 068 return elapsed; 069 } 070 071 public void nextInterval() { 072 if (random != null) { 073 currInterval = minInterval + (maxInterval - minInterval) * random.nextFloat(); 074 } else { 075 currInterval = minInterval + (maxInterval - minInterval) * (float) Math.random(); 076 } 077 elapsed = 0; 078 intervalElapsed = false; 079 } 080 081 public void advance(float amount) { 082 if (intervalElapsed) { 083 nextInterval(); 084 } 085 elapsed += amount; 086 if (elapsed >= currInterval) { 087 intervalElapsed = true; 088 } 089 } 090 091 /** 092 * Returns true once and only once when the current interval is over. Must be called every frame, otherwise 093 * the time it returns true might be missed. 094 * @return 095 */ 096 public boolean intervalElapsed() { 097 return intervalElapsed; 098 } 099 100 public float getIntervalDuration() { 101 return currInterval; 102 } 103 104 public void setInterval(float min, float max) { 105 this.minInterval = min; 106 this.maxInterval = max; 107 nextInterval(); 108 } 109 110 public void setElapsed(float elapsed) { 111 this.elapsed = elapsed; 112 } 113 114 public float getMinInterval() { 115 return minInterval; 116 } 117 118 public float getMaxInterval() { 119 return maxInterval; 120 } 121 122 123}