public void Clicked(object sender, MouseEventArgs e) { int x = e.X / 64; int y = e.Y / 64; Console.WriteLine("Clicked on X:{0}, Y:{1}", x, y); if (selectedPiece == null) { selectedPiece = board.GetPieceOnLocation(new Point(x, y)); if (selectedPiece != null && selectedPiece.Color != board.Turn) { selectedPiece = null; } } else { if (!selectedPiece.Location.Equals(new Point(x, y)) && selectedPiece.ValidateMove(x, y)) { int tmpx = selectedPiece.Location.X; int tmpy = selectedPiece.Location.Y; selectedPiece.Move(x, y); King king = board.Turn == Pieces.Color.BLACK ? board.BlackKing : board.WhiteKing; if (king.CheckForCheck(king.Location.X, king.Location.Y)) { selectedPiece.Move(tmpx, tmpy); selectedPiece = null; return; } board.NextPlayer(); } selectedPiece = null; } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { SceneManager.LoadScene("Menu"); } if (Input.GetMouseButtonDown(0)) { RaycastHit hitInfo = new RaycastHit(); bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo); if (hit) { if (hitInfo.transform.gameObject.tag == "Piece") { if (selected) { selected.GetComponent <Light>().enabled = false; } selected = hitInfo.transform.gameObject; selectedPiece = selected.GetComponent <Piece>(); if (selectedPiece.color != turnColor) { return; } Board.instance.ClearMoves(); King k = Board.instance.GetKing(turnColor); foreach (Board.Move m in selectedPiece.GetAvailableMoves(Board.instance.GetBoard())) { if (k.CheckForCheck(Board.instance.GetBoard())) { if (k.checkCount > 1) { selectedPiece = k; break; } List <Board.Move> blocks = GetBlocks(); foreach (Board.Move block in blocks) { if (m.piece == selectedPiece) { Board.instance.CreateMove(m.file, m.rank); } } } else { checkText.text = ""; Board.instance.CreateMove(m.file, m.rank); } } hitInfo.transform.GetComponent <Light>().enabled = true; } else if (hitInfo.transform.gameObject.tag == "Move") { Board.Square square = Board.instance.GetSquareFromString(hitInfo.transform.parent.name); Board.instance.MakeMove(new Board.Move(square.file, square.rank, selectedPiece), selected); Board.instance.GetKing(turnColor).checkCount = 0; SwitchTurns(); StartCoroutine(RotateCamera(selectedPiece.color == Color.white)); } } } }