Advertisement
Voltonik

Untitled

Aug 13th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 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 bool canSpawn = true;
  27.  
  28. private void Start() {
  29. Instance = this;
  30. }
  31.  
  32. private IEnumerator StartTimer() {
  33. canSpawn = false;
  34. yield return new WaitForSeconds(5);
  35. SpawnAllChessmans();
  36. }
  37.  
  38. private void Update() {
  39. DrawChessBoard();
  40. UpdateSelection();
  41.  
  42. if (PhotonNetwork.PlayerList.Length == 2 && canSpawn == true)
  43. StartCoroutine(StartTimer());
  44.  
  45. if (Input.GetMouseButtonDown(0)) {
  46. if (selectionX >= 0 && selectionY >= 0) {
  47. if (selectedChessman == null) {
  48. //Select the Chessman
  49. SelectChessman(selectionX, selectionY);
  50. }
  51. else {
  52. //Move the Chessman
  53. MoveChessman(selectionX, selectionY);
  54. }
  55. }
  56. }
  57. }
  58.  
  59. private void SelectChessman(int x, int y) {
  60. if (Chessmans[x,y] == null) {
  61. Debug.Log("failed1");
  62. return;
  63. }
  64. if (Chessmans[x,y].isWhite != isWhiteTurn) {
  65. Debug.Log("failed2");
  66. return;
  67. }
  68.  
  69. bool hasAtleastOneMove = false;
  70. allowedMoves = Chessmans[x, y].PossibleMove();
  71. for (int i = 0; i < 8; i++)
  72. for (int j = 0; j < 8; j++)
  73. if (allowedMoves[i, j])
  74. hasAtleastOneMove = true;
  75.  
  76. selectedChessman = Chessmans[x, y];
  77. BoardHighlights.Instance.HighlightAllowedMoves(allowedMoves);
  78. }
  79.  
  80. private void MoveChessman(int x, int y) {
  81. if (allowedMoves[x,y]) {
  82. Chessman c = Chessmans[x, y];
  83.  
  84. if (c != null && c.isWhite != isWhiteTurn) {
  85. //capture a piece
  86. if (Multiplayer == true)
  87. gameObject.GetComponent<PhotonView>().RPC("Capture", RpcTarget.All, c);
  88. else
  89. Capture(c);
  90.  
  91. //if its a king end game
  92. if (c.GetType() == typeof(King)) {
  93. // End game
  94. EndGame();
  95. return;
  96. }
  97. }
  98.  
  99. Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
  100. selectedChessman.transform.position = GetTileCenter(x, y);
  101. selectedChessman.SetPosition(x, y);
  102. Chessmans[x, y] = selectedChessman;
  103. if (Multiplayer == true)
  104. gameObject.GetComponent<PhotonView>().RPC("updateTurn", RpcTarget.All);
  105. else
  106. updateTurn();
  107. }
  108. BoardHighlights.Instance.HideHighlights();
  109. selectedChessman = null;
  110. }
  111. [PunRPC]
  112. private void updateTurn() {
  113. isWhiteTurn = !isWhiteTurn;
  114. }
  115.  
  116. [PunRPC]
  117. private void Capture(Chessman c) {
  118. activeChessman.Remove(c.gameObject);
  119. Destroy(c.gameObject);
  120. Debug.Log("Destroyed/Captured");
  121. }
  122.  
  123. private void UpdateSelection() {
  124. if (!Camera.main) {
  125. return;
  126. }
  127. RaycastHit hit;
  128. if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 500.0f, LayerMask.GetMask("ChessPlane"))) {
  129. selectionX = (int)hit.point.x;
  130. selectionY = (int)hit.point.z;
  131. }
  132. else {
  133. selectionX = -1;
  134. selectionY = -1;
  135. }
  136. }
  137.  
  138. private void SpawnChessman(int index, int x, int y){
  139. if (Multiplayer == true) {
  140. GameObject goM = PhotonNetwork.Instantiate(chessmanPrefabs[index].name, GetTileCenter(x, y), Quaternion.identity) as GameObject;
  141. //goM.transform.SetParent(transform);
  142. Chessmans[x, y] = goM.GetComponent<Chessman>();
  143. Chessmans[x, y].SetPosition(x, y);
  144. activeChessman.Add(goM);
  145. }
  146. else if (Multiplayer == false) {
  147. GameObject go = Instantiate(chessmanPrefabs[index], GetTileCenter(x, y), Quaternion.identity) as GameObject;
  148. go.transform.SetParent(transform);
  149. Chessmans[x, y] = go.GetComponent<Chessman>();
  150. Chessmans[x, y].SetPosition(x, y);
  151. activeChessman.Add(go);
  152. }
  153. }
  154.  
  155. public void SpawnAllChessmans() {
  156. activeChessman = new List<GameObject>();
  157. Chessmans = new Chessman[8, 8];
  158.  
  159. if (PhotonNetwork.IsMasterClient == true && Multiplayer == true) {
  160. //Spawn White
  161. SpawnChessman(0, 3, 0); //king
  162. SpawnChessman(1, 4, 0); //queen
  163. SpawnChessman(2, 0, 0); //rock1
  164. SpawnChessman(2, 7, 0); //rock2
  165. SpawnChessman(3, 2, 0); //bishop1
  166. SpawnChessman(3, 5, 0); //bishop2
  167. SpawnChessman(4, 1, 0); //knight1
  168. SpawnChessman(4, 6, 0); //knight2
  169.  
  170. for (int i = 0; i < 8; i++) //pawns
  171. SpawnChessman(5, i, 1);
  172. }
  173.  
  174.  
  175. if (PhotonNetwork.IsMasterClient == false && Multiplayer == true) {
  176. //Spawn Black
  177. SpawnChessman(6, 3, 7); //king
  178. SpawnChessman(7, 4, 7); //queen
  179. SpawnChessman(8, 0, 7); //rock1
  180. SpawnChessman(8, 7, 7); //rock2
  181. SpawnChessman(9, 2, 7); //bishop1
  182. SpawnChessman(9, 5, 7); //bishop2
  183. SpawnChessman(10, 1, 7); //knight1
  184. SpawnChessman(10, 6, 7); //knight2\
  185.  
  186. for (int i = 0; i < 8; i++) //pawns
  187. SpawnChessman(11, i, 6);
  188. }
  189.  
  190. else if (Multiplayer == false) {
  191. //Spawn White
  192. SpawnChessman(0, 3, 0); //king
  193. SpawnChessman(1, 4, 0); //queen
  194. SpawnChessman(2, 0, 0); //rock1
  195. SpawnChessman(2, 7, 0); //rock2
  196. SpawnChessman(3, 2, 0); //bishop1
  197. SpawnChessman(3, 5, 0); //bishop2
  198. SpawnChessman(4, 1, 0); //knight1
  199. SpawnChessman(4, 6, 0); //knight2
  200.  
  201. for (int i = 0; i < 8; i++) //pawns
  202. SpawnChessman(5, i, 1);
  203.  
  204. //Spawn Black
  205. SpawnChessman(6, 3, 7); //king
  206. SpawnChessman(7, 4, 7); //queen
  207. SpawnChessman(8, 0, 7); //rock1
  208. SpawnChessman(8, 7, 7); //rock2
  209. SpawnChessman(9, 2, 7); //bishop1
  210. SpawnChessman(9, 5, 7); //bishop2
  211. SpawnChessman(10, 1, 7); //knight1
  212. SpawnChessman(10, 6, 7); //knight2\
  213.  
  214. for (int i = 0; i < 8; i++) //pawns
  215. SpawnChessman(11, i, 6);
  216. }
  217.  
  218. }
  219.  
  220. private Vector3 GetTileCenter(int x, int y) {
  221. Vector3 origin = Vector3.zero;
  222. origin.x += (TILE_SIZE * x) + TILE_OFFSET;
  223. origin.z += (TILE_SIZE * y) + TILE_OFFSET;
  224. return origin;
  225. }
  226.  
  227. private void DrawChessBoard() {
  228. Vector3 widthLine = Vector3.right * 8;
  229. Vector3 heightLine = Vector3.forward * 8;
  230.  
  231. for (int i = 0; i <= 8; i++) {
  232. Vector3 start = Vector3.forward * i;
  233. Debug.DrawLine (start, start + widthLine);
  234. for (int j = 0; j <= 8; j++) {
  235. start = Vector3.right * j;
  236. Debug.DrawLine(start, start + heightLine);
  237. }
  238. }
  239.  
  240. //Draw selection
  241. if (selectionX >= 0 && selectionY >= 0) {
  242. Debug.DrawLine(Vector3.forward * selectionY + Vector3.right * selectionX, Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
  243. Debug.DrawLine(Vector3.forward * (selectionY + 1)+ Vector3.right * selectionX, Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
  244. }
  245. }
  246.  
  247. private void EndGame () {
  248. if (isWhiteTurn)
  249. Debug.Log("White team wins");
  250. else
  251. Debug.Log("Black team wins");
  252.  
  253. foreach (GameObject go in activeChessman) {
  254. if (Multiplayer == true)
  255. gameObject.GetComponent<PhotonView>().RPC("destroyPieces", RpcTarget.All, go);
  256. else
  257. destroyPieces(go);
  258. }
  259.  
  260. isWhiteTurn = true;
  261. BoardHighlights.Instance.HideHighlights();
  262. StartCoroutine(StartTimer());
  263. }
  264. [PunRPC]
  265. private void destroyPieces(GameObject go) {
  266. Destroy(go);
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement