-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bullet.java
194 lines (167 loc) · 4.75 KB
/
Bullet.java
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
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.effect.*;
import javafx.stage.*;
import javafx.scene.paint.Color;
import javafx.animation.*;
import javafx.event.*;
import javafx.scene.input.*;
import javafx.scene.shape.Rectangle;
import javafx.scene.canvas.GraphicsContext;
import javafx.util.Duration;
import javafx.scene.shape.*;
import javafx.geometry.Point2D;
import javafx.geometry.Bounds;
import javafx.geometry.BoundingBox;
import javafx.scene.image.*;
import java.util.*;
import java.io.*;
//-------------------------------------------------------------------------------------------
/** Represents a JavaFX circle node that can bounce off walls using black pixel detection.
* Shot by tank
*
*/
public class Bullet {
double xPos;
double yPos;
double xVel;
double yVel;
int collisionCount;
boolean enabled;
Color playerColor;
int radius; // TODO: make radius a class final constant
Circle displayCircle;
PixelReader reader;
/** Constuctor for the bullet.
* Used only once in the <code>start</code> method.
*
* @param root THIS SHOULD ONLY EVER BE THE ROOT NODE
*/
public Bullet(Group root, PixelReader pixelR, Color playerColor) {
this.playerColor = playerColor;
radius = 4;
displayCircle = new Circle(0, 0, radius);
displayCircle.setFill(playerColor);
root.getChildren().add(displayCircle);
reader = pixelR;
}
public void doEvaporate() {
FillTransition waitT = new FillTransition(Duration.millis(300), displayCircle, playerColor, Color.TRANSPARENT);
waitT.setOnFinished((new EventHandler<ActionEvent>() {
public void handle(ActionEvent e){
collisionCount = 0;
displayCircle.setFill(Color.TRANSPARENT);
setStatus(false);
}
}));
waitT.play();
}
/** Sets the status of the bullet.
* @param state true if the bullet is relevant
*
*/
public void setStatus(boolean state) {
enabled = state;
collisionCount = 0;
if (state) {
displayCircle.setFill(playerColor);
} else {
displayCircle.setFill(Color.TRANSPARENT);
setSpeed(0, 0);
setX(0);
setY(0);
displayCircle.setTranslateX(xPos);
displayCircle.setTranslateY(yPos);
}
}
public boolean getEnabled() {
return enabled;
}
/** Moves the bullet by adding velocities to class position integers than
* moves the node itself.
*
*/
public void moveBullet() {
if(enabled){
xPos += xVel;
yPos += yVel;
displayCircle.setTranslateX(xPos);
displayCircle.setTranslateY(yPos);
}
}
/** Tests to see if the point is a black pixel on the image.
* Try to keep the parameter point object not on a decimal place
* @param point The point that you want to check
* @return true if the pixel is black
*/
public boolean inBlack(Point2D point) {
if (point.getX() > 0 && point.getX() < 1280 &&
point.getY() > 0 && point.getY() < 720) {
if (reader.getColor((int)point.getX(),
(int)point.getY()).equals(Color.BLACK)) {
return true;
} else {
return false;
}
} else {
return true;
}
}
public void setReader(PixelReader inputReader) {
reader = inputReader;
}
/** Tests to see if there is a collision then reflects the bullet accordingly.
* Ran on enabled bullets in the game loop
*/
public void doReflect() {
Point2D homePoint = new Point2D(xPos, yPos);
if (inBlack(homePoint)) {
Point2D refX = homePoint.add(-1 * xVel, yVel);
Point2D refY = homePoint.add(xVel, yVel * -1);
if (!inBlack(refX) && inBlack(refY)) { //reflecting x works
xVel *= -1;
} else if (!inBlack(refY) && inBlack(refX)) {
yVel *= -1;
} else if (inBlack(refY) && inBlack(refX)) {
yVel *= -1;
xVel *= -1;
}
collisionCount++;
if (collisionCount == 9) {
doEvaporate();
}
}
}
/** @param x the x value to move the bullet to */
public void setX(double x) {
xPos = x;
}
/** @param y the y value to move the bullet to */
public void setY(double y) {
yPos = y;
}
/** Gets the current x value of the bullet's position.
* @return double - the x value of the bullet
*/
public double getX() {
return xPos;
}
/** Gets the current y value of the bullet's position.
* @return double - the y value of the bullet
*/
public double getY() {
return yPos;
}
/** Sets the speed of the bullet.
* Mostly likely only used in the tanks' {@code shoot} method
* @param x horizontal speed
* @param y vertical speed
*/
public void setSpeed(double x, double y) {
xVel = x;
yVel = y;
}
public Shape getBulletShape() {
return displayCircle;
}
}