public static void AttackPiece(Piece attacker, Piece attacked) { ChessBoard board = ChessTracker.board; if (attacked.isWhite) { board.WhitePieces.Remove(board.WhitePieces.Find(x => x.Controller.name == attacked.Controller.name)); } else { board.BlackPieces.Remove(board.BlackPieces.Find(x => x.Controller.name == attacked.Controller.name)); } Destroy(ChessHelper.GetPiece(attacked.position).Controller.gameObject); attacker.position = attacked.position; attacker.Controller.transform.position = ChessHelper.CalcPosition(attacked.position, attacked); ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat; ChessHelper.ResetOldSquares(); ChessHelper.ResetHistory(); ChessHelper.ResetOldAttackSquares(); ChessTracker.isWhiteTurn = !ChessTracker.isWhiteTurn; ChessHelper.HandleKingsInCheck(); }
private void OnMouseDown() { bool setMatOnCurrentSquare = true; bool displayMoveableSquares = true; if (piece.isWhite && !ChessTracker.isWhiteTurn) { if (ChessHistory.selectedPiece != null && ChessHistory.selectedPiece.PossibleAttacks.Contains(piece.position)) { ChessHelper.AttackPiece(ChessHistory.selectedPiece, piece); piece.attacks = new List <Vector2>(); } return; } else if (!piece.isWhite && ChessTracker.isWhiteTurn) { if (ChessHistory.selectedPiece != null && ChessHistory.selectedPiece.PossibleAttacks.Contains(piece.position)) { ChessHelper.AttackPiece(ChessHistory.selectedPiece, piece); piece.attacks = new List <Vector2>(); } return; } GameObject square = ChessHelper.GetSquare(piece.position); if (ChessHistory.selectedSquare != null) { // If clicks on same piece if (square.GetInstanceID() == ChessHistory.selectedSquare.GetInstanceID()) { square.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat; setMatOnCurrentSquare = false; ChessHelper.ResetHistory(); displayMoveableSquares = false; } else { ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat; } } // Removing old moveable squares ChessHelper.ResetOldSquares(); ChessHelper.ResetOldAttackSquares(); piece.attacks = new List <Vector2>(); Debug.Log(displayMoveableSquares); // Displaying moveable squares if (displayMoveableSquares) { foreach (Vector2 possibleMove in piece.PossibleMoves) { GameObject go = ChessHelper.PlaceSquare(possibleMove, piece.moveableSquare); ChessHistory.moveableSquares.Add(go); } } if (setMatOnCurrentSquare) { ChessHistory.selectedSquareMat = square.GetComponent <MeshRenderer>().material; square.GetComponent <MeshRenderer>().material = piece.selectedMat; ChessHistory.selectedSquare = square; ChessHistory.selectedPiece = piece; } // Display possible attacks if (displayMoveableSquares) { foreach (Vector2 possibleAttack in piece.PossibleAttacks) { if (ChessHelper.HasPiece(possibleAttack) && ChessHelper.GetPiece(possibleAttack).isWhite != piece.isWhite) { GameObject go = ChessHelper.PlaceSquare(possibleAttack, piece.attackSquare); ChessHistory.attackSquares.Add(go); } } } }