TeHArGiS10

Untitled

Jun 27th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Movement : MonoBehaviour {
  4.  
  5. public Transform[] Whites;
  6. public Transform[] Blacks;
  7.  
  8. public Vector3[] whitesPos;
  9. public Vector3[] blacksPos;
  10.  
  11. public Vector3[] whitesDeadPos;
  12. public Transform[] whitesDead;
  13.  
  14. public Vector3[] blacksDeadPos;
  15. public Transform[] blacksDead;
  16.  
  17. public Transform[] startingBlocksWhite;
  18. public Transform[] startingBlocksBlack;
  19.  
  20. public Transform grabbedPawn;
  21. public GameObject placeBlock;
  22.  
  23. Ray ray;
  24. RaycastHit hit;
  25.  
  26. public void Start()
  27. {
  28. for(int i = 0; i < 16; i++)
  29. {
  30. Whites[i].transform.position = startingBlocksWhite[i].transform.position;
  31. Blacks[i].transform.position = startingBlocksBlack[i].transform.position;
  32.  
  33. }
  34. SavePos();
  35.  
  36. for(int g = 0; g < 16; g++)
  37. {
  38. Whites[g].transform.position = whitesPos[g];
  39. Blacks[g].transform.position = blacksPos[g];
  40. }
  41. SavePos();
  42.  
  43. for(int h = 0; h < 15; h++)
  44. {
  45. whitesDeadPos[h] = whitesDead[h].transform.position;
  46. blacksDeadPos[h] = blacksDead[h].transform.position;
  47. }
  48. }
  49.  
  50. public void Update()
  51. {
  52. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  53.  
  54. if (Physics.Raycast(ray, out hit))
  55. {
  56. if (Input.GetMouseButton(0))
  57. {
  58. if (hit.transform.tag == "Chess")
  59. {
  60. grabbedPawn = hit.transform;
  61. grabbedPawn.gameObject.layer = 8;
  62. for (int i = 0; i < 16; i++)
  63. {
  64. Whites[i].GetComponent<BoxCollider>().enabled = false;
  65. Blacks[i].GetComponent<BoxCollider>().enabled = false;
  66. }
  67. } else if (hit.transform.tag == "Chess Black")
  68. {
  69. grabbedPawn = hit.transform;
  70. grabbedPawn.gameObject.layer = 9;
  71. for (int i = 0; i < 16; i++)
  72. {
  73. Whites[i].GetComponent<BoxCollider>().enabled = false;
  74. Blacks[i].GetComponent<BoxCollider>().enabled = false;
  75. }
  76. }
  77. }
  78. else if (Input.GetMouseButtonUp(0))
  79. {
  80. if (hit.transform.tag != "Random")
  81. {
  82. placeBlock = GameObject.Find(hit.transform.name);
  83.  
  84. if (placeBlock != null)
  85. {
  86. if (grabbedPawn != null)
  87. {
  88. Vector3 blockPos = new Vector3(placeBlock.transform.position.x, placeBlock.transform.position.y, 0);
  89. grabbedPawn.transform.position = blockPos;
  90.  
  91. if (grabbedPawn.transform.tag == "Chess")
  92. {
  93. for (int i = 0; i < 16; i++)
  94. {
  95. if(grabbedPawn.transform.position == Whites[i].transform.position || grabbedPawn.transform.position == Blacks[15].transform.position)
  96. {
  97. if (grabbedPawn.transform.gameObject.layer != Whites[i].transform.gameObject.layer)
  98. {
  99. InvalidMove();
  100. }
  101. } else
  102. {
  103. WhiteKill();
  104. }
  105. }
  106. } else if (grabbedPawn.transform.tag == "Chess Black")
  107. {
  108. for (int i = 0; i < 16; i++)
  109. {
  110. if (grabbedPawn.transform.position == Blacks[i].transform.position || grabbedPawn.transform.position == Whites[15].transform.position)
  111. {
  112. if (grabbedPawn.transform.gameObject.layer != Blacks[i].transform.gameObject.layer)
  113. {
  114. InvalidMove();
  115. }
  116. } else
  117. {
  118. BlackKill();
  119. }
  120. }
  121. }
  122.  
  123. grabbedPawn.gameObject.layer = 0;
  124. SavePos();
  125. }
  126. }
  127. }
  128. else
  129. {
  130. InvalidMove();
  131. }
  132.  
  133. for (int f = 0; f < 16; f++)
  134. {
  135. Whites[f].GetComponent<BoxCollider>().enabled = true;
  136. Blacks[f].GetComponent<BoxCollider>().enabled = true;
  137. grabbedPawn = null;
  138.  
  139. Vector3 whitesPos = new Vector3(Whites[f].transform.position.x, Whites[f].transform.position.y, 0);
  140. Vector3 blacksPos = new Vector3(Blacks[f].transform.position.x, Blacks[f].transform.position.y, 0);
  141. Whites[f].transform.position = whitesPos;
  142. Blacks[f].transform.position = blacksPos;
  143. }
  144. }
  145. }
  146.  
  147. if(grabbedPawn != null)
  148. {
  149. grabbedPawn.GetComponent<BoxCollider>().enabled = false;
  150. Vector3 mousePos = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, -1);
  151. grabbedPawn.transform.position = mousePos;
  152. }
  153. }
  154.  
  155. public void InvalidMove()
  156. {
  157. placeBlock = null;
  158.  
  159. for(int i = 0; i < 16; i++)
  160. {
  161. Whites[i].transform.position = whitesPos[i];
  162. Blacks[i].transform.position = blacksPos[i];
  163. }
  164. }
  165.  
  166. public void SavePos()
  167. {
  168. for(int i = 0; i < 16; i++)
  169. {
  170. whitesPos[i] = new Vector3(Whites[i].transform.position.x, Whites[i].transform.position.y, 0);
  171. blacksPos[i] = new Vector3(Blacks[i].transform.position.x, Blacks[i].transform.position.y, 0);
  172. }
  173. }
  174.  
  175. public void WhiteKill()
  176. {
  177. for(int i = 0; i < 16; i++)
  178. {
  179. if(grabbedPawn.transform.position == Blacks[i].transform.position)
  180. {
  181. Blacks[i].transform.position = blacksDeadPos[i];
  182. Blacks[i].transform.gameObject.layer = 2;
  183. }
  184. }
  185. }
  186.  
  187. public void BlackKill()
  188. {
  189. for (int i = 0; i < 15; i++)
  190. {
  191. if (grabbedPawn.transform.position == Whites[i].transform.position)
  192. {
  193. Whites[i].transform.position = whitesDeadPos[i];
  194. Whites[i].transform.gameObject.layer = 2;
  195. }
  196. }
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment