Advertisement
Guest User

Untitled

a guest
Mar 20th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using GG.Infrastructure.Utils.Swipe;
  5. using System.IO.Pipes;
  6.  
  7. public class SwipeDetector : MonoBehaviour
  8. {
  9. public static SwipeDetector instance;
  10.  
  11. [SerializeField] private SwipeListener swipeListener;
  12.  
  13. public string swipeDirection;
  14.  
  15. private void Awake()
  16. {
  17. instance = this;
  18. }
  19.  
  20. private void OnEnable()
  21. {
  22. swipeListener.OnSwipe.AddListener(OnSwipe);
  23. }
  24.  
  25. private void OnSwipe(string swipe)
  26. {
  27. swipeDirection = swipe;
  28. Debug.Log(swipe);
  29. }
  30. private void OnDisable()
  31. {
  32. swipeListener.OnSwipe.RemoveListener(OnSwipe);
  33. }
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. using System.Collections;
  44. using System.Collections.Generic;
  45. using UnityEngine;
  46.  
  47. public class TileBoard : MonoBehaviour
  48. {
  49. public GameManager gameManager;
  50. public Tile tilePrefab;
  51. public TileState[] tileStates;
  52.  
  53. private TileGrid grid;
  54. private List<Tile> tiles;
  55. private bool waiting;
  56.  
  57. SwipeDetector swipeDetector;
  58.  
  59.  
  60. private void Awake()
  61. {
  62. grid = GetComponentInChildren<TileGrid>();
  63. tiles = new List<Tile>(96);
  64. swipeDetector = GetComponent<SwipeDetector>();
  65. }
  66.  
  67. public void ClearBoard()
  68. {
  69. foreach (var cell in grid.cells)
  70. {
  71. cell.tile = null;
  72. }
  73.  
  74. foreach (var tile in tiles)
  75. {
  76. Destroy(tile.gameObject);
  77. }
  78.  
  79. tiles.Clear();
  80. }
  81.  
  82. public void CreateTile()
  83. {
  84. Tile tile = Instantiate(tilePrefab, grid.transform);
  85. tile.SetState(tileStates[0], 2);
  86. tile.Spawn(grid.GetRandomEmptyCell());
  87. tiles.Add(tile);
  88. }
  89.  
  90.  
  91. private void Update()
  92. {
  93. //PC
  94. if (!waiting)
  95. {
  96. if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
  97. {
  98. MoveTiles(Vector2Int.up, 0, 1, 1, 1);
  99. }
  100. else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  101. {
  102. MoveTiles(Vector2Int.down, 0, 1, grid.height - 2, -1);
  103. }
  104. else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
  105. {
  106. MoveTiles(Vector2Int.left, 1, 1, 0, 1);
  107. }
  108. else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
  109. {
  110. MoveTiles(Vector2Int.right, grid.width - 2, -1, 0, 1);
  111. }
  112. }
  113.  
  114. //Mobile Swipe
  115. if (!waiting)
  116. {
  117. if (SwipeDetector.instance.swipeDirection == "Up")
  118. {
  119. MoveTiles(Vector2Int.up, 0, 1, 1, 1);
  120. }
  121. else if (SwipeDetector.instance.swipeDirection == "Down")
  122. {
  123. MoveTiles(Vector2Int.down, 0, 1, grid.height - 2, -1);
  124. }
  125. else if (SwipeDetector.instance.swipeDirection == "Left")
  126. {
  127. MoveTiles(Vector2Int.left, 1, 1, 0, 1);
  128. }
  129. else if (SwipeDetector.instance.swipeDirection == "Right")
  130. {
  131. MoveTiles(Vector2Int.right, grid.width - 2, -1, 0, 1);
  132. }
  133. }
  134. }
  135.  
  136. private void MoveTiles(Vector2Int direction, int startX, int incrementX, int startY, int incrementY)
  137. {
  138. bool changed = false;
  139.  
  140. for(int x = startX; x >= 0 && x < grid.width; x += incrementX)
  141. {
  142. for(int y = startY; y >= 0 && y < grid.height; y += incrementY)
  143. {
  144. TileCell cell = grid.GetCell(x, y);
  145.  
  146. if (cell.occupied)
  147. {
  148. changed |= MoveTile(cell.tile, direction);
  149. }
  150. }
  151. }
  152.  
  153. if (changed)
  154. {
  155. StartCoroutine(WaitForChanges());
  156. }
  157. }
  158.  
  159. private bool MoveTile(Tile tile, Vector2Int direction)
  160. {
  161. TileCell newCell = null;
  162. TileCell adjacent = grid.GetAdjacentCell(tile.cell, direction);
  163.  
  164. while (adjacent != null)
  165. {
  166. if (adjacent.occupied)
  167. {
  168. if (CanMerge(tile, adjacent.tile))
  169. {
  170. Merge(tile, adjacent.tile);
  171. return true;
  172. }
  173.  
  174. break;
  175. }
  176.  
  177. newCell = adjacent;
  178. adjacent = grid.GetAdjacentCell(adjacent, direction);
  179. }
  180.  
  181. if (newCell != null)
  182. {
  183. tile.MoveTo(newCell);
  184. return true;
  185. }
  186.  
  187. return false;
  188. }
  189.  
  190. private bool CanMerge(Tile a, Tile b)
  191. {
  192. return a.number == b.number && !b.locked;
  193. }
  194.  
  195. private void Merge(Tile a, Tile b)
  196. {
  197. tiles.Remove(a);
  198. a.Merge(b.cell);
  199.  
  200. int index = Mathf.Clamp(IndexOf(b.state) + 1, 0, tileStates.Length - 1);
  201. int number = b.number * 2;
  202.  
  203. b.SetState(tileStates[index], number);
  204.  
  205. gameManager.IncreaseScore(number);
  206. }
  207.  
  208. private int IndexOf(TileState state)
  209. {
  210. for (int i = 0; i < tileStates.Length; i++)
  211. {
  212. if (state == tileStates[i])
  213. {
  214. return i;
  215. }
  216. }
  217.  
  218. return -1;
  219. }
  220.  
  221. private IEnumerator WaitForChanges()
  222. {
  223. waiting = true;
  224.  
  225. yield return new WaitForSeconds(0.1f);
  226.  
  227. waiting = false;
  228.  
  229. foreach (var tile in tiles)
  230. {
  231. tile.locked = false;
  232. }
  233.  
  234. if (tiles.Count != grid.size)
  235. {
  236. CreateTile();
  237. }
  238.  
  239. if (CheckForGameOver())
  240. {
  241. gameManager.GameOver();
  242. }
  243.  
  244. }
  245.  
  246. private bool CheckForGameOver()
  247. {
  248. if (tiles.Count != grid.size)
  249. {
  250. return false;
  251. }
  252.  
  253. foreach (var tile in tiles)
  254. {
  255. TileCell up = grid.GetAdjacentCell(tile.cell, Vector2Int.up);
  256. TileCell down = grid.GetAdjacentCell(tile.cell, Vector2Int.down);
  257. TileCell left = grid.GetAdjacentCell(tile.cell, Vector2Int.left);
  258. TileCell right = grid.GetAdjacentCell(tile.cell, Vector2Int.right);
  259.  
  260. if (up != null && CanMerge(tile, up.tile))
  261. {
  262. return false;
  263. }
  264.  
  265. if (down != null && CanMerge(tile, down.tile))
  266. {
  267. return false;
  268. }
  269.  
  270. if (left != null && CanMerge(tile, left.tile))
  271. {
  272. return false;
  273. }
  274.  
  275. if (right != null && CanMerge(tile, right.tile))
  276. {
  277. return false;
  278. }
  279. }
  280.  
  281. return true;
  282. }
  283.  
  284. }
  285.  
  286.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement