Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Photon.Pun;
- public class Board : MonoBehaviourPunCallbacks
- {
- [SerializeField]
- int boardSize = 8;
- public Vector2[,] boardGrid;
- public ChessPiece[,] pieces;
- [SerializeField]
- Transform chessPieceContainer;
- [SerializeField]
- float spaceBetweenPieces = 1;
- [SerializeField]
- GameObject chessPiece, capturedChessPiece;
- TeamColor currentTeam, onePlayerTeam, twoPlayerTeam, computerTeam;
- ChessPiece selectedPiece;
- Vector2 mousePos;
- Camera cam;
- [SerializeField]
- GameObject highlighter, movementBox;
- List<GameObject> boxes;
- SpeechBubble bubble;
- [SerializeField]
- RectTransform[] onePlayerPiecesGraveyard, twoPlayerPiecesGraveyard;
- int numCapturedWhite, numCapturedBlack;
- bool gameOver, blackKingDown,blackQueenDown, firstpawnBlack,firstPawnWhite, enemyTurn;
- PhotonView view;
- private void Awake()
- {
- view = GetComponent<PhotonView>();
- bubble = FindObjectOfType<SpeechBubble>();
- boxes = new List<GameObject>();
- cam = Camera.main;
- currentTeam = TeamColor.White;
- boardGrid = new Vector2[boardSize, boardSize];
- pieces = new ChessPiece[boardSize, boardSize];
- InitiateBoard();
- onePlayerTeam = GameManager.team;
- if (onePlayerTeam == TeamColor.Black)
- {
- if (GameManager.isSinglePlayer)
- {
- computerTeam = TeamColor.White;
- StartCoroutine(ComputerTurn());
- }
- else
- {
- twoPlayerTeam = TeamColor.White;
- }
- bubble.SpeechBubbleDisplay(13);
- }
- else
- {
- bubble.SpeechBubbleDisplay(12);
- if (GameManager.isSinglePlayer)
- {
- computerTeam = TeamColor.Black;
- }
- else
- {
- twoPlayerTeam = TeamColor.Black;
- }
- }
- }
- [PunRPC]
- void InitiateBoard()
- {
- GameManager.boardInitiated = true;
- for (int x = 0; x < boardGrid.GetLength(0); x++)
- {
- for (int y = 0; y < boardGrid.GetLength(1); y++)
- {
- boardGrid[x, y] = new Vector3(chessPieceContainer.position.x + x * spaceBetweenPieces, chessPieceContainer.position.y + y * spaceBetweenPieces, 0);
- if (y < 2 || y > 5)
- {
- GameObject spawnedPiece = PhotonNetwork.InstantiateRoomObject("Prefabs/Piece", boardGrid[x, y], transform.rotation);
- pieces[x, y] = spawnedPiece.GetComponent<ChessPiece>();
- pieces[x, y].posOnBoard = new Vector2Int(x, y);
- }
- }
- }
- CheckAllLegalMoves();
- }
- private void Update()
- {
- if(selectedPiece == null && highlighter.activeInHierarchy)
- {
- highlighter.SetActive(false);
- }
- if (Input.GetMouseButtonDown(0))
- {
- OnMouse();
- }
- }
- void OnMouse()
- {
- mousePos = Input.mousePosition;
- RemoveSelectionBoxes();
- if (!gameOver)
- {
- if (onePlayerTeam == currentTeam)
- {
- if (CheckIfCoordsAreOnGrid(mousePos))
- {
- if (GetPieceAtCoords(mousePos) != null && selectedPiece == null && pieces[CheckGridPosAtCoords(mousePos).x, CheckGridPosAtCoords(mousePos).y].team == currentTeam)
- {
- Vector2Int pieceToSelectIndex = CheckGridPosAtCoords(mousePos);
- SelectPiece(pieces[pieceToSelectIndex.x, pieceToSelectIndex.y]);
- }
- else if (selectedPiece != null)
- {
- if (pieces[CheckGridPosAtCoords(mousePos).x, CheckGridPosAtCoords(mousePos).y] == null)
- {
- MovePiece(CheckGridPosAtCoords(mousePos), selectedPiece);
- }
- else if (GetPieceAtCoords(mousePos).team == selectedPiece.team)
- {
- SelectPiece(GetPieceAtCoords(mousePos));
- }
- else if (selectedPiece != null)
- {
- CapturePiece(GetPieceAtCoords(mousePos));
- }
- }
- }
- }
- }
- }
- void SwitchActiveTeam()
- {
- if(currentTeam == computerTeam)
- {
- currentTeam = onePlayerTeam;
- }
- else if(GameManager.isSinglePlayer)
- {
- currentTeam = computerTeam;
- }
- else
- {
- currentTeam = twoPlayerTeam;
- }
- }
- ChessPiece GetPieceAtCoords(Vector2 coords)
- {
- return pieces[CheckGridPosAtCoords(coords).x, CheckGridPosAtCoords(coords).y];
- }
- Vector2Int CheckGridPosAtCoords(Vector2 coords)
- {
- float distance = 2;
- Vector2Int pieceInd = Vector2Int.one * -1;
- for (int x = 0; x < boardGrid.GetLength(0); x++)
- {
- for (int y = 0; y < boardGrid.GetLength(1); y++)
- {
- if (Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]) < distance)
- {
- distance = Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]);
- pieceInd = new Vector2Int(x, y);
- }
- }
- }if(pieceInd == Vector2Int.one * -1)
- {
- RemoveSelectionBoxes();
- print("Something when wrong in selecting?");
- }
- return new Vector2Int(pieceInd.x,pieceInd.y);
- }
- void CheckAllLegalMoves()
- {
- for (int i = 0; i < pieces.GetLength(0); i++)
- {
- for (int j = 0; j < pieces.GetLength(1); j++)
- {
- if (pieces[i, j] != null)
- {
- pieces[i, j].CalculateLegalMoves();
- }
- }
- }
- }
- void MovePiece(Vector2Int pos, ChessPiece pieceToMove)
- {
- if (pieces[pos.x, pos.y] == null && pieceToMove.legalMoves.Contains(pos))
- {
- Vector2Int previousPos = pieceToMove.posOnBoard;
- if (pieceToMove.firstMove)
- {
- pieceToMove.firstMove = false;
- }
- pieces[previousPos.x, previousPos.y] = null;
- pieceToMove.posOnBoard = pos;
- pieceToMove.transform.position = boardGrid[pos.x,pos.y];
- pieces[pos.x,pos.y] = pieceToMove;
- DeselectPiece();
- StartNewTurn();
- }else if(pieces[pos.x, pos.y] != null && pieces[pos.x, pos.y].team != selectedPiece.team)
- {
- CapturePiece(pieces[pos.x, pos.y]);
- }
- else if(!selectedPiece.legalMoves.Contains(pos))
- {
- print("Something when wrong in moving?");
- }
- }
- void CapturePiece(ChessPiece pieceToCapture)
- {
- Vector2Int posToMoveTo = Vector2Int.zero;
- if(pieceToCapture.team == TeamColor.White)
- {
- ChessPieceEvent(pieceToCapture.pieceIndex,pieceToCapture.GetComponent<SpriteRenderer>().sprite);
- }
- else
- {
- ChessPieceEvent(pieceToCapture.pieceIndex + 6, pieceToCapture.GetComponent<SpriteRenderer>().sprite);
- }
- for (int x = 0; x < pieces.GetLength(0); x++)
- {
- for (int y = 0; y < pieces.GetLength(1); y++)
- {
- if(pieces[x,y] == pieceToCapture)
- {
- posToMoveTo = pieces[x, y].posOnBoard;
- pieces[x, y] = null;
- Destroy(pieceToCapture.gameObject);
- }
- }
- }
- Destroy(pieceToCapture.gameObject);
- MovePiece(posToMoveTo,selectedPiece);
- }
- public int PosX(Vector2Int coords)
- {
- return CheckGridPosAtCoords(coords).x;
- }
- public int PosY(Vector2Int coords)
- {
- return CheckGridPosAtCoords(coords).y;
- }
- public void SelectPiece(ChessPiece piece)
- {
- if(selectedPiece == null)
- {
- RemoveSelectionBoxes();
- }
- selectedPiece = piece;
- HighlightSelectedPiece();
- for (int i = 0; i < selectedPiece.legalMoves.Count; i++)
- {
- boxes.Add(Instantiate(movementBox, boardGrid[selectedPiece.legalMoves[i].x, selectedPiece.legalMoves[i].y], transform.rotation));
- }
- }
- void HighlightSelectedPiece()
- {
- highlighter.SetActive(true);
- highlighter.transform.position = selectedPiece.transform.position;
- }
- public void DeselectPiece()
- {
- selectedPiece = null;
- RemoveSelectionBoxes();
- }
- void RemoveSelectionBoxes()
- {
- for (int i = 0; i < boxes.Count; i++)
- {
- Destroy(boxes[i]);
- }
- }
- public bool CheckIfArrayIsOutOfBounds(Vector2Int pos)
- {
- return pos.x > -1 && pos.x < 8 && pos.y > -1 && pos.y < 8;
- }
- public bool CheckIfCoordsAreOnGrid(Vector2 coords)
- {
- float distance = 2;
- Vector2Int pieceInd = Vector2Int.one * -1;
- for (int x = 0; x < boardGrid.GetLength(0); x++)
- {
- for (int y = 0; y < boardGrid.GetLength(1); y++)
- {
- if (Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]) < distance)
- {
- distance = Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]);
- pieceInd = new Vector2Int(x, y);
- return true;
- }
- }
- }
- if (pieceInd == Vector2Int.one * -1)
- {
- return false;
- }
- return false;
- }
- void StartNewTurn()
- {
- SwitchActiveTeam();
- for (int i = 0; i < pieces.GetLength(0); i++)
- {
- for (int j = 0; j < pieces.GetLength(1); j++)
- {
- if(pieces[i,j] != null)
- {
- pieces[i, j].CalculateLegalMoves();
- }
- }
- }
- if(currentTeam != onePlayerTeam && !enemyTurn && GameManager.isSinglePlayer)
- {
- StopAllCoroutines();
- StartCoroutine(ComputerTurn());
- }
- }
- IEnumerator ComputerTurn()
- {
- if (!gameOver)
- {
- enemyTurn = true;
- float random = UnityEngine.Random.Range(1f, 5f);
- yield return new WaitForSeconds(random);
- CheckAllLegalMoves();
- ComputerRandomMove();
- enemyTurn = false;
- }
- }
- public void ComputerRandomMove()
- {
- ChessPiece pieceToMove = GetRandomPieceOfTeam();
- int highestNumber = 1;
- for (int i = 0; i < 500; i++)
- {
- pieceToMove = GetRandomPieceOfTeam();
- if ( pieceToMove.legalMoves.Count > 0)
- {
- break;
- }
- highestNumber = i + 1;
- }
- bool moving = false; ;
- List<Vector2Int> moves = new List<Vector2Int>();
- for (int i = 0; i < pieceToMove.legalMoves.Count; i++)
- {
- moves.Add(pieceToMove.legalMoves[i]);
- if(pieces[pieceToMove.legalMoves[i].x, pieceToMove.legalMoves[i].y] != null)
- {
- SelectPiece(pieceToMove);
- moving = true;
- CapturePiece(pieces[pieceToMove.legalMoves[i].x, pieceToMove.legalMoves[i].y]);
- break;
- }
- }
- if (!moving)
- {
- int random = UnityEngine.Random.Range(0, moves.Count);
- SelectPiece(pieceToMove);
- MovePiece(pieceToMove.legalMoves[random], selectedPiece);
- }
- }
- ChessPiece GetRandomPieceOfTeam()
- {
- List<ChessPiece> piecesToCycle = new List<ChessPiece>();
- for (int i = 0; i < pieces.GetLength(0); i++)
- {
- for (int j = 0; j < pieces.GetLength(1); j++)
- {
- if (pieces[i, j] != null && pieces[i, j].team != onePlayerTeam)
- {
- piecesToCycle.Add(pieces[i, j]);
- }
- }
- }
- int random = UnityEngine.Random.Range(0, piecesToCycle.Count);
- return piecesToCycle[random];
- }
- void ChessPieceEvent(int pieceIndex, Sprite sprite)
- {
- if (pieceIndex < 5) {
- numCapturedWhite++;
- GameObject deadPiece = PhotonNetwork.Instantiate("Prefabs/CapturedPiece" ,onePlayerPiecesGraveyard[numCapturedWhite % 2].position,Quaternion.identity);
- deadPiece.GetComponent<Image>().sprite = sprite;
- if(pieceIndex == 0)
- {
- EndGame(TeamColor.Black);
- }
- }
- else
- {
- numCapturedBlack++;
- GameObject deadPiece = PhotonNetwork.Instantiate("Prefabs/CapturedPiece", twoPlayerPiecesGraveyard[numCapturedBlack % 2].position, Quaternion.identity);
- deadPiece.GetComponent<Image>().sprite = sprite;
- if(pieceIndex == 6)
- {
- blackKingDown = true;
- }
- if(pieceIndex == 11)
- {
- blackQueenDown = true;
- }
- if(blackKingDown && blackQueenDown)
- {
- EndGame(TeamColor.White);
- }
- }
- bubble.SpeechBubbleDisplay(pieceIndex);
- }
- public void EndGame(TeamColor victor)
- {
- print("Game over, man");
- gameOver = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement