-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStarfallRitual.java
More file actions
364 lines (294 loc) · 11.4 KB
/
Copy pathStarfallRitual.java
File metadata and controls
364 lines (294 loc) · 11.4 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class handles the Starfall Ritual skill visuals and damage
*/
import java.awt.*;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
public class StarfallRitual extends Skill {
// Colours
public static final Color STAR_CORE_COLOR = new Color(255, 255, 200, 255);
public static final Color STAR_GLOW_COLOR = new Color(200, 200, 255, 180);
public static final Color IMPACT_COLOR = new Color(230, 240, 255, 220);
// Timer for firing spacing
public float fireTimer = 0;
// Random number gen for effects
public final Random rng = new Random();
// Projectiles fired per attack
public int projectileCount = 4;
public CosmicCataclysm cosmicCataclysm;
public final List<StarProjectile> projectiles = new CopyOnWriteArrayList<>();
public final List<ImpactEffect> impactEffects = new CopyOnWriteArrayList<>();
// Pool of reusable projectiles to avoid frequent allocations
public final List<StarProjectile> projectilePool = new CopyOnWriteArrayList<>();
// Skill properties
public int damage;
public float cooldown;
public float radius;
public float speed;
public int level = 0;
// Constructor
public StarfallRitual(Player owner) {
super("starfall_ritual", owner);
}
// Skill upgrade to next level
public void levelUp() {
level++;
SkillLevel data = levels.get(level - 1);
this.damage = data.getDamage();
this.cooldown = data.getCooldown() / 1000.0f;
this.projectileCount = data.getCount();
this.radius = 8;
this.speed = 3;
if (level >= 4) {
if (cosmicCataclysm == null) {
cosmicCataclysm = new CosmicCataclysm(owner.parent, damage);
}
cosmicCataclysm.setDamage(damage);
}
}
// Continuously called to update skill
@Override
public void update(float dt, List<Enemy> enemies) {
if (level == 0) {
return;
}
fireTimer += dt;
if (fireTimer >= cooldown) {
fireProjectiles();
fireTimer = 0;
}
if (level >= 4 && cosmicCataclysm != null) {
cosmicCataclysm.update(dt);
}
updateProjectiles(dt, enemies);
updateEffects(dt);
cleanupEffects();
}
// Projectiles fired in circular spread
public void fireProjectiles() {
int i = 0;
double angle = 0.0;
float offsetX = 0f;
float offsetY = 0f;
StarProjectile p = null;
for (i = 0; i < projectileCount; i++) {
angle = 2 * Math.PI * i / projectileCount;
offsetX = (float) (Math.cos(angle) * 30);
offsetY = (float) (Math.sin(angle) * 30);
p = obtainProjectile(
(float) (owner.getCenterX() + offsetX),
(float) (owner.getCenterY() + offsetY),
angle);
projectiles.add(p);
}
}
/**
* Obtains a projectile from the pool or creates a new one.
*/
public StarProjectile obtainProjectile(float x, float y, double angle) {
if (!projectilePool.isEmpty()) {
StarProjectile p = projectilePool.remove(projectilePool.size() - 1);
p.reset(x, y, angle, speed);
return p;
}
return new StarProjectile(x, y, angle, speed);
}
public void updateProjectiles(float dt, List<Enemy> enemies) {
List<StarProjectile> toRemove = new java.util.ArrayList<>();
int projIndex = 0;
StarProjectile projectile = null;
for (projIndex = 0; projIndex < projectiles.size(); projIndex++) {
projectile = projectiles.get(projIndex);
projectile.update(dt);
boolean remove = checkCollisions(projectile, enemies);
if (!remove && isOutOfBounds(projectile)) {
remove = true;
}
if (remove) {
toRemove.add(projectile);
}
}
for (projIndex = 0; projIndex < toRemove.size(); projIndex++) {
projectile = toRemove.get(projIndex);
projectiles.remove(projectile);
projectilePool.add(projectile);
}
}
// Check if projectile has left screen
public boolean isOutOfBounds(StarProjectile projectile) {
return projectile.x < -radius ||
projectile.x > GamePanel.GAME_WIDTH + radius ||
projectile.y < -radius ||
projectile.y > GamePanel.GAME_HEIGHT + radius;
}
public boolean checkCollisions(StarProjectile projectile, List<Enemy> enemies) {
int enemyIndex = 0;
Enemy enemy = null;
for (enemyIndex = 0; enemyIndex < enemies.size(); enemyIndex++) {
enemy = enemies.get(enemyIndex);
if (enemy.isDead() || enemy.isIntangible())
continue;
float distX = (float) (projectile.x - enemy.getCenterX());
float distY = (float) (projectile.y - enemy.getCenterY());
float distance = (float) Math.sqrt(distX * distX + distY * distY);
if (distance < radius + enemy.getWidth() / 2) {
enemy.takeDamage(damage);
createImpactEffect(projectile.x, projectile.y);
float knockbackStrength = 3.0f;
float knockbackX = distX / distance * knockbackStrength;
float knockbackY = distY / distance * knockbackStrength;
enemy.applyKnockback(knockbackX, knockbackY);
return true;
}
}
return false;
}
// Sparkle effect when projectiles hit
public void createImpactEffect(float x, float y) {
impactEffects.add(new ImpactEffect(x, y));
}
// Update sparkle and impact effects
public void updateEffects(float dt) {
int impactIndex = 0;
ImpactEffect impact = null;
for (impactIndex = 0; impactIndex < impactEffects.size(); impactIndex++) {
impact = impactEffects.get(impactIndex);
impact.update(dt);
}
}
// Remove effects
public void cleanupEffects() {
impactEffects.removeIf(impact -> impact.isDone());
}
// Drawing visuals
@Override
public void draw(Graphics2D g2d) {
int projIndex = 0;
StarProjectile projectile = null;
int impactIndex = 0;
ImpactEffect impact = null;
for (projIndex = 0; projIndex < projectiles.size(); projIndex++) {
projectile = projectiles.get(projIndex);
projectile.draw(g2d);
}
for (impactIndex = 0; impactIndex < impactEffects.size(); impactIndex++) {
impact = impactEffects.get(impactIndex);
impact.draw(g2d);
}
if (level >= 4 && cosmicCataclysm != null) {
cosmicCataclysm.draw(g2d);
}
}
// Represents each star projectile
public class StarProjectile {
float x, y;
float velocityX, velocityY;
// Constructor
public StarProjectile(float x, float y, double angle, float speed) {
this.x = x;
this.y = y;
this.velocityX = (float) (Math.cos(angle) * speed);
this.velocityY = (float) (Math.sin(angle) * speed);
}
public void reset(float x, float y, double angle, float speed) {
this.x = x;
this.y = y;
this.velocityX = (float) (Math.cos(angle) * speed);
this.velocityY = (float) (Math.sin(angle) * speed);
}
public void update(float dt) {
x += velocityX * dt * 60;
y += velocityY * dt * 60;
}
// Draw
public void draw(Graphics2D g2d) {
int size = (int) (radius * 2);
// Glow
g2d.setColor(STAR_GLOW_COLOR);
g2d.fillOval((int) (x - radius * 1.5), (int) (y - radius * 1.5),
(int) (size * 1.5), (int) (size * 1.5));
// Core
g2d.setColor(STAR_CORE_COLOR);
g2d.fillOval((int) (x - radius), (int) (y - radius), size, size);
// White centre
g2d.setColor(Color.WHITE);
int centerSize = (int) (radius * 0.6);
g2d.fillOval((int) (x - centerSize / 2), (int) (y - centerSize / 2), centerSize, centerSize);
// Sparkle
drawSparkle(g2d, x, y, size);
}
// Draw sparkles
public void drawSparkle(Graphics2D g2d, float x, float y, int size) {
Composite originalComposite = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
g2d.setColor(Color.WHITE);
int sparkleSize = size;
g2d.drawLine((int) (x - sparkleSize), (int) y, (int) (x + sparkleSize), (int) y);
g2d.drawLine((int) x, (int) (y - sparkleSize), (int) x, (int) (y + sparkleSize));
int diagSize = sparkleSize / 2;
g2d.drawLine((int) (x - diagSize), (int) (y - diagSize), (int) (x + diagSize), (int) (y + diagSize));
g2d.drawLine((int) (x - diagSize), (int) (y + diagSize), (int) (x + diagSize), (int) (y - diagSize));
g2d.setComposite(originalComposite);
}
}
// Represents each small explosion impact
public class ImpactEffect {
float x, y;
int frame = 0;
static final int TOTAL_FRAMES = 8;
float size = radius * 3;
// Constructor
public ImpactEffect(float x, float y) {
this.x = x;
this.y = y;
}
// Update frame
public void update(float dt) {
frame++;
}
// Check if explosion complete
public boolean isDone() {
return frame >= TOTAL_FRAMES;
}
// Draw visuals
public void draw(Graphics2D g2d) {
Composite originalComposite = null;
float alpha = 0f;
float currentSize = 0f;
int i = 0;
double angle = 0.0;
float particleX = 0f;
float particleY = 0f;
float particleSize = 0f;
originalComposite = g2d.getComposite();
alpha = 1.0f - (float) frame / TOTAL_FRAMES;
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
currentSize = size * (1.0f + frame * 0.3f);
g2d.setColor(IMPACT_COLOR);
g2d.setStroke(new BasicStroke(3f * (1.0f - alpha)));
g2d.drawOval(
(int) (x - currentSize / 2),
(int) (y - currentSize / 2),
(int) currentSize,
(int) currentSize);
if (frame < TOTAL_FRAMES / 2) {
for (i = 0; i < 4; i++) {
angle = i * Math.PI / 2 + frame * 0.2;
particleX = x + (float) (Math.cos(angle) * currentSize * 0.5f);
particleY = y + (float) (Math.sin(angle) * currentSize * 0.5f);
particleSize = 5f * (1.0f - alpha);
g2d.setColor(Color.WHITE);
g2d.fillOval(
(int) (particleX - particleSize / 2),
(int) (particleY - particleSize / 2),
(int) particleSize,
(int) particleSize);
}
}
g2d.setComposite(originalComposite);
}
}
}