-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAngel.java
More file actions
287 lines (252 loc) · 9.46 KB
/
Copy pathAngel.java
File metadata and controls
287 lines (252 loc) · 9.46 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class manages the angel skill class
*/
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Random;
public class Angel extends Entity {
public final GamePanel gamePanel;
public BufferedImage angelImage;
// Speed
public float xVelocity = 0;
public float yVelocity = 0;
// How fast angel moves
public final float maxSpeed;
public static final float ACCELERATION = 0.6f;
public static final Random RNG = new Random();
// Size for hitbox and rendering
public static final int ANGEL_SIZE = 100;
// Damage angel does
public int damage;
// Cached halo gradient and parameters to avoid recreating each frame
private RadialGradientPaint haloPaint;
private int lastHaloRadius = -1;
private double lastHaloCenterX = java.lang.Double.NaN;
private double lastHaloCenterY = java.lang.Double.NaN;
// Spawns angel near player
public Angel(int x, int y, int width, int height, int max_hp, int max_speed, BufferedImage[] sprites,
GamePanel gamePanel, int damage) {
super(x, y, ANGEL_SIZE, ANGEL_SIZE, max_hp, max_speed, sprites);
this.gamePanel = gamePanel;
this.maxSpeed = max_speed;
this.damage = damage;
loadImage();
// Initialize the velocity to zero
super.setVelocity(0, 0);
}
// Use the shared cache so the sprite is only read from disk once for efficiency
public void loadImage() {
angelImage = ImageCache.load("/assets/images/sprites/angel.png");
}
// Manages angel movement
public void move(float dt) {
List<Enemy> enemies = null;
float speedSq = 0f;
float speed = 0f;
float decel = 0f;
float newSpeed = 0f;
Enemy closest = null;
double bestDistSq = 0.0;
int i = 0;
Enemy e = null;
float dx = 0f;
float dy = 0f;
float distSq = 0f;
float toX = 0f;
float toY = 0f;
float dist = 0f;
float desiredVelX = 0f;
float desiredVelY = 0f;
float steerX = 0f;
float steerY = 0f;
float steerMag = 0f;
float wanderAngle = 0f;
float wanderStrength = 0.2f;
float speedNow = 0f;
if (isDead())
return;
try {
enemies = gamePanel.enemies;
if (enemies.isEmpty()) {
speedSq = xVelocity * xVelocity + yVelocity * yVelocity;
speed = (float) Math.sqrt(speedSq);
if (speed > 0) {
decel = ACCELERATION * dt * 60f;
newSpeed = Math.max(0, speed - decel);
xVelocity *= newSpeed / speed;
yVelocity *= newSpeed / speed;
}
} else {
closest = null;
bestDistSq = 1000000.0;
for (i = 0; i < enemies.size(); i++) {
e = enemies.get(i);
dx = (float) (e.getCenterX() - getCenterX());
dy = (float) (e.getCenterY() - getCenterY());
distSq = dx * dx + dy * dy;
if (distSq < bestDistSq) {
bestDistSq = distSq;
closest = e;
}
}
toX = (float) (closest.getCenterX() - getCenterX());
toY = (float) (closest.getCenterY() - getCenterY());
dist = (float) Math.sqrt(toX * toX + toY * toY);
if (dist < 1e-3f) {
xVelocity = 0;
yVelocity = 0;
} else {
toX /= dist;
toY /= dist;
desiredVelX = toX * maxSpeed;
desiredVelY = toY * maxSpeed;
steerX = desiredVelX - xVelocity;
steerY = desiredVelY - yVelocity;
steerMag = (float) Math.sqrt(steerX * steerX + steerY * steerY);
if (steerMag > ACCELERATION) {
steerX = steerX / steerMag * ACCELERATION;
steerY = steerY / steerMag * ACCELERATION;
}
wanderAngle = (float) (RNG.nextDouble() * Math.PI * 2);
wanderStrength = 0.2f;
steerX += (float) Math.cos(wanderAngle) * wanderStrength;
steerY += (float) Math.sin(wanderAngle) * wanderStrength;
xVelocity += steerX * dt * 60f;
yVelocity += steerY * dt * 60f;
speedNow = (float) Math.sqrt(xVelocity * xVelocity + yVelocity * yVelocity);
if (speedNow > maxSpeed) {
xVelocity = xVelocity / speedNow * maxSpeed;
yVelocity = yVelocity / speedNow * maxSpeed;
}
}
}
} catch (Exception e1) {
xVelocity = 0;
yVelocity = 0;
}
// Update position and properly set Entity coordinates
// using the setPos method which updates center_x and center_y
setPos(getCenterX() + xVelocity * dt * 60f, getCenterY() + yVelocity * dt * 60f);
// Apply Entity's velocity too for physics calculations
super.setVelocity(xVelocity, yVelocity);
wrap();
}
// Screen wrapping
public void wrap() {
double centerX = getCenterX();
double centerY = getCenterY();
double halfWidth = width / 2.0;
double halfHeight = height / 2.0;
boolean wrapped = false;
// Instead of teleporting instantly to the other side, we'll soften the wrapping
// behavior
// to prevent the constant teleport back and forth that causes the
// flickering/jittering
// Only wrap when the angel is fully off-screen
if (centerX + halfWidth < 0) {
setPos(gamePanel.getWidth() + halfWidth, centerY);
wrapped = true;
} else if (centerX - halfWidth > gamePanel.getWidth()) {
setPos(-halfWidth, centerY);
wrapped = true;
}
if (centerY + halfHeight < 0) {
setPos(centerX, gamePanel.getHeight() + halfHeight);
wrapped = true;
} else if (centerY - halfHeight > gamePanel.getHeight()) {
setPos(centerX, -halfHeight);
wrapped = true;
}
// If we wrapped, reset velocity slightly to prevent getting stuck in a loop
if (wrapped) {
xVelocity *= 0.8f;
yVelocity *= 0.8f;
}
}
// Draw angel sprite and halo
public void draw(Graphics2D g2d) {
int drawWidth = 0;
int drawHeight = 0;
int centerX = 0;
int centerY = 0;
Composite originalComposite = null;
double time = 0.0;
float baseAlpha = 0f;
int haloRadius = 0;
double cx = 0.0;
double cy = 0.0;
float[] dist = null;
Color[] colors = null;
if (isDead())
return;
if (angelImage == null)
return;
drawWidth = 100;
drawHeight = (int) (drawWidth * ((double) angelImage.getHeight() / angelImage.getWidth()));
centerX = (int) getCenterX();
centerY = (int) getCenterY();
originalComposite = g2d.getComposite();
// Halo
time = System.currentTimeMillis() / 1000.0;
baseAlpha = (float) (0.3f + 0.2f * Math.sin(time * 2));
haloRadius = (int) (drawWidth * 1.2 + 6 * Math.sin(time * 3));
cx = getCenterX();
cy = getCenterY();
// Halo gradient for performance
if (haloPaint == null || haloRadius != lastHaloRadius ||
cx != lastHaloCenterX || cy != lastHaloCenterY) {
dist = new float[] { 0f, 0.7f, 1f };
colors = new Color[] {
new Color(255, 255, 255, 128),
new Color(255, 255, 255, 64),
new Color(255, 255, 255, 0)
};
haloPaint = new RadialGradientPaint(
new Point2D.Double(cx, cy),
haloRadius,
dist,
colors);
lastHaloRadius = haloRadius;
lastHaloCenterX = cx;
lastHaloCenterY = cy;
}
// Draw halo behind angel
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, baseAlpha));
g2d.setPaint(haloPaint);
g2d.fill(new Ellipse2D.Double(
centerX - haloRadius,
centerY - haloRadius,
haloRadius * 2,
haloRadius * 2));
g2d.setComposite(originalComposite);
g2d.drawImage(angelImage, x, y, drawWidth, drawHeight, null);
}
// Override methods needed for proper collision detection
@Override
public void update(float dt) {
if (isDead())
return;
// Don't call super.update() to avoid friction and redundant position updates
// Instead do our own update logic
move(dt);
}
// Get x velocity
public float getXVelocity() {
return xVelocity;
}
// Get y velocity
public float getYVelocity() {
return yVelocity;
}
// Set velocity
@Override
public void setVelocity(float vx, float vy) {
this.xVelocity = vx;
this.yVelocity = vy;
// Don't update parent velocity as we're managing it directly
}
}