Advertisement
Voltonik

Untitled

Jul 21st, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Photon.Pun;
  5.  
  6. public class BoardManager : MonoBehaviour {
  7. public static BoardManager Instance { set; get; }
  8. private bool[,] allowedMoves { set; get; }
  9.  
  10. public Chessman[,] Chessmans { set; get; }
  11. public Chessman selectedChessman;
  12.  
  13. private const float TILE_SIZE = 1.0f;
  14. private const float TILE_OFFSET = 0.5f;
  15.  
  16. public int selectionX = -1;
  17. public int selectionY = -1;
  18.  
  19. public List<GameObject> chessmanPrefabs;
  20. private List<GameObject> activeChessman;
  21.  
  22. public bool isWhiteTurn = true;
  23.  
  24. public bool Multiplayer = false;
  25.  
  26. private void Start() {
  27. Instance = this;
  28. SpawnAllChessmans();
  29. }
  30.  
  31. private void Update() {
  32. DrawChessBoard();
  33. UpdateSelection();
  34.  
  35. if (Input.GetMouseButtonDown(0)) {
  36. if (selectionX >= 0 && selectionY >= 0) {
  37. if (selectedChessman == null) {
  38. //Select the Chessman
  39. SelectChessman(selectionX, selectionY);
  40. }
  41. else {
  42. //Move the Chessman
  43. MoveChessman(selectionX, selectionY);
  44. }
  45. }
  46. }
  47. }
  48.  
  49. private void SelectChessman(int x, int y) {
  50. if (Chessmans[x,y] == null) {
  51. Debug.Log("failed1");
  52. return;
  53. }
  54. if (Chessmans[x,y].isWhite != isWhiteTurn) {
  55. Debug.Log("failed2");
  56. return;
  57. }
  58.  
  59. bool hasAtleastOneMove = false;
  60. allowedMoves = Chessmans[x, y].PossibleMove();
  61. for (int i = 0; i < 8; i++)
  62. for (int j = 0; j < 8; j++)
  63. if (allowedMoves[i, j])
  64. hasAtleastOneMove = true;
  65.  
  66. selectedChessman = Chessmans[x, y];
  67. BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
  68. }
  69.  
  70. private void MoveChessman(int x, int y) {
  71. if (allowedMoves[x,y]) {
  72. Chessman c = Chessmans[x, y];
  73.  
  74. if (c != null && c.isWhite != isWhiteTurn) {
  75. //capture a piece
  76.  
  77. //if its a kind end game
  78. if (c.GetType() == typeof(King)) {
  79. // End game
  80. EndGame();
  81. return;
  82. }
  83.  
  84. activeChessman.Remove(c.gameObject);
  85. Destroy(c.gameObject);
  86. }
  87.  
  88. Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
  89. selectedChessman.transform.position = GetTileCenter(x, y);
  90. selectedChessman.SetPosition(x, y);
  91. Chessmans[x, y] = selectedChessman;
  92. isWhiteTurn = !isWhiteTurn;
  93. }
  94. BoardHighlights.Instance.HideHighlights();
  95. selectedChessman = null;
  96. }
  97.  
  98. private void UpdateSelection() {
  99. if (!Camera.main) {
  100. return;
  101. }
  102. RaycastHit hit;
  103. if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 50.0f, LayerMask.GetMask("ChessPlane"))) {
  104. selectionX = (int)hit.point.x;
  105. selectionY = (int)hit.point.z;
  106. }
  107. else {
  108. selectionX = -1;
  109. selectionY = -1;
  110. }
  111. }
  112.  
  113. private void SpawnChessman(int index, int x, int y){
  114. if (Multiplayer == true) {
  115. GameObject goM = PhotonNetwork.Instantiate(chessmanPrefabs[index].name, GetTileCenter(x, y), Quaternion.identity) as GameObject;
  116. goM.transform.SetParent(transform);
  117. Chessmans[x, y] = goM.GetComponent<Chessman>();
  118. Chessmans[x, y].SetPosition(x, y);
  119. activeChessman.Add(goM);
  120. }
  121. else if (Multiplayer == false) {
  122. GameObject go = Instantiate(chessmanPrefabs[index], GetTileCenter(x, y), Quaternion.identity) as GameObject;
  123. go.transform.SetParent(transform);
  124. Chessmans[x, y] = go.GetComponent<Chessman>();
  125. Chessmans[x, y].SetPosition(x, y);
  126. activeChessman.Add(go);
  127. }
  128. }
  129.  
  130. public void SpawnAllChessmans() {
  131. activeChessman = new List<GameObject>();
  132. Chessmans = new Chessman[8, 8];
  133.  
  134. //Spawn White
  135. SpawnChessman(0, 3, 0); //king
  136. SpawnChessman(1, 4, 0); //queen
  137. SpawnChessman(2, 0, 0); //rock1
  138. SpawnChessman(2, 7, 0); //rock2
  139. SpawnChessman(3, 2, 0); //bishop1
  140. SpawnChessman(3, 5, 0); //bishop2
  141. SpawnChessman(4, 1, 0); //knight1
  142. SpawnChessman(4, 6, 0); //knight2
  143.  
  144. for (int i = 0; i < 8; i++) //pawns
  145. SpawnChessman(5, i, 1);
  146.  
  147. //Spawn Black
  148. SpawnChessman(6, 3, 7); //king
  149. SpawnChessman(7, 4, 7); //queen
  150. SpawnChessman(8, 0, 7); //rock1
  151. SpawnChessman(8, 7, 7); //rock2
  152. SpawnChessman(9, 2, 7); //bishop1
  153. SpawnChessman(9, 5, 7); //bishop2
  154. SpawnChessman(10, 1, 7); //knight1
  155. SpawnChessman(10, 6, 7); //knight2
  156.  
  157. for (int i = 0; i < 8; i++) //pawns
  158. SpawnChessman(11, i, 6);
  159. }
  160.  
  161. private Vector3 GetTileCenter(int x, int y) {
  162. Vector3 origin = Vector3.zero;
  163. origin.x += (TILE_SIZE * x) + TILE_OFFSET;
  164. origin.z += (TILE_SIZE * y) + TILE_OFFSET;
  165. return origin;
  166. }
  167.  
  168. private void DrawChessBoard() {
  169. Vector3 widthLine = Vector3.right * 8;
  170. Vector3 heightLine = Vector3.forward * 8;
  171.  
  172. for (int i = 0; i <= 8; i++) {
  173. Vector3 start = Vector3.forward * i;
  174. Debug.DrawLine (start, start + widthLine);
  175. for (int j = 0; j <= 8; j++) {
  176. start = Vector3.right * j;
  177. Debug.DrawLine(start, start + heightLine);
  178. }
  179. }
  180.  
  181. //Draw selection
  182. if (selectionX >= 0 && selectionY >= 0) {
  183. Debug.DrawLine(Vector3.forward * selectionY + Vector3.right * selectionX, Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
  184. Debug.DrawLine(Vector3.forward * (selectionY + 1)+ Vector3.right * selectionX, Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
  185. }
  186. }
  187.  
  188. private void EndGame () {
  189. if (isWhiteTurn)
  190. Debug.Log("White team wins");
  191. else
  192. Debug.Log("Black team wins");
  193.  
  194. foreach (GameObject go in activeChessman)
  195. Destroy(go);
  196.  
  197. isWhiteTurn = true;
  198. BoardHighlights.Instance.HideHighlights();
  199. SpawnAllChessmans();
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement