Advertisement
Guest User

Untitled

a guest
Mar 20th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TileBoard : MonoBehaviour
  6. {
  7. public GameManager gameManager;
  8. public Tile tilePrefab;
  9. public TileState[] tileStates;
  10.  
  11. private TileGrid grid;
  12. private List<Tile> tiles;
  13. private bool waiting;
  14.  
  15. private void Awake()
  16. {
  17. grid = GetComponentInChildren<TileGrid>();
  18. tiles = new List<Tile>(96);
  19. }
  20.  
  21. public void ClearBoard()
  22. {
  23. foreach (var cell in grid.cells)
  24. {
  25. cell.tile = null;
  26. }
  27.  
  28. foreach (var tile in tiles)
  29. {
  30. Destroy(tile.gameObject);
  31. }
  32.  
  33. tiles.Clear();
  34. }
  35.  
  36. public void CreateTile()
  37. {
  38. Tile tile = Instantiate(tilePrefab, grid.transform);
  39. tile.SetState(tileStates[0], 2);
  40. tile.Spawn(grid.GetRandomEmptyCell());
  41. tiles.Add(tile);
  42. }
  43.  
  44.  
  45. private void Update()
  46. {
  47. if (!waiting)
  48. {
  49. if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
  50. {
  51. MoveTiles(Vector2Int.up, 0, 1, 1, 1);
  52. }
  53. else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  54. {
  55. MoveTiles(Vector2Int.down, 0, 1, grid.height - 2, -1);
  56. }
  57. else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
  58. {
  59. MoveTiles(Vector2Int.left, 1, 1, 0, 1);
  60. }
  61. else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
  62. {
  63. MoveTiles(Vector2Int.right, grid.width - 2, -1, 0, 1);
  64. }
  65. }
  66. }
  67.  
  68. private void MoveTiles(Vector2Int direction, int startX, int incrementX, int startY, int incrementY)
  69. {
  70. bool changed = false;
  71.  
  72. for(int x = startX; x >= 0 && x < grid.width; x += incrementX)
  73. {
  74. for(int y = startY; y >= 0 && y < grid.height; y += incrementY)
  75. {
  76. TileCell cell = grid.GetCell(x, y);
  77.  
  78. if (cell.occupied)
  79. {
  80. changed |= MoveTile(cell.tile, direction);
  81. }
  82. }
  83. }
  84.  
  85. if (changed)
  86. {
  87. StartCoroutine(WaitForChanges());
  88. }
  89. }
  90.  
  91. private bool MoveTile(Tile tile, Vector2Int direction)
  92. {
  93. TileCell newCell = null;
  94. TileCell adjacent = grid.GetAdjacentCell(tile.cell, direction);
  95.  
  96. while (adjacent != null)
  97. {
  98. if (adjacent.occupied)
  99. {
  100. if (CanMerge(tile, adjacent.tile))
  101. {
  102. Merge(tile, adjacent.tile);
  103. return true;
  104. }
  105.  
  106. break;
  107. }
  108.  
  109. newCell = adjacent;
  110. adjacent = grid.GetAdjacentCell(adjacent, direction);
  111. }
  112.  
  113. if (newCell != null)
  114. {
  115. tile.MoveTo(newCell);
  116. return true;
  117. }
  118.  
  119. return false;
  120. }
  121.  
  122. private bool CanMerge(Tile a, Tile b)
  123. {
  124. return a.number == b.number && !b.locked;
  125. }
  126.  
  127. private void Merge(Tile a, Tile b)
  128. {
  129. tiles.Remove(a);
  130. a.Merge(b.cell);
  131.  
  132. int index = Mathf.Clamp(IndexOf(b.state) + 1, 0, tileStates.Length - 1);
  133. int number = b.number * 2;
  134.  
  135. b.SetState(tileStates[index], number);
  136.  
  137. gameManager.IncreaseScore(number);
  138. }
  139.  
  140. private int IndexOf(TileState state)
  141. {
  142. for (int i = 0; i < tileStates.Length; i++)
  143. {
  144. if (state == tileStates[i])
  145. {
  146. return i;
  147. }
  148. }
  149.  
  150. return -1;
  151. }
  152.  
  153. private IEnumerator WaitForChanges()
  154. {
  155. waiting = true;
  156.  
  157. yield return new WaitForSeconds(0.1f);
  158.  
  159. waiting = false;
  160.  
  161. foreach (var tile in tiles)
  162. {
  163. tile.locked = false;
  164. }
  165.  
  166. if (tiles.Count != grid.size)
  167. {
  168. CreateTile();
  169. }
  170.  
  171. if (CheckForGameOver())
  172. {
  173. gameManager.GameOver();
  174. }
  175.  
  176. }
  177.  
  178. private bool CheckForGameOver()
  179. {
  180. if (tiles.Count != grid.size)
  181. {
  182. return false;
  183. }
  184.  
  185. foreach (var tile in tiles)
  186. {
  187. TileCell up = grid.GetAdjacentCell(tile.cell, Vector2Int.up);
  188. TileCell down = grid.GetAdjacentCell(tile.cell, Vector2Int.down);
  189. TileCell left = grid.GetAdjacentCell(tile.cell, Vector2Int.left);
  190. TileCell right = grid.GetAdjacentCell(tile.cell, Vector2Int.right);
  191.  
  192. if (up != null && CanMerge(tile, up.tile))
  193. {
  194. return false;
  195. }
  196.  
  197. if (down != null && CanMerge(tile, down.tile))
  198. {
  199. return false;
  200. }
  201.  
  202. if (left != null && CanMerge(tile, left.tile))
  203. {
  204. return false;
  205. }
  206.  
  207. if (right != null && CanMerge(tile, right.tile))
  208. {
  209. return false;
  210. }
  211. }
  212.  
  213. return true;
  214. }
  215.  
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement