private IEnumerator PieceDown(GameObject changedPiece = null, bool checkValidity = false) { Vector3 piecePosition = pieceSelected.transform.localPosition; Vector3 down = new Vector3(piecePosition.x, 0, piecePosition.z); float time = 0; while (time <= duration) { time += Time.deltaTime; float blend = Mathf.Clamp01(time / duration); pieceSelected.transform.localPosition = Vector3.Lerp(piecePosition, down, blend); yield return(null); } // If the player changes the piece they want to move if (changedPiece != null) { ChangePiece(changedPiece); } // Check if chosen position is valid if (checkValidity) { pieceInformation.GetMoves(); pieceInformation.Moved(); } pieceSelected.GetComponent <Rigidbody>().detectCollisions = true; pieceSelected.GetComponent <Rigidbody>().isKinematic = false; pieceSelected = null; pieceInformation = null; }
/// <summary> /// Checks if at least one piece can block the path or eliminate the piece checking the king - if in check /// Or /// Checks to see if the opponent can play a move - for stalemate condition /// </summary> /// <returns> True if checkmate/stalemate </returns> static bool CheckmateStalemate(int colour, BoardInformation boardInfo) { foreach (GameObject pieceOnBoard in boardInfo.GetPieceAvailable()) { PieceInformation pieceOnBoardInfo = pieceOnBoard.GetComponent <PieceInformation>(); // skip if not opponent's piece if ((int)pieceOnBoardInfo.colour == colour) { continue; } pieceOnBoardInfo.GetMoves(); List <string> allowedPositions = pieceOnBoardInfo.GetPossibleMoves(); // Break out early if at least one piece can be moved if (allowedPositions.Count != 0) { string spots = ""; for (int i = 0; i < allowedPositions.Count; i++) { spots += allowedPositions[i] + " "; } return(false); } } return(true); }
/// <summary> /// Highlights tiles that this piece can move to /// Used by Interactable script events /// </summary> public void TilesOn() { //Look for possible moves pi.GetMoves(); possibleMoves = pi.GetPossibleMoves(); if (possibleMoves == null || bi.GetTurn() != (int)pi.colour) { return; } //Swap the corresponding tiles with highlighted version foreach (string item in possibleMoves) { int x = (int)char.GetNumericValue(item[0]); int z = (int)char.GetNumericValue(item[2]); GameObject tile = chessboard.transform.GetChild(z).gameObject.transform.GetChild(x).gameObject; activeTiles.Add(tile); tile.GetComponent <ActivateHighlight>().HighlightOn(); } }