Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Photon.Pun;
- public class BoardManager : MonoBehaviour {
- public static BoardManager Instance { set; get; }
- private bool[,] allowedMoves { set; get; }
- public Chessman[,] Chessmans { set; get; }
- public Chessman selectedChessman;
- private const float TILE_SIZE = 1.0f;
- private const float TILE_OFFSET = 0.5f;
- public int selectionX = -1;
- public int selectionY = -1;
- public List<GameObject> chessmanPrefabs;
- private List<GameObject> activeChessman;
- public bool isWhiteTurn = true;
- public bool Multiplayer = false;
- private void Start() {
- Instance = this;
- SpawnAllChessmans();
- }
- private void Update() {
- DrawChessBoard();
- UpdateSelection();
- if (Input.GetMouseButtonDown(0)) {
- if (selectionX >= 0 && selectionY >= 0) {
- if (selectedChessman == null) {
- //Select the Chessman
- SelectChessman(selectionX, selectionY);
- }
- else {
- //Move the Chessman
- MoveChessman(selectionX, selectionY);
- }
- }
- }
- }
- private void SelectChessman(int x, int y) {
- if (Chessmans[x,y] == null) {
- Debug.Log("failed1");
- return;
- }
- if (Chessmans[x,y].isWhite != isWhiteTurn) {
- Debug.Log("failed2");
- return;
- }
- bool hasAtleastOneMove = false;
- allowedMoves = Chessmans[x, y].PossibleMove();
- for (int i = 0; i < 8; i++)
- for (int j = 0; j < 8; j++)
- if (allowedMoves[i, j])
- hasAtleastOneMove = true;
- selectedChessman = Chessmans[x, y];
- BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
- }
- private void MoveChessman(int x, int y) {
- if (allowedMoves[x,y]) {
- Chessman c = Chessmans[x, y];
- if (c != null && c.isWhite != isWhiteTurn) {
- //capture a piece
- //if its a kind end game
- if (c.GetType() == typeof(King)) {
- // End game
- EndGame();
- return;
- }
- activeChessman.Remove(c.gameObject);
- Destroy(c.gameObject);
- }
- Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
- selectedChessman.transform.position = GetTileCenter(x, y);
- selectedChessman.SetPosition(x, y);
- Chessmans[x, y] = selectedChessman;
- isWhiteTurn = !isWhiteTurn;
- }
- BoardHighlights.Instance.HideHighlights();
- selectedChessman = null;
- }
- private void UpdateSelection() {
- if (!Camera.main) {
- return;
- }
- RaycastHit hit;
- if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 50.0f, LayerMask.GetMask("ChessPlane"))) {
- selectionX = (int)hit.point.x;
- selectionY = (int)hit.point.z;
- }
- else {
- selectionX = -1;
- selectionY = -1;
- }
- }
- private void SpawnChessman(int index, int x, int y){
- if (Multiplayer == true) {
- GameObject goM = PhotonNetwork.Instantiate(chessmanPrefabs[index].name, GetTileCenter(x, y), Quaternion.identity) as GameObject;
- goM.transform.SetParent(transform);
- Chessmans[x, y] = goM.GetComponent<Chessman>();
- Chessmans[x, y].SetPosition(x, y);
- activeChessman.Add(goM);
- }
- else if (Multiplayer == false) {
- GameObject go = Instantiate(chessmanPrefabs[index], GetTileCenter(x, y), Quaternion.identity) as GameObject;
- go.transform.SetParent(transform);
- Chessmans[x, y] = go.GetComponent<Chessman>();
- Chessmans[x, y].SetPosition(x, y);
- activeChessman.Add(go);
- }
- }
- public void SpawnAllChessmans() {
- activeChessman = new List<GameObject>();
- Chessmans = new Chessman[8, 8];
- //Spawn White
- SpawnChessman(0, 3, 0); //king
- SpawnChessman(1, 4, 0); //queen
- SpawnChessman(2, 0, 0); //rock1
- SpawnChessman(2, 7, 0); //rock2
- SpawnChessman(3, 2, 0); //bishop1
- SpawnChessman(3, 5, 0); //bishop2
- SpawnChessman(4, 1, 0); //knight1
- SpawnChessman(4, 6, 0); //knight2
- for (int i = 0; i < 8; i++) //pawns
- SpawnChessman(5, i, 1);
- //Spawn Black
- SpawnChessman(6, 3, 7); //king
- SpawnChessman(7, 4, 7); //queen
- SpawnChessman(8, 0, 7); //rock1
- SpawnChessman(8, 7, 7); //rock2
- SpawnChessman(9, 2, 7); //bishop1
- SpawnChessman(9, 5, 7); //bishop2
- SpawnChessman(10, 1, 7); //knight1
- SpawnChessman(10, 6, 7); //knight2
- for (int i = 0; i < 8; i++) //pawns
- SpawnChessman(11, i, 6);
- }
- private Vector3 GetTileCenter(int x, int y) {
- Vector3 origin = Vector3.zero;
- origin.x += (TILE_SIZE * x) + TILE_OFFSET;
- origin.z += (TILE_SIZE * y) + TILE_OFFSET;
- return origin;
- }
- private void DrawChessBoard() {
- Vector3 widthLine = Vector3.right * 8;
- Vector3 heightLine = Vector3.forward * 8;
- for (int i = 0; i <= 8; i++) {
- Vector3 start = Vector3.forward * i;
- Debug.DrawLine (start, start + widthLine);
- for (int j = 0; j <= 8; j++) {
- start = Vector3.right * j;
- Debug.DrawLine(start, start + heightLine);
- }
- }
- //Draw selection
- if (selectionX >= 0 && selectionY >= 0) {
- Debug.DrawLine(Vector3.forward * selectionY + Vector3.right * selectionX, Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
- Debug.DrawLine(Vector3.forward * (selectionY + 1)+ Vector3.right * selectionX, Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
- }
- }
- private void EndGame () {
- if (isWhiteTurn)
- Debug.Log("White team wins");
- else
- Debug.Log("Black team wins");
- foreach (GameObject go in activeChessman)
- Destroy(go);
- isWhiteTurn = true;
- BoardHighlights.Instance.HideHighlights();
- SpawnAllChessmans();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement