-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLightLance.java
More file actions
536 lines (465 loc) · 17.4 KB
/
Copy pathLightLance.java
File metadata and controls
536 lines (465 loc) · 17.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
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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class implements the Light Lance super skill, firing a high damage laser targeting the nearest enemy.
* It also includes the implementation of the Prismatic Ray super skill, which splits a light lance into 5 multicolour rays
*/
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
public class LightLance extends Skill {
public static final float BEAM_LENGTH = 1500f;
public static final Color CORE_COLOR = new Color(255, 255, 255, 220);
public static final Color MAIN_COLOR = new Color(120, 180, 255, 180);
public static final Color GLOW_COLOR = new Color(100, 150, 255, 100);
public static final Color CHARGE_COLOR = new Color(200, 220, 255);
public static final float CHARGE_TIME = 0.25f;
public static final float BASE_DURATION = 0.3f;
public int damage;
public float width;
public int cooldown;
public float duration;
public int level = 0;
// List of impact effects
public CopyOnWriteArrayList<ImpactEffect> impacts = new CopyOnWriteArrayList<>();
public boolean firing = false;
public float beamProg = 0f;
public Point2D.Float beamStart = new Point2D.Float();
public Point2D.Float beamDir = new Point2D.Float();
public long lastFireTime = 0L;
public Random rng = new Random();
public Line2D.Float beamLine = new Line2D.Float();
public List<Enemy> hitEnemies = new CopyOnWriteArrayList<>();
private java.util.Map<Integer, BasicStroke> strokeCache = new java.util.HashMap<>();
public boolean isPrismatic = false;
public boolean splitActive = false;
public Line2D.Float[] splitLines = new Line2D.Float[5];
public Color[] splitColors = {
Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, new Color(180, 80, 255)
};
// Pre-cached colors used during rendering
private static final Color TRAIL_COLOR = new Color(100, 180, 255, 100);
private static final Color MID_GLOW_COLOR = new Color(120, 190, 255, 180);
private static final Color WHITE_180 = new Color(255, 255, 255, 180);
private static final Color WHITE_240 = new Color(255, 255, 255, 240);
// Stroke caches keyed by quantized width
private Map<Integer, BasicStroke> roundStrokeCache = new HashMap<>();
private Map<Integer, BasicStroke> defaultStrokeCache = new HashMap<>();
public final float SPLIT_RANGE = BEAM_LENGTH * 0.6f;
public float impactX, impactY;
public LightLance(Player owner) {
super("light_lance", owner);
int i = 0;
for (i = 0; i < splitLines.length; i++) {
splitLines[i] = new Line2D.Float();
}
}
private BasicStroke getStroke(float width) {
int key = Math.round(width * 10f);
BasicStroke s = strokeCache.get(key);
if (s == null) {
s = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
strokeCache.put(key, s);
}
return s;
}
@Override
public void levelUp() {
SkillLevel data;
level++;
data = levels.get(level - 1);
this.damage = data.getDamage();
this.width = data.getWidth();
this.cooldown = data.getCooldown();
this.duration = BASE_DURATION + 0.1f * (level - 1);
this.isPrismatic = data.isPrismatic();
}
@Override
public void update(float dt, List<Enemy> enemies) {
long now = System.currentTimeMillis();
Enemy target = null;
float aimAngle = 0f;
double dx = 0.0;
double dy = 0.0;
float cosA = 0f;
float sinA = 0f;
if (level == 0)
return;
if (!firing && now - lastFireTime >= cooldown) {
// Compute aim direction: toward closest enemy or default rightward
target = findClosestEnemy(enemies);
if (target != null) {
dx = target.getCenterX() - owner.getCenterX();
dy = target.getCenterY() - owner.getCenterY();
aimAngle = (float) Math.atan2(dy, dx);
} else {
aimAngle = 0f;
}
cosA = (float) Math.cos(aimAngle);
sinA = (float) Math.sin(aimAngle);
// Set beam start and direction
beamStart.setLocation(owner.getCenterX(), owner.getCenterY());
beamDir.setLocation(cosA, sinA);
fireBeam(enemies, cosA, sinA);
lastFireTime = now;
beamProg = 0f;
firing = true;
}
if (firing) {
beamProg += dt / duration;
if (beamProg >= 1f) {
firing = false;
// only turn off splits if you’re *not* prismatic
if (!isPrismatic)
splitActive = false;
}
}
updateImpacts(dt);
}
public Enemy findClosestEnemy(List<Enemy> enemies) {
Enemy closest = null;
double bestDistSq = Double.MAX_VALUE;
double cx = 0.0;
double cy = 0.0;
int i = 0;
Enemy e = null;
double dx = 0.0;
double dy = 0.0;
double distSq = 0.0;
cx = owner.getCenterX();
cy = owner.getCenterY();
for (i = 0; i < enemies.size(); i++) {
e = enemies.get(i);
if (e.isDead())
continue;
dx = e.getCenterX() - cx;
dy = e.getCenterY() - cy;
distSq = dx * dx + dy * dy;
if (distSq < bestDistSq) {
bestDistSq = distSq;
closest = e;
}
}
return closest;
}
public void fireBeam(List<Enemy> enemies, float cosA, float sinA) {
float sx;
float sy;
float ex;
float ey;
boolean foundImpact = false;
float firstProj = Float.MAX_VALUE;
float halfW;
float halfWSq;
int i = 0;
Enemy enemy = null;
float dx = 0f;
float dy = 0f;
float proj = 0f;
float perp = 0f;
float ix = 0f;
float iy = 0f;
sx = (float) owner.getCenterX();
sy = (float) owner.getCenterY();
ex = sx + BEAM_LENGTH * cosA;
ey = sy + BEAM_LENGTH * sinA;
beamLine.setLine(sx, sy, ex, ey);
hitEnemies.clear();
halfW = width / 2f;
halfWSq = halfW * halfW;
for (i = 0; i < enemies.size(); i++) {
enemy = enemies.get(i);
if (enemy.isDead() || enemy.isIntangible())
continue;
dx = (float) (enemy.getCenterX() - sx);
dy = (float) (enemy.getCenterY() - sy);
proj = dx * cosA + dy * sinA;
if (proj < 0 || proj > BEAM_LENGTH)
continue;
perp = -sinA * dx + cosA * dy;
if (perp * perp > halfWSq)
continue;
ix = sx + proj * cosA;
iy = sy + proj * sinA;
enemy.takeDamage(damage);
hitEnemies.add(enemy);
impacts.add(new ImpactEffect(ix, iy));
if (proj < firstProj) {
firstProj = proj;
impactX = ix;
impactY = iy;
foundImpact = true;
}
}
// 2) Only spawn split beams if prismatic AND we actually hit someone
if (isPrismatic && foundImpact) {
splitActive = true;
spawnSplitBeams(enemies, cosA, sinA);
} else {
splitActive = false;
}
}
public void spawnSplitBeams(List<Enemy> enemies, float cosA, float sinA) {
float[] ang = { -30f, -15f, 0f, 15f, 30f };
int i = 0;
int j = 0;
double rad = 0.0;
float dirX = 0f;
float dirY = 0f;
float sx = 0f;
float sy = 0f;
float ex = 0f;
float ey = 0f;
float halfW = 0f;
float halfWSq = 0f;
Enemy e = null;
float dx = 0f;
float dy = 0f;
float proj = 0f;
float perp = 0f;
float ix = 0f;
float iy = 0f;
splitActive = true;
for (i = 0; i < splitLines.length; i++) {
// compute the direction for this split ray
rad = Math.toRadians(ang[i]);
dirX = (float) (cosA * Math.cos(rad) - sinA * Math.sin(rad));
dirY = (float) (sinA * Math.cos(rad) + cosA * Math.sin(rad));
// start at the impact point
sx = impactX;
sy = impactY;
// always go full length
ex = sx + dirX * SPLIT_RANGE;
ey = sy + dirY * SPLIT_RANGE;
// still do damage & impacts along the way
halfW = (width * 0.5f) * 0.5f; // as before
halfWSq = halfW * halfW;
for (j = 0; j < enemies.size(); j++) {
e = enemies.get(j);
if (e.isDead() || e.isIntangible())
continue;
dx = (float) (e.getCenterX() - sx);
dy = (float) (e.getCenterY() - sy);
proj = dx * dirX + dy * dirY;
if (proj < 0f || proj > SPLIT_RANGE)
continue;
perp = -dirY * dx + dirX * dy;
if (perp * perp > halfWSq)
continue;
// hit the enemy
ix = sx + proj * dirX;
iy = sy + proj * dirY;
e.takeDamage((int) (damage * 0.75f));
impacts.add(new ImpactEffect(ix, iy));
}
// now set the full-length line
splitLines[i].setLine(sx, sy, ex, ey);
}
}
public void updateImpacts(float dt) {
int i = 0;
ImpactEffect imp = null;
for (i = impacts.size() - 1; i >= 0; i--) {
imp = impacts.get(i);
imp.update(dt);
if (imp.dead()) {
impacts.remove(i);
}
}
}
private BasicStroke getRoundStroke(float width) {
int key = Math.round(width * 100);
BasicStroke s = roundStrokeCache.get(key);
if (s == null) {
s = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
roundStrokeCache.put(key, s);
}
return s;
}
private BasicStroke getDefaultStroke(float width) {
int key = Math.round(width * 100);
BasicStroke s = defaultStrokeCache.get(key);
if (s == null) {
s = new BasicStroke(width);
defaultStrokeCache.put(key, s);
}
return s;
}
@Override
public void draw(Graphics2D g) {
Composite origComp = g.getComposite();
Stroke origStroke = g.getStroke();
int i = 0;
ImpactEffect imp = null;
if (firing) {
drawBeam(g);
}
for (i = 0; i < impacts.size(); i++) {
imp = impacts.get(i);
imp.draw(g);
}
g.setComposite(origComp);
g.setStroke(origStroke);
}
public void drawBeam(Graphics2D g) {
long now;
float start_x;
float start_y;
float end_x;
float end_y;
Graphics2D b2;
float fade;
float shine;
float pulse_val;
float outer_width;
float mid_width;
float main_width;
float core_width;
float center_width;
int i = 0;
Line2D.Float line = null;
float sx2 = 0f;
float sy2 = 0f;
float ex2 = 0f;
float ey2 = 0f;
float sw = 0f;
float cw = 0f;
now = System.nanoTime();
start_x = (float) owner.getCenterX();
start_y = (float) owner.getCenterY();
end_x = (float) beamLine.getX2();
end_y = (float) beamLine.getY2();
b2 = (Graphics2D) g.create();
fade = Math.max(0f, 1f - beamProg);
// Sine-based parameters, computed once
shine = (float) (Math.sin(now * 1e-8) * 0.1f + 0.9f);
pulse_val = (float) (Math.sin(now * (1f / 3e7)) * 0.15f + 0.85f);
// Outer glow
outer_width = width * 3.8f * fade * pulse_val;
b2.setStroke(getStroke(outer_width));
b2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f * fade));
b2.setColor(GLOW_COLOR);
b2.drawLine((int) start_x, (int) start_y, (int) end_x, (int) end_y);
// Mid glow
mid_width = width * 2.5f * fade;
b2.setStroke(getStroke(mid_width));
b2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.35f * fade * pulse_val));
b2.setColor(MID_GLOW_COLOR);
b2.drawLine((int) start_x, (int) start_y, (int) end_x, (int) end_y);
// Main beam
main_width = width * 1.7f * fade * shine;
b2.setStroke(getStroke(main_width));
b2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f * fade * shine));
b2.setColor(MAIN_COLOR);
b2.drawLine((int) start_x, (int) start_y, (int) end_x, (int) end_y);
// Core beam
core_width = width * 0.9f * fade;
b2.setStroke(getStroke(core_width));
b2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f * fade * shine));
b2.setColor(CORE_COLOR);
b2.drawLine((int) start_x, (int) start_y, (int) end_x, (int) end_y);
// White hotspot
center_width = core_width * 0.4f * (0.9f + 0.1f * (float) Math.sin(now * (1f / 1.5e8f)));
b2.setStroke(getStroke(center_width));
b2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.95f * fade));
b2.setColor(WHITE_240);
b2.drawLine((int) start_x, (int) start_y, (int) end_x, (int) end_y);
if (splitActive) {
for (i = 0; i < splitLines.length; i++) {
line = splitLines[i];
sx2 = (float) line.x1;
sy2 = (float) line.y1;
ex2 = (float) line.x2;
ey2 = (float) line.y2;
sw = width * 0.8f * fade;
b2.setStroke(getStroke(sw));
b2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f * fade));
b2.setColor(splitColors[i % splitColors.length]);
b2.drawLine((int) sx2, (int) sy2, (int) ex2, (int) ey2);
cw = sw * 0.5f;
b2.setStroke(getStroke(cw));
b2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f * fade));
b2.setColor(Color.WHITE);
b2.drawLine((int) sx2, (int) sy2, (int) ex2, (int) ey2);
}
}
b2.dispose();
}
public class ImpactEffect {
double x, y;
float life = 0.5f;
float maxLife = 0.5f;
float size;
float rotSpeed;
ImpactEffect(double x, double y) {
this.x = x;
this.y = y;
this.size = 5f + rng.nextFloat() * 5f;
this.rotSpeed = 5f + rng.nextFloat() * 5f;
}
void update(float dt) {
life -= dt;
}
boolean dead() {
return life <= 0;
}
void draw(Graphics2D g) {
Graphics2D p = (Graphics2D) g.create();
float alpha = Math.min(1f, life / maxLife);
float scale = 0.5f + (1f - life / maxLife) * 1.8f;
float safeA = 0f;
int spikes = 8;
int i = 0;
double ang = 0.0;
float innerR = 0f;
float outerR = 0f;
p.translate(x, y);
p.rotate(life * rotSpeed);
p.scale(scale, scale);
safeA = Math.max(0f, Math.min(alpha * 0.9f, 1f));
p.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, safeA * 0.7f));
p.setColor(new Color(170, 210, 255, (int) (120 * alpha)));
p.fillOval(
(int) (-size * 2.5),
(int) (-size * 2.5),
(int) (size * 5),
(int) (size * 5));
p.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, safeA * 0.8f));
p.setColor(new Color(200, 220, 255, (int) (150 * alpha)));
p.fillOval(
(int) (-size * 1.7),
(int) (-size * 1.7),
(int) (size * 3.4),
(int) (size * 3.4));
p.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, safeA));
p.setColor(new Color(255, 255, 255, (int) (220 * alpha)));
for (i = 0; i < spikes; i++) {
ang = i * Math.PI * 2 / spikes;
innerR = size * 0.5f;
outerR = size * (1f + 0.3f * (float) Math.sin(i * 3 + life * 10));
p.setStroke(getStroke(2f));
p.drawLine(
(int) (innerR * Math.cos(ang)),
(int) (innerR * Math.sin(ang)),
(int) (outerR * Math.cos(ang)),
(int) (outerR * Math.sin(ang)));
}
p.setColor(new Color(230, 240, 255, (int) (180 * alpha)));
p.fillOval(
(int) (-size * 0.8),
(int) (-size * 0.8),
(int) (size * 1.6),
(int) (size * 1.6));
p.setColor(new Color(255, 255, 255, (int) (240 * alpha)));
p.fillOval(
(int) (-size * 0.3),
(int) (-size * 0.3),
(int) (size * 0.6),
(int) (size * 0.6));
p.dispose();
}
}
}