-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDamageNumber.java
More file actions
118 lines (101 loc) · 3.63 KB
/
Copy pathDamageNumber.java
File metadata and controls
118 lines (101 loc) · 3.63 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class is used to show the numbers representing damage taken when enemies are hit
*/
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.Random;
public class DamageNumber {
// visual constants
public static final Color TEXT_COLOR = new Color(255, 80, 120); // Brighter, more saturated red
public static final Color GLOW_COLOR = new Color(255, 200, 200, 80); // Additional glow
public static final Color SHADOW_COLOR = new Color(40, 10, 30, 200); // Darker shadow for contrast
public static final int LIFE_SPAN = 60; // Increased lifespan for better visibility
public static final Random RNG = new Random();
// Instance variables
public double x, y;
public int damage;
public int life = LIFE_SPAN;
public double initialY;
/**
* Constructor for new damage number at the specified position
*/
public DamageNumber(int x, int y, int damage) {
this.x = x;
this.y = this.initialY = y;
this.damage = damage;
}
/**
* Updates the damage number's position and lifetime with floating wavy fx
*/
public void update(float dt) {
float progress = 1f - (life / (float) LIFE_SPAN);
y = initialY - (80 * progress + 20 * Math.sin(Math.PI * progress));
life--;
}
/**
* Checks if the damage number should be removed
*/
public boolean isDead() {
return life <= 0;
}
/**
* Draws the damage number with visual effects
*/
public void draw(Graphics2D g) {
float lifeRatio = 0f;
float alpha = 0f;
float scale = 0f;
String text = null;
Font originalFont = null;
Font font = null;
FontMetrics metrics = null;
int textWidth = 0;
int textX = 0;
int textY = 0;
Composite originalComposite = null;
AffineTransform originalTransform = null;
int i = 0;
// Fade out near life end
lifeRatio = life / (float) LIFE_SPAN;
alpha = lifeRatio > 0.3f ? 1f : (lifeRatio / 0.3f);
// Scaling with bounce effect
scale = 1.2f + 0.8f * (float) Math.sin(Math.PI * (1 - lifeRatio));
text = String.valueOf(damage);
//Font
originalFont = g.getFont();
font = originalFont.deriveFont(Font.BOLD, 12f * scale);
g.setFont(font);
// Figure out text positioning
metrics = g.getFontMetrics();
textWidth = metrics.stringWidth(text);
textX = (int) x - textWidth / 2;
textY = (int) y;
// Save the original transform and composite
originalComposite = g.getComposite();
originalTransform = g.getTransform();
// Add glow effect
g.setComposite(AlphaComposite.SrcOver.derive(alpha * 0.6f));
g.setColor(GLOW_COLOR);
for (i = 1; i <= 3; i++) {
g.drawString(text, textX - i, textY - i);
g.drawString(text, textX + i, textY - i);
g.drawString(text, textX - i, textY + i);
g.drawString(text, textX + i, textY + i);
}
// Add shadow effect
g.setComposite(AlphaComposite.SrcOver.derive(alpha * 0.8f));
g.setColor(SHADOW_COLOR);
for (i = 1; i <= 2; i++) {
g.drawString(text, textX + i, textY + i);
}
g.setComposite(AlphaComposite.SrcOver.derive(alpha));
g.setColor(TEXT_COLOR);
g.drawString(text, textX, textY);
// Restore original settings
g.setFont(originalFont);
g.setComposite(originalComposite);
g.setTransform(originalTransform);
}
}