|
|
|
@ -18,7 +18,7 @@ public class Bullet {
|
|
|
|
|
private boolean live = true;
|
|
|
|
|
private GroupEnum group = GroupEnum.BAD;
|
|
|
|
|
private TankFrame tankFrame;
|
|
|
|
|
// private Rectangle rectangle;
|
|
|
|
|
private Rectangle rectangle = new Rectangle();
|
|
|
|
|
|
|
|
|
|
public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
|
|
|
|
|
this.x = x;
|
|
|
|
@ -26,7 +26,11 @@ public class Bullet {
|
|
|
|
|
this.dir = dir;
|
|
|
|
|
this.group = group;
|
|
|
|
|
this.tankFrame = tankFrame;
|
|
|
|
|
// this.rectangle = new Rectangle(x, y, WIDTH, HEIGHT);
|
|
|
|
|
|
|
|
|
|
rectangle.x = this.x;
|
|
|
|
|
rectangle.x = this.y;
|
|
|
|
|
rectangle.width = WIDTH;
|
|
|
|
|
rectangle.height = HEIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
@ -61,6 +65,9 @@ public class Bullet {
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rectangle.x = this.x;
|
|
|
|
|
rectangle.y = this.y;
|
|
|
|
|
|
|
|
|
|
if(x<0 || y<0 || x>TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT) this.live = false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -79,13 +86,18 @@ public class Bullet {
|
|
|
|
|
if(this.group == tank.getGroup()) return;
|
|
|
|
|
|
|
|
|
|
//TODO 每次碰撞的时候都小new 一个 Rectangle,应该用一个Rectangle来记录子弹的位置,产生内存占用,垃圾回收器会时不时运行一下
|
|
|
|
|
Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
|
|
|
|
|
/*Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
|
|
|
|
|
Rectangle tankRec = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
|
|
|
|
|
if(bullteRec.intersects(tankRec)) {
|
|
|
|
|
// if(this.rectangle.intersects(tank.getRectangle())) {
|
|
|
|
|
this.die();
|
|
|
|
|
tank.die();
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
if(rectangle.intersects(tank.getRectangle())) {
|
|
|
|
|
this.die();
|
|
|
|
|
tank.die();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void die() {
|
|
|
|
|