-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArchangel.java
More file actions
288 lines (261 loc) · 9.05 KB
/
Copy pathArchangel.java
File metadata and controls
288 lines (261 loc) · 9.05 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class represents the super skill Archangel which summons a massive angel to fight with the player
*/
import java.awt.*;
import java.awt.image.BufferedImage;
public class Archangel extends Angel {
// Size constants
public static final int WIDTH = 300;
public static final int HEIGHT = 96;
// Micellaneous static final variables
private static final float MAX_SPEED = 4f; // px/frame
private static final float ACCEL = 0.15f; // steering strength
private static final float BEAM_COOLDOWN = 0.6f;
private static final float BEAM_DURATION = 0.2f;
private static final float BEAM_RANGE = 1200f;
private static final float AURA_RADIUS = 280f;
private static final float AURA_PUSH = 5f;
private static final int AURA_DAMAGE = 20;
private static final float AURA_INTERVAL = 0.25f;
private static final float KNOCKBACK_FORCE = 12f;
private static final float FRAME_INTERVAL = 0.18f;
private static final int BONUS_DAMAGE = 10;
// Game screen archangel is in
private GamePanel panel;
//Current enemy targeted
private Enemy target;
// For switching between the 2 sprites
private BufferedImage[] sprites;
private int frameIndex = 0;
private float frameTimer = 0f;
// Beam properties
private float beamCooldownTimer = 0f;
private boolean beamActive = false;
private float beamTimer = 0f;
private float beamDirX;
private float beamDirY;
// Time since previous aura hit
private float auraTimer = 0f;
// Constructor spawns archangel near player
public Archangel(GamePanel panel) {
super((int) panel.player.getCenterX(),
(int) panel.player.getCenterY(),
WIDTH,
HEIGHT,
panel.player.getAngelicSummons().angelHp,
(int) MAX_SPEED,
null,
panel,
40);
this.panel = panel;
this.x = (int) (panel.player.getCenterX() - WIDTH / 2f);
this.y = (int) (panel.player.getCenterY() - HEIGHT / 2f);
this.sprites = new BufferedImage[] { ImageCache.load("/assets/images/sprites/archangel.png") };
}
// Updates Archangel each frame
public void update(float dt) {
beamCooldownTimer += dt;
frameTimer += dt;
if (frameTimer >= FRAME_INTERVAL) {
frameIndex = (frameIndex + 1) % sprites.length;
frameTimer -= FRAME_INTERVAL;
}
acquireTarget();
super.update(dt);
handleAura(dt);
handleContactKnockback();
handleBeam(dt);
}
// Find closest enemy to attack
public void acquireTarget() {
float bestDistSq = 1_000_000f;
Enemy best = null;
float dx = 0f;
float dy = 0f;
float distSq = 0f;
Enemy eTemp = null;
int i = 0;
for (i = 0; i < panel.enemies.size(); i++) {
eTemp = panel.enemies.get(i);
if (eTemp.isDead())
continue;
dx = (float) (eTemp.getCenterX() - getCenterX());
dy = (float) (eTemp.getCenterY() - getCenterY());
distSq = dx * dx + dy * dy;
if (distSq < bestDistSq && distSq <= 1_000_000f) { // 1000 px radius
bestDistSq = distSq;
best = eTemp;
}
}
target = best;
}
// Controls beautiful beam firing logic
public void handleBeam(float dt) {
float dx = 0f;
float dy = 0f;
float dist = 0f;
if (beamActive) {
beamTimer += dt;
if (beamTimer >= BEAM_DURATION) {
beamActive = false;
}
}
if (target != null && beamCooldownTimer >= BEAM_COOLDOWN) {
beamCooldownTimer -= BEAM_COOLDOWN;
beamActive = true;
beamTimer = 0f;
dx = (float) (target.getCenterX() - getCenterX());
dy = (float) (target.getCenterY() - getCenterY());
dist = (float) Math.sqrt(dx * dx + dy * dy);
if (dist == 0f) dist = 1f;
beamDirX = dx / dist;
beamDirY = dy / dist;
fireBeam();
}
}
// Hurts enemies in beam path
private void fireBeam() {
float sx = 0f;
float sy = 0f;
float ex = 0f;
float ey = 0f;
Enemy eTemp = null;
int i = 0;
sx = (float) getCenterX();
sy = (float) getCenterY();
ex = sx + beamDirX * BEAM_RANGE;
ey = sy + beamDirY * BEAM_RANGE;
for (i = 0; i < panel.enemies.size(); i++) {
eTemp = panel.enemies.get(i);
if (eTemp.isDead())
continue;
if (lineHitsEnemy(sx, sy, ex, ey, eTemp, 15f)) {
eTemp.takeDamage(40);
}
}
}
// Checks if beam hits enemy
public boolean lineHitsEnemy(float sx, float sy, float ex, float ey, Enemy e, float width) {
float dx = ex - sx;
float dy = ey - sy;
float lenSq = dx * dx + dy * dy;
float t = ((float) e.getCenterX() - sx) * dx + ((float) e.getCenterY() - sy) * dy;
t = Math.max(0, Math.min(lenSq, t));
float projX = sx + dx * t / lenSq;
float projY = sy + dy * t / lenSq;
float pdx = (float) e.getCenterX() - projX;
float pdy = (float) e.getCenterY() - projY;
float radius = e.getRadius() + width;
return pdx * pdx + pdy * pdy <= radius * radius;
}
// Knocks away enemies that touch Archangel
private void handleContactKnockback() {
float cx = 0f;
float cy = 0f;
float r = 0f;
float dx = 0f;
float dy = 0f;
float dist = 0f;
Enemy eTemp = null;
int i = 0;
cx = (float) getCenterX();
cy = (float) getCenterY();
r = WIDTH / 2f;
for (i = 0; i < panel.enemies.size(); i++) {
eTemp = panel.enemies.get(i);
if (eTemp.isDead())
continue;
dx = (float) (eTemp.getCenterX() - cx);
dy = (float) (eTemp.getCenterY() - cy);
dist = (float) Math.sqrt(dx * dx + dy * dy);
if (dist < r + eTemp.getRadius() && !eTemp.isKnockedBack()) {
if (dist == 0f)
dist = 1f;
dx /= dist;
dy /= dist;
eTemp.applyKnockback(dx * KNOCKBACK_FORCE, dy * KNOCKBACK_FORCE);
}
}
}
// Damages and knockbacks enemies in aura range
public void handleAura(float dt) {
float cx = 0f;
float cy = 0f;
float dx = 0f;
float dy = 0f;
float dist = 0f;
Enemy eTemp = null;
int i = 0;
auraTimer += dt;
if (auraTimer < AURA_INTERVAL) return;
auraTimer -= AURA_INTERVAL;
cx = (float) getCenterX();
cy = (float) getCenterY();
for (i = 0; i < panel.enemies.size(); i++) {
eTemp = panel.enemies.get(i);
if (eTemp.isDead())
continue;
dx = (float) (eTemp.getCenterX() - cx);
dy = (float) (eTemp.getCenterY() - cy);
dist = (float) Math.sqrt(dx * dx + dy * dy);
if (dist <= AURA_RADIUS) {
if (dist == 0f)
dist = 1f;
dx /= dist;
dy /= dist;
eTemp.applyKnockback(dx * AURA_PUSH, dy * AURA_PUSH);
eTemp.takeDamage(AURA_DAMAGE);
}
}
}
// Draw Archangel and effects
public void draw(Graphics2D g) {
int cx = 0;
int cy = 0;
Composite orig = null;
int ar = 0;
Stroke old = null;
int ex = 0;
int ey = 0;
BufferedImage img = null;
int imgWidth = 0;
int imgHeight = 0;
double scale = 0.0;
int scaledHeight = 0;
cx = (int) getCenterX();
cy = (int) getCenterY();
// Aura ring
orig = g.getComposite();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
g.setColor(new Color(255, 255, 200, 120));
ar = (int) AURA_RADIUS;
g.fillOval(cx - ar, cy - ar, ar * 2, ar * 2);
g.setComposite(orig);
// Beam
if (beamActive) {
old = g.getStroke();
g.setStroke(new BasicStroke(4f));
g.setColor(Color.WHITE);
ex = (int) (cx + beamDirX * BEAM_RANGE);
ey = (int) (cy + beamDirY * BEAM_RANGE);
g.drawLine(cx, cy, ex, ey);
g.setStroke(old);
}
img = sprites[frameIndex];
if (img != null) {
// Maintain aspect ratio while scaling
imgWidth = img.getWidth();
imgHeight = img.getHeight();
scale = (double)WIDTH / imgWidth;
scaledHeight = (int)(imgHeight * scale);
g.drawImage(img, (int) x, (int) y, WIDTH, scaledHeight, null);
}
}
// Buff up player attacks while Archangel active
public int getBonusDamage() {
return BONUS_DAMAGE;
}
// Inherits getCenterX/getCenterY from Rectangle
}