Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Tile : MonoBehaviour
  6. {
  7. private Vector3 firstPosition;
  8. private Vector3 finalPosition;
  9. private float swipeAngle;
  10. private Vector3 tempPosition;
  11.  
  12. public float xPosition;
  13. public float yPosition;
  14. public int column;
  15. public int row;
  16. private Grid grid;
  17. private GameObject otherTile;
  18. private int previousColumn;
  19. private int previousRow;
  20. public bool isMatched = false;
  21.  
  22.  
  23.  
  24. // Start is called before the first frame update
  25. void Start()
  26. {
  27. grid = FindObjectOfType<Grid>();
  28. xPosition = transform.position.x;
  29. yPosition = transform.position.y;
  30. column = Mathf.RoundToInt((xPosition - grid.startPos.x) / grid.offset.x);
  31. row = Mathf.RoundToInt((yPosition - grid.startPos.y) / grid.offset.x);
  32. previousRow = row;
  33. previousColumn = column;
  34. }
  35.  
  36. // Update is called once per frame
  37. void Update()
  38. {
  39. CheckMatches();
  40. if (isMatched)
  41. {
  42. SpriteRenderer sprite = GetComponent<SpriteRenderer>();
  43. sprite.color = Color.grey;
  44. }
  45.  
  46. xPosition = (column * grid.offset.x) + grid.startPos.x;
  47. yPosition = (row * grid.offset.y) + grid.startPos.y;
  48. SwipeTile();
  49. }
  50.  
  51. void OnMouseDown()
  52. {
  53. firstPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  54. }
  55.  
  56. void OnMouseUp()
  57. {
  58. finalPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  59. CalculateAngle();
  60. }
  61.  
  62. void CalculateAngle()
  63. {
  64. swipeAngle = Mathf.Atan2(finalPosition.y - firstPosition.y, finalPosition.x - firstPosition.x) * 180 / Mathf.PI;
  65. MoveTile();
  66. }
  67.  
  68. void SwipeTile()
  69. {
  70. if (Mathf.Abs(xPosition - transform.position.x) > .1)
  71. {
  72. //Move towards the target
  73. tempPosition = new Vector2(xPosition, transform.position.y);
  74. transform.position = Vector2.Lerp(transform.position, tempPosition, .4f);
  75. }
  76. else
  77. {
  78. //Directly set the position
  79. tempPosition = new Vector2(xPosition, transform.position.y);
  80. transform.position = tempPosition;
  81. grid.tiles[column, row] = this.gameObject;
  82. }
  83.  
  84. if (Mathf.Abs(yPosition - transform.position.y) > .1)
  85. {
  86. //Move towards the target
  87. tempPosition = new Vector2(transform.position.x, yPosition);
  88. transform.position = Vector2.Lerp(transform.position, tempPosition, .4f);
  89. }
  90. else
  91. {
  92. //Directly set the position
  93. tempPosition = new Vector2(transform.position.x, yPosition);
  94. transform.position = tempPosition;
  95. grid.tiles[column, row] = this.gameObject;
  96. }
  97. }
  98.  
  99. void MoveTile()
  100. {
  101. if (swipeAngle > -45 && swipeAngle <= 45 && column < grid.gridSizeX)
  102. {
  103. //Right swipe
  104. SwipeRightMove();
  105. Debug.Log("Right swipe");
  106. }
  107. else if (swipeAngle > 45 && swipeAngle <= 135 && row < grid.gridSizeY)
  108. {
  109. //Up swipe
  110. SwipeUpMove();
  111. Debug.Log("Up swipe");
  112. }
  113. else if (swipeAngle > 135 || swipeAngle <= -135 && column > 0)
  114. {
  115. //Left swipe
  116. SwipeLeftMove();
  117. Debug.Log("Left swipe");
  118. }
  119. else if (swipeAngle < -45 && swipeAngle >= -135 && row > 0)
  120. {
  121. //Down swipe
  122. SwipeDownMove();
  123. Debug.Log("Down swipe");
  124. }
  125. StartCoroutine(checkMove());
  126. }
  127.  
  128. void SwipeRightMove()
  129. {
  130. otherTile = grid.tiles[column + 1, row];
  131. otherTile.GetComponent<Tile>().column -= 1;
  132. column += 1;
  133. }
  134.  
  135. void SwipeUpMove()
  136. {
  137. otherTile = grid.tiles[column, row + 1];
  138. otherTile.GetComponent<Tile>().row -= 1;
  139. row += 1;
  140. }
  141.  
  142. void SwipeLeftMove()
  143. {
  144. otherTile = grid.tiles[column - 1, row];
  145. otherTile.GetComponent<Tile>().column += 1;
  146. column -= 1;
  147. }
  148.  
  149. void SwipeDownMove()
  150. {
  151. otherTile = grid.tiles[column, row - 1];
  152. otherTile.GetComponent<Tile>().row += 1;
  153. row -= 1;
  154. }
  155.  
  156. void CheckMatches()
  157. {
  158. //Check horizontal matching
  159. if (column > 0 && column < grid.gridSizeX - 1)
  160. {
  161. GameObject leftTile = grid.tiles[column - 1, row];
  162. GameObject rightTile = grid.tiles[column + 1, row];
  163. if (leftTile != null && rightTile != null)
  164. {
  165. if (leftTile.CompareTag(gameObject.tag) && rightTile.CompareTag(gameObject.tag))
  166. {
  167. isMatched = true;
  168. rightTile.GetComponent<Tile>().isMatched = true;
  169. leftTile.GetComponent<Tile>().isMatched = true;
  170. }
  171. }
  172. }
  173. //Check vertical matching
  174. if (row > 0 && row < grid.gridSizeY - 1)
  175. {
  176. GameObject upTile = grid.tiles[column, row + 1];
  177. GameObject downTile = grid.tiles[column, row - 1];
  178. if (upTile != null && downTile != null)
  179. {
  180. if (upTile.CompareTag(gameObject.tag) && downTile.CompareTag(gameObject.tag))
  181. {
  182. isMatched = true;
  183. downTile.GetComponent<Tile>().isMatched = true;
  184. upTile.GetComponent<Tile>().isMatched = true;
  185. }
  186. }
  187. }
  188. }
  189.  
  190. IEnumerator checkMove()
  191. {
  192. yield return new WaitForSeconds(.5f);
  193. if (otherTile != null)
  194. {
  195. if (!isMatched && !otherTile.GetComponent<Tile>().isMatched)
  196. {
  197. otherTile.GetComponent<Tile>().row = row;
  198. otherTile.GetComponent<Tile>().column = column;
  199. row = previousRow;
  200. column = previousColumn;
  201. }
  202. else
  203. {
  204. grid.DestroyMatches();
  205. }
  206. }
  207. otherTile = null;
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement