-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulletFactory.java
78 lines (57 loc) · 1.94 KB
/
BulletFactory.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
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
public class BulletFactory {
private final static BulletFactory instance = new BulletFactory();
private static Rectangle startBounds;
private BulletFactory() {}
public static BulletFactory getInstance() {
return instance;
}
public void setStartBounds(int x, int y, int width, int height) {
startBounds = new Rectangle(x, y, width, height);
}
public Bullet makeBullet(int score, int x, int y) {
if (score < 800) {
return new BulletImpl((int)startBounds.getMaxX(), (int)startBounds.getCenterY(), Bullet.BulletType.ONE);
} else if (score < 1700) {
return new BulletImpl((int)startBounds.getMaxX(), (int)startBounds.getCenterY(), Bullet.BulletType.TWO);
} else if (score < 2700) {
return new BulletImpl((int)startBounds.getMaxX(), (int)startBounds.getCenterY(), Bullet.BulletType.THREE);
} else if (score < 4400) {
return new BulletImpl((int)startBounds.getMaxX(), (int)startBounds.getCenterY(), Bullet.BulletType.FOUR);
} else {
return new BulletImpl((int)startBounds.getMaxX(), (int)startBounds.getCenterY(), Bullet.BulletType.FIVE);
}
}
private static class BulletImpl implements Bullet {
private final Ellipse2D.Double shape;
private BulletType b;
private BulletImpl(int x, int y, BulletType b) {
this.b = b;
shape = new Ellipse2D.Double(x, y, b.width, b.width);
}
public void draw(Graphics2D g) {
g.setColor(b.color);
g.fill(shape);
}
public void move() {
shape.x += b.velocity;
}
public boolean isVisible() {
return !(shape.x > 900);
}
public Shape getShape() {
return shape;
}
public boolean intersects(Sprite other) {
Area bulletArea = new Area(shape);
Area otherArea = new Area(other.getShape());
bulletArea.intersect(otherArea);
return !otherArea.isEmpty();
}
}
}