-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCosmicCataclysm.java
More file actions
225 lines (189 loc) · 6.81 KB
/
Copy pathCosmicCataclysm.java
File metadata and controls
225 lines (189 loc) · 6.81 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class represents the super skill Cosmic Cataclysm, managing visuals and damager for meteors that
* shoot toward the players mouse
*/
import java.awt.*;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
public class CosmicCataclysm {
// Time between use
public static final float COOLDOWN = 4f;
// # of meteors chosen per storm
public static final int MIN_METEORS = 20;
public static final int MAX_METEORS = 35;
// How wide area in which meteors can land
public static final float ZONE_HALF_SIZE = 250f;
// Min or max speed of falling meteors
public static final float MIN_SPEED = 720f; // px/s
public static final float MAX_SPEED = 1200f; // px/s
// Where meteors can deal damage
public static final float IMPACT_RADIUS = 90f;
// Enemy knockback
public static final float KNOCKBACK = 7f;
// Gives access to mouse + enemy location
public GamePanel panel;
// List of meteors
public CopyOnWriteArrayList<Meteor> meteors = new CopyOnWriteArrayList<>();
// Track next meteor
public float cooldownTimer = 0f;
// Check if currently showering meteors
public boolean active = false;
// Damage each meteor deals
public int damage;
// Constructor
public CosmicCataclysm(GamePanel panel, int damage) {
this.panel = panel;
this.damage = damage;
}
// Change damage amount
public void setDamage(int dmg) {
this.damage = dmg;
}
// Continuously call to update state of meteors
public void update(float dt) {
int i = 0;
Meteor m = null;
// If ability not active, start countdown
if (!active) {
cooldownTimer += dt;
// Create new meteor storm when cooldown over
if (cooldownTimer >= COOLDOWN) {
cooldownTimer -= COOLDOWN;
spawnStorm();
}
}
// Update meteros
for (i = meteors.size() - 1; i >= 0; i--) {
m = meteors.get(i);
m.update(dt);
if (m.finished()) {
meteors.remove(i);
}
}
// If meteors gone, end storm
if (active && meteors.isEmpty()) {
active = false;
}
}
// Draw meteors to screen
public void draw(Graphics2D g) {
Meteor m = null;
int i = 0;
for (i = 0; i < meteors.size(); i++) {
m = meteors.get(i);
m.draw(g);
}
}
// Create random # meteors that fall toward mouse
public void spawnStorm() {
Point p = null;
float cx = 0f;
float cy = 0f;
int count = 0;
int i = 0;
float tx = 0f;
float ty = 0f;
float speed = 0f;
p = panel.mousePos;
cx = p.x;
cy = p.y;
// Randomize # meteors
count = ThreadLocalRandom.current().nextInt(MIN_METEORS, MAX_METEORS + 1);
// Random position within mouse area
for (i = 0; i < count; i++) {
tx = cx + ThreadLocalRandom.current().nextFloat() * ZONE_HALF_SIZE * 2 - ZONE_HALF_SIZE;
ty = cy + ThreadLocalRandom.current().nextFloat() * ZONE_HALF_SIZE * 2 - ZONE_HALF_SIZE;
// Random speed for falling meteors
speed = MIN_SPEED + ThreadLocalRandom.current().nextFloat() * (MAX_SPEED - MIN_SPEED);
// Create and add meteor to list
meteors.add(new Meteor(tx, ty, speed));
}
active = true;
}
// Class that represents individual meteor
public class Meteor {
private float x, y;
private float targetX, targetY; // Where meteor is to hit
private float vy; //Vertical speed
private boolean impacting = false; // is the meteor going KABOOM?
private float impactAlpha = 1f; // Used to fade kaboom
// Construct meteor with properties
public Meteor(float targetX, float targetY, float speed) {
this.targetX = targetX;
this.targetY = targetY;
this.x = targetX;
this.y = -300f;
this.vy = speed;
}
// Continously update meteor movement and action
public void update(float dt) {
if (impacting) {
impactAlpha -= dt * 2f;
return;
}
y += vy * dt;
// When hits target y, EXPLODE!
if (y >= targetY) {
impact();
}
}
// Handles meteor explosion
public void impact() {
Enemy eTemp = null;
float dx = 0f;
float dy = 0f;
float distSq = 0f;
float dist = 0f;
int i = 0;
impacting = true;
// Damage all enemies in impact area
for (i = 0; i < panel.enemies.size(); i++) {
eTemp = panel.enemies.get(i);
if (eTemp.isDead())
continue;
dx = (float) eTemp.getCenterX() - targetX;
dy = (float) eTemp.getCenterY() - targetY;
distSq = dx * dx + dy * dy;
// Check if can hit enemy
if (distSq <= IMPACT_RADIUS * IMPACT_RADIUS) {
eTemp.takeDamage(damage);
// Knockback
dist = (float) Math.sqrt(distSq);
if (dist == 0f)
dist = 1f;
eTemp.applyKnockback(dx / dist * KNOCKBACK, dy / dist * KNOCKBACK);
}
}
}
// Check if explosion done
public boolean finished() {
return impacting && impactAlpha <= 0f;
}
// Draw meteor
public void draw(Graphics2D g) {
if (!impacting) {
// Glowing line with a dot
g.setColor(new Color(255, 230, 200));
g.drawLine((int) x, (int) y - 10, (int) x, (int) y + 4);
g.fillOval((int) x - 3, (int) y - 3, 6, 6);
} else {
// Circular explosion and gradient
float progress = 1f - impactAlpha;
int r = (int) (IMPACT_RADIUS * (0.6f + progress));
int a = (int) (impactAlpha * 255);
RadialGradientPaint paint = new RadialGradientPaint(
new Point2D.Float(targetX, targetY), r,
new float[] {0f, 1f},
new Color[] { new Color(255, 240, 200, a), new Color(255, 120, 80, 0)});
Paint old = g.getPaint();
g.setPaint(paint);
g.fillOval((int) (targetX - r), (int) (targetY - r), r * 2, r * 2);
g.setPaint(old);
}
}
}
}