-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrowSprite.java
More file actions
174 lines (142 loc) · 6.12 KB
/
Copy pathArrowSprite.java
File metadata and controls
174 lines (142 loc) · 6.12 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
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
/**
* Arrow sprite, positions itself 250px from the player in the direction
* of the nearest uncollected collectible coin, pointing towards it.
* Uses DisappearFX for the fading effect.
*/
public class ArrowSprite {
private BufferedImage originalImage;
private BufferedImage currentImage;
private DisappearFX disappearFX;
private int screenX;
private int screenY;
private double rotationAngle;
private float currentAlpha;
private static final double ORBIT_DISTANCE = 250;
private static final int MIN_FADE_DISTANCE = 300;
// Arrow dimensions
private int width;
private int height;
// Constructor that loads the arrow image and creates DisappearFX instance.
public ArrowSprite() {
originalImage = ImageManager.loadBufferedImage("images/Arrow.png");
if (originalImage != null) {
width = originalImage.getWidth();
height = originalImage.getHeight();
// Create a copy for pixel manipulation
currentImage = ImageManager.copyImage(originalImage);
} else {
width = 32;
height = 32;
}
disappearFX = new DisappearFX(0, 0, width, height, "images/Arrow.png");
rotationAngle = 0;
screenX = 0;
screenY = 0;
currentAlpha = 1.0f;
}
public void update(int playerScreenX, int playerScreenY, ArrayList<Collectible> collectibles) {
// Find the nearest uncollected collectible relative to the player
Collectible nearestCoin = findNearestUncollectedCoin(playerScreenX, playerScreenY, collectibles);
if (nearestCoin != null) {
// Calculate the distance from player to nearest coin
double dx = nearestCoin.getScreenX() - playerScreenX;
double dy = nearestCoin.getScreenY() - playerScreenY;
double distance = Math.sqrt(dx * dx + dy * dy);
// Calculate alpha based on distance:
if (distance >= 500) {
currentAlpha = 1.0f;
} else if (distance <= MIN_FADE_DISTANCE) {
currentAlpha = 0.0f;
} else {
currentAlpha = (float)((distance - MIN_FADE_DISTANCE) / 200);
}
// Calculate the angle from player to coin (in radians)
double angleToCoin = Math.atan2(dy, dx);
// Position the arrow at 250px from player in the direction of the coin
screenX = playerScreenX + (int)(Math.cos(angleToCoin) * ORBIT_DISTANCE);
screenY = playerScreenY + (int)(Math.sin(angleToCoin) * ORBIT_DISTANCE);
// Update DisappearFX position
disappearFX.setPosition(screenX, screenY);
// Convert to degrees for AffineTransform
rotationAngle = Math.toDegrees(angleToCoin);
} else {
screenX = playerScreenX;
screenY = playerScreenY;
rotationAngle = 0;
currentAlpha = 0.0f;
disappearFX.setPosition(screenX, screenY);
}
}
// Find the nearest uncollected collectible from the list relative to a position.
private Collectible findNearestUncollectedCoin(int fromX, int fromY, ArrayList<Collectible> collectibles) {
if (collectibles == null || collectibles.isEmpty()) {
return null;
}
Collectible nearest = null;
double minDistance = Double.MAX_VALUE;
for (Collectible collectible : collectibles) {
if (!collectible.isCollected()) {
double dx = collectible.getScreenX() - fromX;
double dy = collectible.getScreenY() - fromY;
double distance = Math.sqrt(dx * dx + dy * dy);
if (distance < minDistance) {
minDistance = distance;
nearest = collectible;
}
}
}
return nearest;
}
/**
* Draw the arrow with rotation applied.
* Uses AffineTransform for proper rotation around the arrow's center.
* Uses AlphaComposite for proper alpha/fade effect based on distance to coin.
*/
public void draw(Graphics2D g2) {
// Get the alpha-applied image from DisappearFX
BufferedImage imageToDraw = disappearFX.getCurrentImage();
if (imageToDraw == null || currentAlpha <= 0.0f) {
return;
}
// Save the current transform and composite
AffineTransform originalTransform = g2.getTransform();
Composite originalComposite = g2.getComposite();
// Calculate center of the arrow
int centerX = screenX + width / 2;
int centerY = screenY + height / 2;
// Create transform for rotation around center
AffineTransform transform = new AffineTransform();
transform.translate(centerX, centerY);
transform.rotate(Math.toRadians(rotationAngle));
transform.translate(-width / 2, -height / 2);
// Apply the transform
g2.setTransform(transform);
// Enable alpha blending for proper transparency rendering
// This ensures the per-pixel alpha from DisappearFX is respected
// The normal method from the class wasn't working
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, currentAlpha));
// Draw the image with alpha composite applied
g2.drawImage(imageToDraw, 0, 0, width, height, null);
// Restore the original transform and composite
g2.setTransform(originalTransform);
g2.setComposite(originalComposite);
}
public int getScreenX() {
return screenX;
}
public int getScreenY() {
return screenY;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}