-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.java
84 lines (62 loc) · 1.8 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
/*
*/
import java.util.ArrayList;
import java.awt.Rectangle;
import java.awt.Color;
class Bullet extends MapObject {
private int speed;
private int damage;
private static ArrayList<Bullet> instances; // tracks all initialized bullets for updates
private int playerShot; // 1 or 2 (1 meaning player 1 shot this bullet, 2
// meaning player 2 shot this bullet)
/**
*
* @param pos
* @param dir
* @param damage
* @param playerShot
* @param color
*/
Bullet(Point pos, Point dir, int damage, int playerShot, Color color) {
super(pos, dir, 10, color);
this.damage = damage;
this.speed = 15;
this.playerShot = playerShot;
instances.add(this);
// System.out.println("Bullet created at " + pos);
// System.out.println("Number of bullets: " + instances.size());
hitBox = new Rectangle((int) getPos().x - 5, (int) getPos().y - 5, 10, 10);
}
// be careful of null in instances arraylist
public static void init() {
instances = new ArrayList<Bullet>();
}
/*
move
updates bullet position and hitbox based on direction, speed, and elapsed time
*/
public void move(double elapsedTime) {
//System.out.println(this.dir.x + " " + speed + " " + elapsedTime + " ");
pos = new Point(pos.x + dir.x * speed * elapsedTime * 100, pos.y + dir.y * speed * elapsedTime * 100);
hitBox = new Rectangle((int) pos.x, (int) pos.y, size, size);
}
public static ArrayList<Bullet> getInstances() {
return instances;
}
public Point getPos() {
return pos;
}
public int getSize() {
return size;
}
public int getDamage() {
return this.damage;
}
// for collisions
public int getPlayerShot() {
return playerShot;
}
public void setPlayerShot(int playerShot) {
this.playerShot = playerShot;
}
}