Advertisement
Voltonik

Untitled

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