public void placeCell(GameObject cell, Vector2Int pos) { cell.transform.position = BoardHandler.toWorldPos(pos); Vector3 wpos = cell.transform.position; wpos.y -= 1.15f; wpos.z -= 1.85f; wpos.x -= 0.45f; cell.transform.position = wpos; }
public void undoMove() { if (moves.Count != 0) { List <Move> last = moves[moves.Count - 1]; foreach (Move move in last) { if (move.Beaten != null) { move.Beaten.SetActive(true); } Figure fig = move.Moved.GetComponent <Figure>(); fig.WasMoved = !move.IsFirstTimeMoved; board[move.EndPoint.x, move.EndPoint.y] = move.Beaten; board[move.OldPoint.x, move.OldPoint.y] = move.Moved; move.Moved.transform.position = BoardHandler.toWorldPos(move.OldPoint); } moves.Remove(last); last.Clear(); changeTurn(); } }
private void handleUserUpdate() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { Vector3 point = hit.point; Vector2Int cell = BoardHandler.toGamePos(point); selectedCell.SetActive(true); placeCell(selectedCell, cell); selectedCell.transform.position = selectedCell.transform.position + new Vector3(0, 0.1f, 0); if (Input.GetMouseButtonDown(0) && !isSelected) { if (board[cell.x, cell.y] != null) { Figure fig = board[cell.x, cell.y].GetComponent <Figure>(); if (fig.team == makesMove) { isSelected = true; selectedFigure = cell; correctMoves = determineMoves(cell); if (correctMoves.Count != 0) { moveCells = new List <GameObject>(); foreach (Vector2Int move in correctMoves) { GameObject tempMoveCell = Instantiate <GameObject>(moveCellPrefab); placeCell(tempMoveCell, move); moveCells.Add(tempMoveCell); } } else { isSelected = false; } } } } else { if (Input.GetMouseButtonDown(0) && isSelected) { foreach (Vector2Int move in correctMoves) { if (cell.Equals(move)) { moveFigure(selectedFigure, cell); if (isBotPlaying) { imThinking = true; } } } isSelected = false; foreach (GameObject moveCell in moveCells) { Destroy(moveCell); } moveCells.Clear(); } } } else { selectedCell.SetActive(false); } if (isSelected && Input.GetMouseButtonDown(1)) { foreach (GameObject moveCell in moveCells) { Destroy(moveCell); } isSelected = false; moveCells.Clear(); } }
public void moveFigure(Vector2Int oldPos, Vector2Int newPos) { if (newPos.x >= 0 && newPos.x < 8 && newPos.y >= 0 && newPos.y < 8 && board[oldPos.x, oldPos.y] != null) { List <Move> doubleMove = new List <Move>(); Figure fig = board[oldPos.x, oldPos.y].GetComponent <Figure>(); if (board[newPos.x, newPos.y] != null && fig.Type == TypeFigure.King) { Figure fig2 = board[newPos.x, newPos.y].GetComponent <Figure>(); if (fig2.Type == TypeFigure.Rook && fig2.team == fig.team) { if (newPos.x == 7) { doubleMove = new List <Move>(); doubleMove.Add(new Move(board[oldPos.x, oldPos.y], board[6, oldPos.y], oldPos, new Vector2Int(6, oldPos.y), true)); doubleMove.Add(new Move(board[newPos.x, newPos.y], board[5, oldPos.y], newPos, new Vector2Int(5, oldPos.y), true)); moves.Add(doubleMove); fig.WasMoved = true; fig2.WasMoved = true; board[6, oldPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[6, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(6, oldPos.y)); board[5, oldPos.y] = board[newPos.x, newPos.y]; board[newPos.x, newPos.y] = null; board[5, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(5, oldPos.y)); } else { doubleMove = new List <Move>(); doubleMove.Add(new Move(board[oldPos.x, oldPos.y], board[2, oldPos.y], oldPos, new Vector2Int(2, oldPos.y), true)); doubleMove.Add(new Move(board[newPos.x, newPos.y], board[3, oldPos.y], newPos, new Vector2Int(3, oldPos.y), true)); moves.Add(doubleMove); fig.WasMoved = true; fig2.WasMoved = true; board[2, oldPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[2, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(2, oldPos.y)); board[3, oldPos.y] = board[newPos.x, newPos.y]; board[newPos.x, newPos.y] = null; board[3, oldPos.y].transform.position = BoardHandler.toWorldPos(new Vector2Int(3, oldPos.y)); } } else { Move newMove = new Move(board[oldPos.x, oldPos.y], board[newPos.x, newPos.y], oldPos, newPos, !fig.WasMoved); doubleMove.Add(newMove); moves.Add(doubleMove); fig.WasMoved = true; if (board[newPos.x, newPos.y] != null) { board[newPos.x, newPos.y].SetActive(false); } board[newPos.x, newPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[newPos.x, newPos.y].transform.position = BoardHandler.toWorldPos(newPos); } } else { Move newMove = new Move(board[oldPos.x, oldPos.y], board[newPos.x, newPos.y], oldPos, newPos, !fig.WasMoved); doubleMove.Add(newMove); moves.Add(doubleMove); fig.WasMoved = true; if (board[newPos.x, newPos.y] != null) { board[newPos.x, newPos.y].SetActive(false); } board[newPos.x, newPos.y] = board[oldPos.x, oldPos.y]; board[oldPos.x, oldPos.y] = null; board[newPos.x, newPos.y].transform.position = BoardHandler.toWorldPos(newPos); } changeTurn(); } }