-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXPBarUI.java
More file actions
185 lines (168 loc) · 5.7 KB
/
EXPBarUI.java
File metadata and controls
185 lines (168 loc) · 5.7 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.util.Duration;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class EXPBarUI extends HBox {
private static EXPBarUI instance;
private final IntegerProperty currentXP = new SimpleIntegerProperty(0);
private final IntegerProperty maxXP = new SimpleIntegerProperty(100);
private final DoubleProperty progress = new SimpleDoubleProperty(0.0);
private final Queue<Integer> xpQueue = new ConcurrentLinkedQueue<>();
private final AtomicBoolean animating = new AtomicBoolean(false);
private final ProgressBar progressBar;
private final Label xpLabel;
private final Label level;
private int currentLevel;
public static EXPBarUI getInstance() {
if (instance == null) {
instance = new EXPBarUI(0, 100);
}
return instance;
}
public EXPBarUI(int initialXP, int max) {
progressBar = new ProgressBar();
xpLabel = new Label();
level = new Label();
currentLevel = 1;
xpLabel.getStyleClass().add("xp-label");
level.getStyleClass().add("level-label");
progressBar.getStyleClass().add("progress-bar");
setSpacing(8);
setAlignment(Pos.CENTER);
setPadding(new Insets(4, 12, 4, 12));
getChildren().addAll(level, progressBar, xpLabel);
progressBar.progressProperty().bind(progress);
try {
int saved = DataStore.getXP("default");
if (saved > 0) initialXP = saved;
} catch (Throwable ignored) {}
currentXP.addListener((obs, oldV, newV) -> updateProgress());
maxXP.addListener((obs, oldV, newV) -> updateProgress());
currentXP.set(initialXP);
maxXP.set(max);
updateProgress();
Animations.fadeIn(this);
currentXP.addListener((obs, o, n) -> {
try {
DataStore.saveXP("default", n.intValue());
} catch (Throwable ignored) {}
});
}
private void updateProgress() {
int cur = Math.max(0, currentXP.get());
int max = Math.max(1, maxXP.get());
progress.set((double) cur / max);
updateLabel();
}
private void updateLabel() {
xpLabel.setText(String.format("%d / %d XP", currentXP.get(), maxXP.get()));
level.setText("Level " + currentLevel);
}
////old add xp without animation:
// public void addXP(int amount) {
// Platform.runLater(() -> {
// int newXP = currentXP.get() + amount;
// while (newXP >= maxXP.get()) {
// newXP -= maxXP.get();
// levelUp();
// }
// currentXP.set(newXP);
// updateProgress();
// });
// }
public void addXP(int amount) {
if (amount <= 0)
return;
xpQueue.add(amount);
if (animating.compareAndSet(false, true)) {
Platform.runLater(this::processQueue);
}
}
private void processQueue() {
Integer nextAmount = xpQueue.poll();
if (nextAmount != null) {
animateAdd(nextAmount);
} else {
animating.set(false);
}
}
public void animateAdd(int remaining) {
if (remaining == 0) {
processQueue();
return;
}
int current = currentXP.get();
int max = maxXP.get();
int toFill = max - current;
if (remaining >= toFill) {
Timeline t = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(progress, (double)current / max)),
new KeyFrame(Duration.seconds(0.25), new KeyValue(progress, 1))
);
t.setOnFinished(e -> {
currentXP.set(0);
levelUp();
animateAdd(remaining - toFill);
});
t.play();
} else {
double target = (double)(current + remaining) / max;
Timeline t = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(progress, (double)current / max)),
new KeyFrame(Duration.seconds(0.1), new KeyValue(progress, target))
);
t.setOnFinished(e -> {
currentXP.set(current + remaining);
updateLabel();
processQueue();
});
t.play();
}
}
private void levelUp() {
currentLevel++;
int old = maxXP.get();
if (currentLevel <= 100)
maxXP.set((int)(old + 100 - currentLevel * 0.5));
else
maxXP.set((int)(old + 9));
updateProgress();
}
public void setXP(int xp) {
currentLevel = 1;
maxXP.set(100);
currentXP.set(xp);
updateProgress();
}
public void setMaxXP(int max) {
maxXP.set(max);
updateProgress();
}
public double getProgress() {
return progress.get();
}
public DoubleProperty progressProperty() {
return progress;
}
public int getXP() {
return currentXP.get();
}
public int getMaxXP() {
return maxXP.get();
}
}