public static GameObject PlaceSquare(Vector2 position, GameObject prefab) { GameObject squarePossibleMove = ChessHelper.GetSquare(position); if (squarePossibleMove == null) { return(null); } Vector3 squarePosition = squarePossibleMove.transform.position; Vector3 moveableSquarePos = new Vector3(squarePosition.x, squarePosition.y + 0.1f, squarePosition.z); GameObject GOmoveableSquare = Instantiate(prefab, moveableSquarePos, Quaternion.identity); GOmoveableSquare.transform.SetParent(squarePossibleMove.transform); GOmoveableSquare.transform.position = moveableSquarePos; return(GOmoveableSquare); }
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); } } } }
public static Vector3 CalcPosition(Vector2 pos, Piece piece) { Vector3 SquarePos = ChessHelper.GetSquare(pos).transform.position; return(new Vector3(SquarePos.x, piece.Controller.transform.position.y, SquarePos.z)); }