private void ColorPossibleMoves(ChessPieceBase chessPiece) { DrawChecker(); foreach (Vector3 position in chessPiece.GetMoves()) { Square plane = _squares.Where(x => x.transform.position == position).FirstOrDefault(); ChessPieceBase figureToCapture = (CheckIfPositionOccupied(position)); if (plane != null) { plane.GetComponent <MeshRenderer>().material.color = (figureToCapture?.Color != chessPiece?.Color && figureToCapture != null && figureToCapture.isActiveAndEnabled) ? Color.red : Color.cyan; } } }
void Update() { if (_selectedChessPiece == null) { ChessPieceBase chessPiece = GetRaycastedChessPiece(); if (chessPiece != null) { if (Input.GetKeyDown(KeyCode.Mouse0)) { _selectedChessPiece = chessPiece; OnChessPieceSelection?.Invoke(chessPiece); } } } else { Square square = GetRaycastedSquare(); if (square != null) { if (Input.GetKeyDown(KeyCode.Mouse0) && _selectedChessPiece.GetMoves().Contains(square.transform.position)) { Vector3 temp = _selectedChessPiece.transform.position; _selectedChessPiece.transform.position = square.transform.position; if (!Board.CheckIfCheck(_selectedChessPiece.Color)) { OnMove?.Invoke(_selectedChessPiece); OnDeselection?.Invoke(); _selectedChessPiece = null; } else { _selectedChessPiece.transform.position = temp; } } } ChessPieceBase chessPiece = GetRaycastedChessPiece(); if (chessPiece != null && chessPiece.Type != PiecesType.King) { if (Input.GetKeyDown(KeyCode.Mouse0) && _selectedChessPiece != null && _selectedChessPiece.GetMoves().Contains(chessPiece.transform.position)) { Vector3 temp = _selectedChessPiece.transform.position; _selectedChessPiece.transform.position = chessPiece.transform.position; if (!Board.CheckIfCheck(_selectedChessPiece.Color)) { OnMove?.Invoke(_selectedChessPiece); OnCapture?.Invoke(chessPiece, _selectedChessPiece); OnDeselection?.Invoke(); _selectedChessPiece = null; } else { _selectedChessPiece.transform.position = temp; } } } } if (Input.GetKeyDown(KeyCode.Mouse1)) { OnDeselection?.Invoke(); _selectedChessPiece = null; } }