-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRocket.java
98 lines (83 loc) · 2.61 KB
/
Rocket.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
package game.obj;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import javax.swing.ImageIcon;
public class Rocket extends HpRender {
public Rocket() {
super(new HP(20, 20));
this.image = new ImageIcon(getClass().getResource("/game/image/rocket.png")).getImage();
Path2D p = new Path2D.Double();
p.moveTo(0, ROCKET_SIZE / 2);
p.lineTo(15, 10);
p.lineTo(ROCKET_SIZE - 5, 13);
p.lineTo(ROCKET_SIZE + 10, ROCKET_SIZE / 2);
p.lineTo(ROCKET_SIZE - 5, ROCKET_SIZE - 13);
p.lineTo(15, ROCKET_SIZE - 10);
rocketShap = new Area(p);
}
public static final double ROCKET_SIZE = 50;
private double x;
private double y;
private final float speed = 0.3f;
private float angle = 0;
private final Image image;
private final Area rocketShap;
public void changeLocation(double x, double y) {
this.x = x;
this.y = y;
}
public void update() {
x += Math.cos(Math.toRadians(angle)) * speed;
y += Math.sin(Math.toRadians(angle)) * speed;
}
public void changeAngle(float angle) {
if (angle < 0) {
angle = 359;
} else if (angle > 359) {
angle = 0;
}
this.angle = angle;
}
public void draw(Graphics2D g2) {
AffineTransform oldTransform = g2.getTransform();
g2.translate(x, y);
AffineTransform tran = new AffineTransform();
tran.rotate(Math.toRadians(angle + 45), ROCKET_SIZE / 2, ROCKET_SIZE / 2);
g2.drawImage(image, tran, null);
Shape shap = getShape();
hpRender(g2, shap, y);
g2.setTransform(oldTransform);
// Test
// g2.setColor(new Color(36, 214, 63));
// g2.draw(shap);
// g2.draw(shap.getBounds2D());
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public float getAngle() {
return angle;
}
public Area getShape() {
AffineTransform afx = new AffineTransform();
afx.translate(x, y);
afx.rotate(Math.toRadians(angle), ROCKET_SIZE / 2, ROCKET_SIZE / 2);
return new Area(afx.createTransformedShape(rocketShap));
}
public boolean check(int width, int height) {
Rectangle size = getShape().getBounds();
if (x <= -size.getWidth() || y < -size.getHeight() || x > width || y > height) {
return false;
} else {
return true;
}
}
}