protected List <Vector3> GetMovesInDirection(Vector3 direction, float range, bool isPawnMove = false, bool canNotCapture = false) //TODO: separate function for pawn { List <Vector3> possibleMoves = new List <Vector3>(); for (int distance = 1; distance <= range; distance++) { Vector3 position = (direction * distance) + transform.position; ChessPieceBase chessPiece = _board.CheckIfPositionOccupied(position); if ((chessPiece != null && chessPiece.isActiveAndEnabled || _board.CheckIfPositionOutOfBoard(position))) { if (chessPiece?.Color != _color && !canNotCapture || chessPiece?.GetType() == typeof(PawnDummy)) { possibleMoves.Add(position); } break; } if (!isPawnMove) { possibleMoves.Add(position); } } return(possibleMoves); }
protected override void DisableOnCapture(ChessPieceBase chessPiece, ChessPieceBase chessPiece1) { if (chessPiece == this && chessPiece1.Type == PiecesType.Pawn) { _pawn.gameObject.SetActive(false); } }
protected virtual void DisableOnCapture(ChessPieceBase chessPiece, ChessPieceBase chessPiece1) { if (chessPiece == this) { chessPiece.transform.position = new Vector3(10, 10, 210); //TODO: find better solution gameObject.SetActive(false); } }
public void LookForPromotion(ChessPieceBase chessPiece) { if (chessPiece == this && chessPiece.transform.position.z == promotionRank) { Debug.Log("PROMO"); chessPiece.gameObject.SetActive(false); Queen queen = Instantiate(_queenPrefab, transform.position, Quaternion.identity); queen.SetColor(_color); RegisterPieceOnBoard(queen); } }
public void SetCaptureForInPassing(ChessPieceBase chessPiece) { int rank = (_color == PiecesColor.White) ? 3 : 4; int side = (_color == PiecesColor.White) ? 1 : -1; if (chessPiece == this && !Moved && transform.position.z == rank) { Debug.Log("Spawn"); PawnDummy pawnDummy = Instantiate(_pawnDummy, chessPiece.transform.position - new Vector3(0, 0, side), Quaternion.identity); pawnDummy.SetConnectedPawn(this); } }
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; } } }
private ChessPieceBase GetRaycastedChessPiece() { RaycastHit hitInfo; Ray ray = _camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo)) { ChessPieceBase chessPiece = hitInfo.collider.GetComponent <ChessPieceBase>(); return((chessPiece && chessPiece.GetType() != typeof(PawnDummy)) ? chessPiece : null); } else { return(null); } }
public bool CheckIfCheck(PiecesColor color) { foreach (ChessPieceBase chessPiece in ChessPieces) { List <Vector3> moves = chessPiece?.GetMoves(); if (moves?.Count > 0) { foreach (Vector3 position in moves) { ChessPieceBase piece = CheckIfPositionOccupied(position); if (piece?.Type == PiecesType.King && chessPiece.isActiveAndEnabled && piece?.Color == color) { Debug.Log("CHECK!!!"); return(true); } } } } return(false); }
protected void RegisterPieceOnBoard(ChessPieceBase chessPiece) { _board = FindObjectOfType <Board>(); _board.ChessPieces.Add(this); }
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; } }
public ChessPieceBase CheckIfPositionOccupied(Vector3 position) { ChessPieceBase chessPiece = ChessPieces.Where(p => p.transform.position == position).FirstOrDefault(); return(chessPiece); }