-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayScreen.java
More file actions
577 lines (519 loc) · 20.8 KB
/
Copy pathPlayScreen.java
File metadata and controls
577 lines (519 loc) · 20.8 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class is the main playscreen for the game, managing game states and updating visuals
*/
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.Timer;
public class PlayScreen extends JPanel {
// Time pre-level
public static final int COUNTDOWN_SECONDS = 3;
public static final int UPGRADE_DELAY_MS = 500;
public static final String[] ALL_SKILLS = {
"luminous_pulse",
"light_lance",
"photon_orbs",
"starfall_ritual",
"angelic_summons"
};
public static final String[] SUPER_SKILLS = {
"solar_flare",
"prismatic_ray",
"celestial_constellation",
"archangel",
"cosmic_cataclysm"
};
// Game states
public enum State {
COUNTDOWN, PLAYING, UPGRADE
}
public State state = State.UPGRADE;
public GamePanel parent;
public Font uiFont;
public TexturePaint mapTexture;
// Countdown
public int countdown;
public Timer countdownTimer;
// Upgrade overlay
public JPanel upgradeOverlay;
public boolean upgradeScheduled = false;
// Skill icon cache
public final Map<String, ImageIcon> iconCache = new HashMap<>();
public final Random rand = new Random();
public final Set<String> acquiredSupers = new HashSet<>();
public PlayScreen(GamePanel parent) {
this.parent = parent;
this.uiFont = parent.getGameFont();
setOpaque(false);
setDoubleBuffered(true);
setLayout(null);
loadMapTexture();
preloadSkillIcons();
if (Settings.DEBUG_MODE) {
applyDebugSettings();
}
if (!DebugConfig.maxUpgrades)
SwingUtilities.invokeLater(this::showUpgradeMenu);
}
public void loadMapTexture() {
BufferedImage map = null;
Rectangle2D rect = null;
try {
map = ImageIO.read(getClass().getResource("/assets/images/map.png"));
rect = new Rectangle2D.Float(0, 0, map.getWidth(), map.getHeight());
mapTexture = new TexturePaint(map, rect);
} catch (IOException | IllegalArgumentException e) {
mapTexture = null;
}
}
public void preloadSkillIcons() {
String basePath = "/assets/images/bordered_sprites/";
int i = 0;
String skill = null;
for (i = 0; i < ALL_SKILLS.length; i++) {
skill = ALL_SKILLS[i];
loadIcon(skill, basePath + skill + ".png");
}
for (i = 0; i < SUPER_SKILLS.length; i++) {
skill = SUPER_SKILLS[i];
loadIcon(skill, basePath + skill + ".png");
}
}
private void loadIcon(String key, String path) {
BufferedImage img = null;
BufferedImage scaled = null;
Graphics2D g = null;
try {
// System.out.println("[Icon] Loading " + path);
img = ImageCache.load(path);
if (img == null)
throw new IOException("missing image");
scaled = new BufferedImage(120, 120, BufferedImage.TYPE_INT_ARGB);
g = scaled.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, 120, 120, null);
g.dispose();
iconCache.put(key, new ImageIcon(scaled));
} catch (Exception ex) {
System.err.println("Failed to load icon " + path + ": " + ex.getMessage());
iconCache.put(key, new ImageIcon());
}
}
/** Apply active debug settings to the player and wave. */
public void applyDebugSettings() {
int i = 0;
//reset skills to level 0
parent.player.getLuminousPulse().level = 0;
parent.player.getLightLance().level = 0;
parent.player.getPhotonOrbs().level = 0;
parent.player.getStarfallRitual().level = 0;
parent.player.getAngelicSummons().level = 0;
acquiredSupers.clear();
parent.setCurrentWave(DebugConfig.startWave);
if (DebugConfig.maxUpgrades) {
for (i = 0; i < 4; i++)
parent.player.getLuminousPulse().levelUp();
for (i = 0; i < 4; i++)
parent.player.getLightLance().levelUp();
for (i = 0; i < 4; i++)
parent.player.getPhotonOrbs().levelUp();
for (i = 0; i < 4; i++)
parent.player.getStarfallRitual().levelUp();
for (i = 0; i < 4; i++)
parent.player.getAngelicSummons().levelUp();
acquiredSupers.add("prismatic_ray");
acquiredSupers.add("celestial_constellation");
acquiredSupers.add("archangel");
acquiredSupers.add("cosmic_cataclysm");
// Skip the upgrade menu entirely when max upgrades are enabled
hideUpgradeOverlay();
scheduleCountdown();
}
}
public int getPlayerSkillLevel(String skill) {
return switch (skill) {
case "luminous_pulse" -> parent.player.getLuminousPulse().level;
case "light_lance" -> parent.player.getLightLance().level;
case "photon_orbs" -> parent.player.getPhotonOrbs().level;
case "starfall_ritual" -> parent.player.getStarfallRitual().level;
case "angelic_summons" -> parent.player.getAngelicSummons().level;
default -> 0;
};
}
public boolean isUpgradeActive() {
return state == State.UPGRADE;
}
public boolean isCountdownFinished() {
return state == State.PLAYING;
}
/**
* Returns true if all base skills have reached their maximum level.
* A super upgrade may push a skill beyond level 3, so we check for
* <code>>= 3</code> rather than strict equality. This prevents the upgrade
* menu from reverting to base upgrades after the first super upgrade.
*/
public boolean allBaseSkillsMaxed() {
return parent.player.getLuminousPulse().level >= 3 &&
parent.player.getLightLance().level >= 3 &&
parent.player.getPhotonOrbs().level >= 3 &&
parent.player.getAngelicSummons().level >= 3 &&
parent.player.getStarfallRitual().level >= 3;
}
public void applyUpgrade(String skill) {
switch (skill) {
case "luminous_pulse" -> parent.player.getLuminousPulse().levelUp();
case "light_lance" -> parent.player.getLightLance().levelUp();
case "photon_orbs" -> parent.player.getPhotonOrbs().levelUp();
case "starfall_ritual" -> parent.player.getStarfallRitual().levelUp();
case "angelic_summons" -> parent.player.getAngelicSummons().levelUp();
}
hideUpgradeOverlay();
scheduleCountdown();
}
public void applySuperUpgrade(String skill) {
switch (skill) {
case "solar_flare" -> parent.player.getLuminousPulse().levelUp();
case "prismatic_ray" -> parent.player.getLightLance().levelUp();
case "celestial_constellation" -> parent.player.getPhotonOrbs().levelUp();
case "archangel" -> parent.player.getAngelicSummons().levelUp();
case "cosmic_cataclysm" -> parent.player.getStarfallRitual().levelUp();
}
acquiredSupers.add(skill);
hideUpgradeOverlay();
scheduleCountdown();
}
// Hides the upgrade menu
public void hideUpgradeOverlay() {
if (upgradeOverlay != null)
upgradeOverlay.setVisible(false);
state = State.PLAYING;
}
/** Reset screen state when starting a new game. */
public void reset() {
if (countdownTimer != null) {
countdownTimer.stop();
countdownTimer = null;
}
upgradeScheduled = false;
acquiredSupers.clear();
state = State.UPGRADE;
if (upgradeOverlay != null) {
upgradeOverlay.setVisible(false);
upgradeOverlay.removeAll();
}
if (!DebugConfig.maxUpgrades)
SwingUtilities.invokeLater(this::showUpgradeMenu);
}
public void showUpgradeMenu() {
JLabel title;
boolean superMode;
List<String> available;
List<String> chosen;
String s = null;
int idx = 0;
JPanel grid;
JPanel content;
upgradeScheduled = false;
state = State.UPGRADE;
parent.player.getPhotonOrbs().clearOrbs();
parent.player.getAngelicSummons().clearAngels();
// Overlay panel if needed
if (upgradeOverlay == null) {
upgradeOverlay = new JPanel(new BorderLayout()) {
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(new Color(0, 0, 0, 160));
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
super.paintComponent(g);
}
};
upgradeOverlay.setOpaque(false);
add(upgradeOverlay);
}
upgradeOverlay.removeAll();
title = new JLabel(
parent.getCurrentWave() == 1 ? "Choose Your Starting Skill" : "Choose an Upgrade",
SwingConstants.CENTER);
title.setFont(uiFont.deriveFont(Font.BOLD, 38f));
title.setForeground(Color.WHITE);
title.setBorder(BorderFactory.createEmptyBorder(30, 0, 20, 0));
superMode = allBaseSkillsMaxed() && acquiredSupers.size() < SUPER_SKILLS.length;
available = new ArrayList<>(Arrays.asList(superMode ? SUPER_SKILLS : ALL_SKILLS));
if (superMode) {
available.removeAll(acquiredSupers);
}
Collections.shuffle(available, rand);
chosen = new ArrayList<>();
s = null;
idx = 0;
for (idx = 0; idx < available.size(); idx++) {
s = available.get(idx);
if (superMode) {
if (chosen.size() < 3)
chosen.add(s);
} else if (getPlayerSkillLevel(s) < 3 && chosen.size() < 3) {
chosen.add(s);
}
}
if (chosen.isEmpty()) {
new Timer(UPGRADE_DELAY_MS, e -> {
((Timer) e.getSource()).stop();
scheduleCountdown();
}).start();
return;
}
grid = new JPanel(new GridLayout(1, chosen.size(), 30, 0));
grid.setOpaque(false);
grid.setBorder(BorderFactory.createEmptyBorder(10, 30, 30, 30));
for (idx = 0; idx < chosen.size(); idx++) {
s = chosen.get(idx);
grid.add(buildCard(s));
}
content = new JPanel(new BorderLayout());
content.setOpaque(false);
content.add(title, BorderLayout.NORTH);
content.add(grid, BorderLayout.CENTER);
upgradeOverlay.add(content, BorderLayout.CENTER);
upgradeOverlay.setBounds(0, 0, getWidth(), getHeight());
upgradeOverlay.setVisible(true);
repaint();
}
public JPanel buildCard(String skill) {
JPanel card;
JLabel icon;
boolean isSuper;
int lvl;
JLabel name;
JLabel levelLabel;
JPanel titlePanel;
JTextArea desc;
JScrollPane scroll;
JPanel center;
JButton btn;
card = new JPanel(new BorderLayout(0, 8));
card.setBackground(new Color(35, 35, 55));
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(100, 100, 150), 2),
BorderFactory.createEmptyBorder(12, 12, 12, 12)));
icon = new JLabel(iconCache.get(skill));
icon.setHorizontalAlignment(SwingConstants.CENTER);
icon.setPreferredSize(new Dimension(120, 120));
card.add(icon, BorderLayout.NORTH);
isSuper = Arrays.asList(SUPER_SKILLS).contains(skill);
lvl = isSuper ? 4 : getPlayerSkillLevel(skill);
name = new JLabel(format(skill), SwingConstants.CENTER);
name.setFont(uiFont.deriveFont(Font.BOLD, 18f));
name.setForeground(Color.WHITE);
levelLabel = new JLabel(isSuper ? "Super" : (lvl > 0 ? "Level " + lvl : "New Skill"),
SwingConstants.CENTER);
levelLabel.setFont(uiFont.deriveFont(14f));
levelLabel.setForeground(new Color(255, 215, 0));
titlePanel = new JPanel(new GridLayout(2, 1, 0, 2));
titlePanel.setOpaque(false);
titlePanel.add(name);
titlePanel.add(levelLabel);
desc = new JTextArea(getUpgradeDescription(skill));
desc.setWrapStyleWord(true);
desc.setLineWrap(true);
desc.setOpaque(false);
desc.setForeground(new Color(200, 200, 255));
desc.setFont(uiFont.deriveFont(13f));
desc.setEditable(false);
desc.setFocusable(false);
desc.setBorder(null);
scroll = new JScrollPane(desc);
scroll.setOpaque(false);
scroll.getViewport().setOpaque(false);
scroll.setBorder(null);
scroll.setPreferredSize(new Dimension(0, 70));
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
center = new JPanel(new BorderLayout(0, 6));
center.setOpaque(false);
center.add(titlePanel, BorderLayout.NORTH);
center.add(scroll, BorderLayout.CENTER);
card.add(center, BorderLayout.CENTER);
btn = new JButton(isSuper ? "Select" : (lvl >= 3 ? "Maxed" : "Select"));
btn.setEnabled(isSuper || lvl < 3);
btn.setFont(uiFont.deriveFont(Font.BOLD, 14f));
btn.setBackground(new Color(80, 80, 120));
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
btn.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
if (isSuper) {
btn.addActionListener(e -> applySuperUpgrade(skill));
} else {
btn.addActionListener(e -> applyUpgrade(skill));
}
card.add(btn, BorderLayout.SOUTH);
return card;
}
public String format(String id) {
StringBuilder sb = new StringBuilder();
String[] parts = id.split("_");
int i = 0;
String w = null;
for (i = 0; i < parts.length; i++) {
w = parts[i];
if (!w.isEmpty())
sb.append(Character.toUpperCase(w.charAt(0))).append(w.substring(1)).append(' ');
}
return sb.toString().trim();
}
public String getUpgradeDescription(String skill) {
boolean isSuper = Arrays.asList(SUPER_SKILLS).contains(skill);
int lvl = isSuper ? 4 : getPlayerSkillLevel(skill);
if (isSuper) {
return switch (skill) {
case "solar_flare" -> "Emit a blinding flash that damages and stuns all enemies.";
case "prismatic_ray" -> "Automatically fire a splitting rainbow beam toward your cursor.";
case "celestial_constellation" -> "Spawn homing orbs that explode on impact.";
case "archangel" -> "Summon an archangel ally wielding beams and an aura.";
case "cosmic_cataclysm" -> "Unleash a meteor storm at your cursor every few seconds.";
default -> "A powerful new ability.";
};
} else if (lvl == 0) {
return switch (skill) {
case "luminous_pulse" -> "Unlock a radiant aura that damages nearby enemies.";
case "light_lance" -> "Unlock a powerful beam attack that pierces enemies.";
case "photon_orbs" -> "Unlock orbiting spheres of light attacking nearby foes.";
case "starfall_ritual" -> "Call down celestial projectiles from the heavens.";
case "angelic_summons" -> "Summon angelic allies to fight alongside you.";
default -> "A new power awaits you.";
};
} else {
return switch (skill) {
case "luminous_pulse" -> "Increase your aura's radius and damage.";
case "light_lance" -> "Enhance beam damage and width.";
case "photon_orbs" -> "Empower orbs with faster attacks.";
case "starfall_ritual" -> "Call down more projectiles with higher damage.";
case "angelic_summons" -> "Summon stronger angels to aid you.";
default -> "An upgrade to bolster your abilities.";
};
}
}
public void scheduleCountdown() {
state = State.COUNTDOWN;
countdown = COUNTDOWN_SECONDS;
if (countdownTimer != null)
countdownTimer.stop();
countdownTimer = new Timer(1000, e -> {
countdown--;
repaint();
if (countdown <= 0) {
countdownTimer.stop();
SwingUtilities.invokeLater(() -> {
state = State.PLAYING;
parent.player.setInvulnerable(true);
parent.player.getAngelicSummons().regenerateAngels();
parent.spawnWave();
upgradeScheduled = false;
if (parent.getCurrentWave() >= 21) {
parent.playMusic("boss");
} else {
parent.playMusic("gameplay");
}
repaint();
});
}
});
countdownTimer.start();
}
// Check all enemies are dead and wave over
public void checkWaveCompletion() {
if (state == State.PLAYING && parent.enemies.isEmpty() && !upgradeScheduled) {
upgradeScheduled = true;
parent.player.getPhotonOrbs().clearOrbs();
parent.player.getAngelicSummons().clearAngels();
new Timer(UPGRADE_DELAY_MS, e -> {
int nextWave = parent.getCurrentWave();
((Timer) e.getSource()).stop();
if (nextWave > parent.waves.size()) {
SwingUtilities.invokeLater(() -> parent.showScreen("GAME_WON"));
} else if (nextWave >= 21) {
scheduleCountdown();
} else if (!DebugConfig.maxUpgrades) {
showUpgradeMenu();
} else {
scheduleCountdown();
}
}).start();
}
}
// Check if player is dead and show game over screen
public void checkGameOver() {
if (parent.player.getHp() <= 0) {
if (Settings.DEBUG_MODE && DebugConfig.retryWave) {
parent.retryCurrentWave();
} else {
SwingUtilities.invokeLater(() -> parent.showScreen("GAME_OVER"));
}
}
}
// Draw beautiful, magnificent, one-of-a-kind visuals
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
Angel angelTemp = null;
Enemy enemyTemp = null;
int i = 0;
String t = null;
FontMetrics fm;
int w = 0;
int h = 0;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());
if (mapTexture != null) {
g2.setPaint(mapTexture);
g2.fillRect(0, 0, getWidth(), getHeight());
}
switch (state) {
case COUNTDOWN -> {
g2.setFont(uiFont.deriveFont(72f));
g2.setColor(Color.WHITE);
t = countdown > 0 ? String.valueOf(countdown) : "GO!";
fm = g2.getFontMetrics();
w = fm.stringWidth(t);
h = fm.getAscent();
g2.drawString(t, (getWidth() - w) / 2, (getHeight() + h) / 2);
}
case PLAYING -> {
parent.player.draw(g2);
for (i = 0; i < parent.angels.size(); i++) {
angelTemp = parent.angels.get(i);
angelTemp.draw(g2);
}
for (i = 0; i < parent.enemies.size(); i++) {
enemyTemp = parent.enemies.get(i);
enemyTemp.draw(g2);
}
parent.hud.draw(g2, getWidth());
checkWaveCompletion();
checkGameOver();
}
case UPGRADE -> {
/* overlay paints itself */ }
}
if (state == State.UPGRADE && upgradeOverlay != null) {
upgradeOverlay.setVisible(true);
setComponentZOrder(upgradeOverlay, 0);
}
g2.dispose();
}
// Ensured upgrade screen fits screen size
@Override
public void doLayout() {
super.doLayout();
if (upgradeOverlay != null)
upgradeOverlay.setBounds(0, 0, getWidth(), getHeight());
}
}