Recursion-Group-P / blokee

https://blockee.netlify.app
3 stars 0 forks source link

handleMouseClick関数でのFlagを使った処理のリファクタリング #27

Closed tkwonn closed 2 years ago

tkwonn commented 2 years ago

Flag抜きで同じ処理を行いたい。

const handleMouseClick = (event) => {
        if (this.isDragging && this.currPlayerSelectedPieceId !== -1) {
          let mouseX = event.pageX - canvas.offsetLeft;
          let mouseY = event.pageY - canvas.offsetTop - 50;

          let cellWidth = this.boardSettings.cellWidth;
          let row = Math.floor(mouseY / cellWidth);
          let col = Math.floor(mouseX / cellWidth);

          let currPiece = this.currPlayer.remainingPieces[this.currPlayerSelectedPieceId];
          let pieceCanBePlaced = false;

          // check if center piece can be placed
          if (this.inBounds(row, col) && this.gameBoard[row][col] === 0) {
            pieceCanBePlaced = true;

            // check if other piece can be placed
            for (let i = 0; i < currPiece.length; i++) {
              let curr_row = row + currPiece[i][0];
              let curr_col = col + currPiece[i][1];
              // console.log('curr_row: ' + curr_row + ' curr_col: ' + curr_col);
              if (this.inBounds(curr_row, curr_col) && this.gameBoard[curr_row][curr_col] === 0) {
                pieceCanBePlaced = true;
              } else {
                pieceCanBePlaced = false;
                break;
              }
            }
          }

          if (pieceCanBePlaced) {
            this.gameBoard[row][col] = this.currentPlayerId + 1;

            for (let i = 0; i < currPiece.length; i++) {
              let curr_row = row + currPiece[i][0];
              let curr_col = col + currPiece[i][1];
              this.gameBoard[curr_row][curr_col] = this.currentPlayerId + 1;
            }

            this.setCurrentPlayerRemainingPieces({
              currentPlayerId: this.currentPlayerId,
            });

            this.setCurrentPlayerSelectedPieceId({
              currentPlayerId: this.currentPlayerId,
              selectedPieceId: -1,
            });
          }

          console.log(this.gameBoard);
        }

        this.drawBoard(context);
      };