forked from ob-f/OpenBot
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNoise.java
More file actions
58 lines (49 loc) · 1.43 KB
/
Noise.java
File metadata and controls
58 lines (49 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.openbot.env;
import android.os.SystemClock;
import java.util.Random;
public class Noise {
public Noise(int minDuration, int maxDuration, long timeout) {
this.minDuration = minDuration;
this.maxDuration = maxDuration;
this.timeout = timeout;
}
private final int minDuration;
private final int maxDuration;
private final long timeout;
private float value = 0;
private int direction = 0;
private long duration = 0;
private long startTime = 0;
public void update() {
long currentTime = SystemClock.elapsedRealtime();
if (currentTime > startTime + duration + timeout) {
startTime = currentTime;
duration = generateRandomInt(minDuration, maxDuration);
value = 0;
Random r = new Random();
direction = r.nextBoolean() ? -1 : 1;
}
if (currentTime < startTime + duration) {
if (currentTime < startTime + duration / 2) {
value += (float) generateRandomInt(1, 8) / 255;
} else {
value -= (float) generateRandomInt(1, 8) / 255;
}
value = Math.max(0, Math.min(value, 0.5f));
} else value = 0;
}
public float getValue() {
return value;
}
public int getDirection() {
return direction;
}
private int generateRandomInt(int min, int max) {
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
private float generateRandomFloat() {
Random r = new Random();
return r.nextFloat();
}
}