Ejemplo n.º 1
0
  public boolean moveTetrominoRight() {
    Log.d(TAG, "move tetromino to right");
    synchronized (lockObj) {
      if (currentTetromino.moveRight(oldMap)) {
        currentMap.copyFrom(oldMap);
        currentTetromino.putOnMap(currentMap);
        lastMoveMap.copyFrom(currentMap);

        // if old position will collision but new moved position will not, then need remove the new
        // tetromino message and send tetromino move message=
        if (!currentTetromino.isCollisionY(
            currentTetromino.getY() + 1, currentTetromino.getSharp(), oldMap)) {
          if (handler.hasMessages(MSG_NEW_TETROMINO)) {
            handler.removeMessages(MSG_NEW_TETROMINO);
            handler.sendEmptyMessageDelayed(MSG_TETROMINO_MOVE, TETROMINO_MOVE_INTERVAL_RESEND);
          }
        }

        for (ITetrisListener listener : customizedListeners) {
          listener.onTetrominoMove();
        }
        return true;
      }
      return false;
    }
  }
Ejemplo n.º 2
0
  public boolean rotateTetromino() {
    Log.d(TAG, "rotate tetromino");
    synchronized (lockObj) {
      if (currentTetromino.rotate(oldMap)) {
        currentMap.copyFrom(oldMap);
        currentTetromino.putOnMap(currentMap);
        lastMoveMap.copyFrom(currentMap);

        for (ITetrisListener listener : customizedListeners) {
          listener.onTetrominoRotated();
        }
        return true;
      }
      return false;
    }
  }
Ejemplo n.º 3
0
  public boolean moveTetrominoDown() {
    Log.d(TAG, "move tetromino down");
    synchronized (lockObj) {
      if (currentTetromino.moveDown(oldMap)) {
        currentMap.copyFrom(oldMap);
        currentTetromino.putOnMap(currentMap);
        lastMoveMap.copyFrom(currentMap);

        for (ITetrisListener listener : customizedListeners) {
          listener.onTetrominoMove();
        }
        return true;
      } else {
        return false;
      }
    }
  }
Ejemplo n.º 4
0
  public void dropTetromino() {
    Log.d(TAG, "drop tetromino down");
    synchronized (lockObj) {
      while (currentTetromino.moveDown(oldMap)) {
        currentMap.copyFrom(oldMap);
        currentTetromino.putOnMap(currentMap);
        lastMoveMap.copyFrom(currentMap);

        for (ITetrisListener listener : customizedListeners) {
          listener.onTetrominoMove();
        }
      }
      handler.removeMessages(MSG_TETROMINO_MOVE);
      Log.d(TAG, "drop tetromino down, send new tetromino message");
      handler.removeMessages(MSG_NEW_TETROMINO);
      handler.sendEmptyMessage(MSG_NEW_TETROMINO);
    }
  }
Ejemplo n.º 5
0
        @Override
        public void handleMessage(Message msg) {
          if (isStarted()) {
            if (!isPaused()) {
              if (msg.what == MSG_TETROMINO_MOVE) {
                Log.d(TAG, "receive move tetromino message");
                lastMoveMap.copyFrom(currentMap);
                gameMove();
              } else if (msg.what == MSG_NEW_TETROMINO) {
                Log.d(TAG, "receive new tetromino message");

                currentMap.copyFrom(lastMoveMap);
                gameChecking();

                oldMap.copyFrom(currentMap);

                currentTetromino = getNewTetromino();
                if (currentTetromino.isAvailable(currentMap)) {
                  currentTetromino.putOnMap(currentMap);

                  this.removeMessages(MSG_TETROMINO_MOVE);
                  this.sendEmptyMessageDelayed(
                      MSG_TETROMINO_MOVE, (int) (TETROMINO_MOVE_INTERVAL / moveSpeed));

                  for (ITetrisListener listener : customizedListeners) {
                    listener.onNewTetromino();
                  }
                } else {
                  gameOver();
                }
              }

            } else {
              Log.d(TAG, "game paused, delay message");
              delayMessage(msg.what);
            }
          }
        }
Ejemplo n.º 6
0
  private Tetromino createRandomTetromino() {
    Tetromino result = null;

    SecureRandom random = new SecureRandom();

    random.setSeed(UUID.randomUUID().hashCode());

    int type = random.nextInt(NUMBER_OF_TETROMINO_TYPE);
    switch (type) {
      case TETROMINO_L:
        result = new LTetromino();
        break;
      case TETROMINO_J:
        result = new JTetromino();
        break;
      case TETROMINO_S:
        result = new STetromino();
        break;
      case TETROMINO_Z:
        result = new ZTetromino();
        break;
      case TETROMINO_T:
        result = new TTetromino();
        break;
      case TETROMINO_O:
        result = new OTetromino();
        break;
      case TETROMINO_I:
        result = new ITetromino();
        break;
    }

    int color = random.nextInt(Tetromino.NUMBER_OF_COLOR) + 1;
    result.setColor(color);
    result.setX(0);
    result.setY(0);

    int col = result.getFirstBlockColIndex();
    int row = result.getFirstBlockRowIndex();

    result.setX(4 - col);
    result.setY(0 - row);

    return result;
  }