-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWaveProgressBar.java
More file actions
134 lines (111 loc) · 4.56 KB
/
Copy pathWaveProgressBar.java
File metadata and controls
134 lines (111 loc) · 4.56 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class manages and displays the progress bar
*/
import java.awt.*;
import java.util.*;
public class WaveProgressBar {
// Constants for the height of progress bar + margins around it
public static final int BAR_HEIGHT = 20;
public static final int TOP_MARGIN = 20;
public static final int SIDE_MARGIN = 40;
// Font used for displaying wave info
public final Font WAVE_FONT;
// Variables to track the current progress
public int curr_defeated;
public int enemy_req;
// Colors used for the progress bar's background, progress, border, and text
public final Color BACKGROUND_COLOR = new Color(60, 60, 60, 200);
public final Color PROGRESS_COLOR = new Color(140, 200, 255);
public final Color BORDER_COLOR = Color.WHITE;
public final Color TEXT_COLOR = Color.WHITE;
// List of waves + enemy counts
public java.util.List<Map<String, Integer>> waves;
public int current_wave;
// Constructor
public WaveProgressBar(Font font, GamePanel gamePanel) {
this.WAVE_FONT = font.deriveFont(18f);
this.curr_defeated = 0;
this.waves = gamePanel.waves;
current_wave = 0;
nextWave();
}
// Updates progress bar with how many enemies defeated
public void updateProgress(int defeated) {
this.curr_defeated = defeated;
}
// Move to next wave and count enemies in it
public void nextWave() {
Map<String, Integer> wave = null;
Set<Map.Entry<String, Integer>> entries = null;
Iterator<Map.Entry<String, Integer>> iter = null;
Map.Entry<String, Integer> entry = null;
current_wave++;
wave = waves.get(current_wave - 1);
enemy_req = 0;
entries = wave.entrySet();
iter = entries.iterator();
while (iter.hasNext()) {
entry = iter.next();
enemy_req += entry.getValue();
}
}
public void setCurrentWave(int wave) {
current_wave = wave - 1;
nextWave();
}
// Reset for new game
public void reset() {
curr_defeated = 0;
enemy_req = 0;
current_wave = 0;
nextWave();
}
// Draws progress bar on the screen
public void draw(Graphics2D g2d, int screenWidth) {
int barWidth = 0;
int barX = 0;
int barY = 0;
float progressPercentage = 0f;
int progressWidth = 0;
GradientPaint gp = null;
String waveText = null;
FontMetrics fm = null;
int textWidth = 0;
int textX = 0;
int textY = 0;
// Figure out how wide + where place bar
barWidth = screenWidth - (2 * SIDE_MARGIN);
barX = SIDE_MARGIN;
barY = TOP_MARGIN;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw the background rectangle for the progress bar
g2d.setColor(BACKGROUND_COLOR);
g2d.fillRoundRect(barX, barY, barWidth, BAR_HEIGHT, 10, 10);
// Calculate the progress percentage based on enemies slayed
progressPercentage = (float) curr_defeated / enemy_req;
progressPercentage = Math.min(1.0f, progressPercentage); // Ensure the percentage does not exceed 100%
progressWidth = (int) (barWidth * progressPercentage); // Calculate the width of the progress portion
// Draw the progress portion of the bar
gp = new GradientPaint(barX, barY, PROGRESS_COLOR.brighter(), barX, barY + BAR_HEIGHT, PROGRESS_COLOR.darker());
g2d.setPaint(gp);
g2d.fillRoundRect(barX, barY, progressWidth, BAR_HEIGHT, 10, 10);
// Draw border around the progress bar
g2d.setColor(BORDER_COLOR);
g2d.drawRoundRect(barX, barY, barWidth, BAR_HEIGHT, 10, 10);
// Create the text to display the wave and progress information
waveText = "Wave " + current_wave + " - " +
curr_defeated + " / " + enemy_req;
// Set the font and color for text
g2d.setFont(WAVE_FONT);
g2d.setColor(TEXT_COLOR);
// Center the text within the progress bar
fm = g2d.getFontMetrics(); // Get font metrics for text measurement
textWidth = fm.stringWidth(waveText); // Calculate the width of the text
textX = barX + (barWidth - textWidth) / 2; // X-coordinate to center the text
textY = barY + ((BAR_HEIGHT - fm.getHeight()) / 2) + fm.getAscent(); // Y-coordinate for vertical centering
// Draw the text on the progress bar
g2d.drawString(waveText, textX, textY);
}
}