-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlayer.java
122 lines (104 loc) · 3.05 KB
/
Player.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
package game.obj;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import javax.swing.ImageIcon;
public class Player extends HpRender {
public Player() {
super(new HP(50, 50));
this.image = new ImageIcon(getClass().getResource("/game/image/plane.png")).getImage();
this.image_speed = new ImageIcon(getClass().getResource("/game/image/plane_speed.png")).getImage();
Path2D p = new Path2D.Double();
p.moveTo(0, 15);
p.lineTo(20, 5);
p.lineTo(PLAYER_SIZE + 15, PLAYER_SIZE / 2);
p.lineTo(20, PLAYER_SIZE - 5);
p.lineTo(0, PLAYER_SIZE - 15);
playerShap = new Area(p);
}
public static final double PLAYER_SIZE = 64;
private double x;
private double y;
private final float MAX_SPEED = 1f;
private float speed = 0f;
private float angle = 0f;
private final Area playerShap;
private final Image image;
private final Image image_speed;
private boolean speedUp;
private boolean alive = true;
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), PLAYER_SIZE / 2, PLAYER_SIZE / 2);
g2.drawImage(speedUp ? image_speed : image, tran, null);
hpRender(g2, getShape(), y);
g2.setTransform(oldTransform);
// Test Shap
// g2.setColor(new Color(12, 173, 84));
// g2.draw(getShape());
// g2.draw(getShape().getBounds());
}
public Area getShape() {
AffineTransform afx = new AffineTransform();
afx.translate(x, y);
afx.rotate(Math.toRadians(angle), PLAYER_SIZE / 2, PLAYER_SIZE / 2);
return new Area(afx.createTransformedShape(playerShap));
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public float getAngle() {
return angle;
}
public void speedUp() {
speedUp = true;
if (speed > MAX_SPEED) {
speed = MAX_SPEED;
} else {
speed += 0.01f;
}
}
public void speedDown() {
speedUp = false;
if (speed <= 0) {
speed = 0;
} else {
speed -= 0.003f;
}
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public void reset() {
alive = true;
resetHP();
angle = 0;
speed = 0;
}
}