-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projectile.java
103 lines (82 loc) · 2.33 KB
/
Projectile.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
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.ImageIcon;
//a projectile class for all player and enemy projectiles in the game
public class Projectile {
private double x, y, angle, speed, lifetime;
private int q, r;
public Projectile(double speed, double x, double y, double angle) {
this.x = x;
this.y = y;
this.speed = speed;
this.angle = angle;
lifetime = 0;
}
public void update(double delta, Hextile[][] hextiles) {
if (speed > 0) {
x = x + Math.cos(angle) * speed * delta
* (1 - 0.5 * Math.abs(Math.sin(angle)));
y = y + Math.sin(angle) * speed * delta
* (1 - 0.5 * Math.abs(Math.sin(angle)));
updateTilePos(hextiles);
} else
lifetime += delta;
}
private void updateTilePos(Hextile[][] hextiles) {
int[] pos;
pos = Hextile.hexContainCal(hextiles, (int) x, (int) y);
if (pos != null) {
q = pos[0];
r = pos[1];
}
}
// draws an enemy projectile
public void drawE(Graphics2D g, ImageIcon fire) {
if (speed > 0) {
g.setColor(Color.white);
g.drawLine((int) x, (int) y, (int) (x + Math.cos(angle) * 10),
(int) (y + Math.sin(angle) * 10));
g.setStroke(new BasicStroke(2));
g.drawLine((int) x, (int) y, (int) (x + Math.cos(angle) * 6),
(int) (y + Math.sin(angle) * 6));
g.setStroke(new BasicStroke(4));
g.drawLine((int) x, (int) y, (int) (x + Math.cos(angle) * 2),
(int) (y + Math.sin(angle) * 2));
g.setColor(Color.white);
g.setStroke(new BasicStroke(1));
} else {
g.drawImage(fire.getImage(), (int) x - 25, (int) y - 40, null);
}
}
// draws a player projectile
public void drawP(Graphics2D g) {
g.setColor(Color.cyan);
g.drawLine((int) x, (int) y, (int) (x + Math.cos(angle) * 10),
(int) (y + Math.sin(angle) * 10));
g.setStroke(new BasicStroke(2));
g.drawLine((int) x, (int) y, (int) (x + Math.cos(angle) * 6),
(int) (y + Math.sin(angle) * 6));
g.setStroke(new BasicStroke(4));
g.drawLine((int) x, (int) y, (int) (x + Math.cos(angle) * 2),
(int) (y + Math.sin(angle) * 2));
g.setColor(Color.white);
g.setStroke(new BasicStroke(1));
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getQ() {
return q;
}
public double getR() {
return r;
}
public double getLifetime() {
return lifetime;
}
}