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