Exemplo n.º 1
0
  public boolean isBulletFree(Entity bullet, double xc, double yc, int w, int h) {
    double e = 0.1;
    int x0 = (int) (xc / 10);
    int y0 = (int) (yc / 10);
    int x1 = (int) ((xc + w - e) / 10);
    int y1 = (int) ((yc + h - e) / 10);
    boolean ok = true;
    for (int x = x0; x <= x1; x++)
      for (int y = y0; y <= y1; y++) {
        if (x >= 0 && y >= 0 && x < width && y < height) {
          byte ww = walls[x + y * width];
          if (ww != 0) ok = false;
          if (ww == 5) ok = true;
          if (ww == 2) {
            int xPush = 0;
            int yPush = 0;

            if (Math.abs(bullet.xa) > Math.abs(bullet.ya)) {
              if (bullet.xa < 0) xPush = -1;
              if (bullet.xa > 0) xPush = 1;
            } else {
              if (bullet.ya < 0) yPush = -1;
              if (bullet.ya > 0) yPush = 1;
            }
            double r = 0.5;
            if (walls[(x + xPush) + (y + yPush) * width] == 0
                && getEntities((x + xPush) * 10 + r, (y + yPush) * 10 + r, 10 - r * 2, 10 - r * 2)
                        .size()
                    == 0) {
              walls[x + y * width] = 0;
              walls[(x + xPush) + (y + yPush) * width] = 2;
            }
            bullet.remove();
          }
          if (ww == 3) {
            Sound.boom.play();
            for (int i = 0; i < 16; i++) {
              double dir = i * Math.PI * 2 / 8.0;
              double xa = Math.sin(dir);
              double ya = Math.cos(dir);
              double dist = (i / 8) + 1;
              add(new Explosion(1, i * 3, x * 10 + 5 + xa * dist, y * 10 + 5 + ya * dist, xa, ya));
            }
            bullet.remove();
            walls[x + y * width] = 0;
          }
          if (ww == 9) {
            if ((bullet instanceof Explosion) && ((Explosion) bullet).power > 0) {
              Sound.boom.play();
              for (int i = 0; i < 16; i++) {
                double dir = i * Math.PI * 2 / 8.0;
                double xa = Math.sin(dir);
                double ya = Math.cos(dir);
                double dist = (i / 8) + 1;
                add(
                    new Explosion(
                        1, i * 3, x * 10 + 5 + xa * dist, y * 10 + 5 + ya * dist, xa, ya));
              }
              bullet.remove();
              walls[x + y * width] = 0;
            }
          }
        }
      }

    return ok;
  }