Ejemplo n.º 1
0
  public Sprite(Sprite otherSprite) {
    // copy the otherSprite
    super(
        otherSprite.getX(),
        otherSprite.getY(),
        otherSprite.getWidth(),
        otherSprite.getHeight(),
        otherSprite.isVisible());
    this.frame = otherSprite.frame;
    this.sequence = otherSprite.sequence;
    this.cols = otherSprite.cols;
    this.rows = otherSprite.rows;
    this.transform = otherSprite.transform;
    this.img = otherSprite.img;

    this.collX = otherSprite.collX;
    this.collY = otherSprite.collY;
    this.collWidth = otherSprite.collWidth;
    this.collHeight = otherSprite.collHeight;
    tilesTextRegions = img.split((int) getWidth(), (int) getHeight());
  }
Ejemplo n.º 2
0
  /**
   * Helper methods that check for collisions They are at the end of the file because of the length
   * of the code
   *
   * <p>For both methods, the second and third parameters are significant only if o is an Bitmap,
   * otherwise they are ignored
   */
  private synchronized boolean collidesWith(Object o, int oX, int oY) {

    int tX = 0, tY = 0, tW = 0, tH = 0;
    int oW = 0, oH = 0;

    Sprite t = this;
    boolean another = true;

    while (another) {
      int sX, sY, sW, sH;

      int cX = t.collX;
      int cY = t.collY;
      int cW = t.collWidth;
      int cH = t.collHeight;

      // if there is a zero in a dimension
      // then it cannot intersect with anything!
      if (cW == 0 || cH == 0) {
        return false;
      }

      switch (t.transform) {
        case TRANS_NONE:
          sX = (int) (t.getX() + cX);
          sY = (int) (t.getY() + cY);
          sW = cW;
          sH = cH;
          break;
        case TRANS_MIRROR_ROT180:
          sX = (int) (t.getX() + cX);
          sY = (int) (t.getY() + (t.getHeight() - cY - 1) - cH);
          sW = cW;
          sH = cH;
          break;
        case TRANS_MIRROR:
          sX = (int) (t.getX() + (t.getWidth() - cX - 1) - cW);
          sY = (int) (t.getY() + cY);
          sW = cW;
          sH = cH;
          break;
        case TRANS_ROT180:
          sX = (int) (t.getX() + (t.getWidth() - cX - 1) - cW);
          sY = (int) (t.getY() + (t.getHeight() - cY - 1) - cH);
          sW = cW;
          sH = cH;
          break;
        case TRANS_MIRROR_ROT270:
          sX = (int) (t.getX() + cY);
          sY = (int) (t.getY() + cX);
          sW = cH;
          sH = cW;
          break;
        case TRANS_ROT90:
          sX = (int) (t.getX() + (t.getHeight() - cY - 1) - cH);
          sY = (int) (t.getY() + cX);
          sW = cH;
          sH = cW;
          break;
        case TRANS_MIRROR_ROT90:
          sX = (int) (t.getX() + (t.getHeight() - cY - 1) - cH);
          sY = (int) (t.getY() + (t.getWidth() - cX - 1) - cW);
          sW = cH;
          sH = cW;
          break;
        case TRANS_ROT270:
          sX = (int) (t.getX() + cY);
          sY = (int) (t.getY() + (t.getWidth() - cX - 1) - cW);
          sW = cH;
          sH = cW;
          break;
        default: // cant really happen, but the return keeps the
          // compiler happy (otherwise it'll report variable
          // may not be initialized)
          return false;
      }

      if (o != t) {
        tX = sX;
        tY = sY;
        tW = sW;
        tH = sH;
        if (o instanceof Sprite) {
          // two sprites first round
          // another = true;
          t = (Sprite) o;
        } else if (o instanceof TiledLayer) {
          another = false;
          TiledLayer layer = (TiledLayer) o;
          oX = (int) (layer.getX());
          oY = (int) (layer.getY());
          oW = (int) (layer.getWidth());
          oH = (int) (layer.getHeight());
        } else { // o instanceof lcdui.Bitmap
          another = false;
          TextureRegion img = (TextureRegion) o;
          oW = img.getRegionWidth();
          oH = img.getRegionHeight();
        }
      } else {
        another = false;
        // two sprites
        // second round
        oX = sX;
        oY = sY;
        oW = sW;
        oH = sH;
      }
    }

    // if there is no intersection
    // we know there is no collision
    if (tX > oX && tX >= oX + oW) return false;
    else if (tX < oX && tX + tW <= oX) return false;
    else if (tY > oY && tY >= oY + oH) return false;
    else if (tY < oY && tY + tH <= oY) return false;

    if (o instanceof TiledLayer) {
      // if o is a tiledLayer then
      // it is possible to have not a
      // collision if every intersection tile
      // has a zero value
      TiledLayer layer = (TiledLayer) o;
      // this is the intersection of the two rectangles
      int rX, rY, rW, rH;

      if (oX > tX) {
        rX = oX;
        rW = ((oX + oW < tX + tW) ? oX + oW : tX + tW) - rX;
      } else {
        rX = tX;
        rW = ((tX + tW < oX + oW) ? tX + tW : oX + oW) - rX;
      }
      if (oY > tY) {
        rY = oY;
        rH = ((oY + oH < tY + tH) ? oY + oH : tY + tH) - rY;
      } else {
        rY = tY;
        rH = ((tY + tH < oY + oH) ? tY + tH : oY + oH) - rY;
      }

      int lW = layer.getCellWidth();
      int lH = layer.getCellHeight();

      int minC = (rX - oX) / lW;
      int minR = (rY - oY) / lH;
      int maxC = (rX - oX + rW - 1) / lW;
      int maxR = (rY - oY + rH - 1) / lH;

      // travel across all cells in the collision
      // rectangle
      for (int row = minR; row <= maxR; row++) {
        for (int col = minC; col <= maxC; col++) {
          int cell = layer.getCell(col, row);
          // if cell is animated get current
          // associated static tile
          if (cell < 0) cell = layer.getAnimatedTile(cell);

          if (cell != 0) return true;
        }
      }

      // if no non zero cell was found
      // there is no collision
      return false;
    } else {
      // if the other object is an image or sprite
      // collision happened
      return true;
    }
  }