Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SocialPlatforms.Impl;
  5.  
  6. public class TileBoard : MonoBehaviour
  7. {
  8. public GameManager gameManager;
  9. public Tile tilePrefab;
  10. public TileState[] tileStates;
  11.  
  12. private TileGrid grid;
  13. private List<Tile> tiles;
  14. private bool waiting;
  15.  
  16. SwipeDetector swipeDetector;
  17.  
  18. public int moves = 0;
  19.  
  20.  
  21. private void Awake()
  22. {
  23. grid = GetComponentInChildren<TileGrid>();
  24. tiles = new List<Tile>(96);
  25. swipeDetector = GetComponent<SwipeDetector>();
  26. }
  27.  
  28. public void ClearBoard()
  29. {
  30. foreach (var cell in grid.cells)
  31. {
  32. cell.tile = null;
  33. }
  34.  
  35. foreach (var tile in tiles)
  36. {
  37. Destroy(tile.gameObject);
  38. }
  39.  
  40. tiles.Clear();
  41. }
  42.  
  43. public void CreateTile()
  44. {
  45. Tile tile = Instantiate(tilePrefab, grid.transform);
  46. tile.SetState(tileStates[0], 2);
  47. tile.Spawn(grid.GetRandomEmptyCell());
  48. tiles.Add(tile);
  49. }
  50.  
  51.  
  52. private void Update()
  53. {
  54. //PC
  55. if (!waiting)
  56. {
  57. if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
  58. {
  59. MoveTiles(Vector2Int.up, 0, 1, 1, 1);
  60. }
  61. else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  62. {
  63. MoveTiles(Vector2Int.down, 0, 1, grid.height - 2, -1);
  64. }
  65. else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
  66. {
  67. MoveTiles(Vector2Int.left, 1, 1, 0, 1);
  68. }
  69. else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
  70. {
  71. MoveTiles(Vector2Int.right, grid.width - 2, -1, 0, 1);
  72. }
  73. }
  74.  
  75. //Mobile Swipe
  76. if (!waiting)
  77. {
  78. if (SwipeDetector.instance.swipeDirection == "Up")
  79. {
  80. MoveTiles(Vector2Int.up, 0, 1, 1, 1);
  81. }
  82. else if (SwipeDetector.instance.swipeDirection == "Down")
  83. {
  84. MoveTiles(Vector2Int.down, 0, 1, grid.height - 2, -1);
  85. }
  86. else if (SwipeDetector.instance.swipeDirection == "Left")
  87. {
  88. MoveTiles(Vector2Int.left, 1, 1, 0, 1);
  89. }
  90. else if (SwipeDetector.instance.swipeDirection == "Right")
  91. {
  92. MoveTiles(Vector2Int.right, grid.width - 2, -1, 0, 1);
  93. }
  94. }
  95. SwipeDetector.instance.swipeDirection = null;
  96. }
  97.  
  98. private void MoveTiles(Vector2Int direction, int startX, int incrementX, int startY, int incrementY)
  99. {
  100. bool changed = false;
  101.  
  102. for(int x = startX; x >= 0 && x < grid.width; x += incrementX)
  103. {
  104. for(int y = startY; y >= 0 && y < grid.height; y += incrementY)
  105. {
  106. TileCell cell = grid.GetCell(x, y);
  107.  
  108. if (cell.occupied)
  109. {
  110. changed |= MoveTile(cell.tile, direction);
  111. }
  112. }
  113. }
  114.  
  115. if (changed)
  116. {
  117. StartCoroutine(WaitForChanges());
  118. moves++;
  119. PlayerPrefs.SetInt("moves", moves);
  120. }
  121. }
  122.  
  123. private bool MoveTile(Tile tile, Vector2Int direction)
  124. {
  125. TileCell newCell = null;
  126. TileCell adjacent = grid.GetAdjacentCell(tile.cell, direction);
  127.  
  128. while (adjacent != null)
  129. {
  130. if (adjacent.occupied)
  131. {
  132. if (CanMerge(tile, adjacent.tile))
  133. {
  134. Merge(tile, adjacent.tile);
  135. return true;
  136. }
  137.  
  138. break;
  139. }
  140.  
  141. newCell = adjacent;
  142. adjacent = grid.GetAdjacentCell(adjacent, direction);
  143. }
  144.  
  145. if (newCell != null)
  146. {
  147. tile.MoveTo(newCell);
  148. return true;
  149. }
  150.  
  151. return false;
  152. }
  153.  
  154. private bool CanMerge(Tile a, Tile b)
  155. {
  156. return a.number == b.number && !b.locked;
  157. }
  158.  
  159. private void Merge(Tile a, Tile b)
  160. {
  161. tiles.Remove(a);
  162. a.Merge(b.cell);
  163.  
  164. int index = Mathf.Clamp(IndexOf(b.state) + 1, 0, tileStates.Length - 1);
  165. int number = b.number * 2;
  166.  
  167. b.SetState(tileStates[index], number);
  168.  
  169. gameManager.IncreaseScore(number);
  170. }
  171.  
  172. private int IndexOf(TileState state)
  173. {
  174. for (int i = 0; i < tileStates.Length; i++)
  175. {
  176. if (state == tileStates[i])
  177. {
  178. return i;
  179. }
  180. }
  181.  
  182. return -1;
  183. }
  184.  
  185. private IEnumerator WaitForChanges()
  186. {
  187. waiting = true;
  188.  
  189. yield return new WaitForSeconds(0.1f);
  190.  
  191. waiting = false;
  192.  
  193. foreach (var tile in tiles)
  194. {
  195. tile.locked = false;
  196. }
  197.  
  198. if (tiles.Count != grid.size)
  199. {
  200. CreateTile();
  201. }
  202.  
  203. if (CheckForGameOver())
  204. {
  205. gameManager.GameOver();
  206. }
  207.  
  208. }
  209.  
  210. private bool CheckForGameOver()
  211. {
  212. if (tiles.Count != grid.size)
  213. {
  214. return false;
  215. }
  216.  
  217. foreach (var tile in tiles)
  218. {
  219. TileCell up = grid.GetAdjacentCell(tile.cell, Vector2Int.up);
  220. TileCell down = grid.GetAdjacentCell(tile.cell, Vector2Int.down);
  221. TileCell left = grid.GetAdjacentCell(tile.cell, Vector2Int.left);
  222. TileCell right = grid.GetAdjacentCell(tile.cell, Vector2Int.right);
  223.  
  224. if (up != null && CanMerge(tile, up.tile))
  225. {
  226. return false;
  227. }
  228.  
  229. if (down != null && CanMerge(tile, down.tile))
  230. {
  231. return false;
  232. }
  233.  
  234. if (left != null && CanMerge(tile, left.tile))
  235. {
  236. return false;
  237. }
  238.  
  239. if (right != null && CanMerge(tile, right.tile))
  240. {
  241. return false;
  242. }
  243. }
  244.  
  245. return true;
  246. }
  247.  
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. using System.Collections;
  261. using System.Collections.Generic;
  262. using UnityEngine;
  263. using TMPro;
  264.  
  265. public class GameManager : MonoBehaviour
  266. {
  267. public TileBoard board;
  268. public CanvasGroup gameOver;
  269. public TextMeshProUGUI scoreText;
  270. public TextMeshProUGUI hiscoreText;
  271. public TextMeshProUGUI movesText;
  272.  
  273.  
  274. private int score;
  275. private int moves;
  276.  
  277.  
  278. private void Start()
  279. {
  280. NewGame();
  281. }
  282.  
  283. private void Update()
  284. {
  285. SetMoves(0);
  286. }
  287.  
  288. public void NewGame()
  289. {
  290. SetScore(0);
  291. SetMoves(0);
  292. hiscoreText.text = LoadHiscore().ToString();
  293.  
  294. gameOver.alpha = 0f;
  295. gameOver.interactable = false;
  296.  
  297. board.ClearBoard();
  298. board.CreateTile();
  299. board.CreateTile();
  300. board.enabled = true;
  301. }
  302.  
  303. public void GameOver()
  304. {
  305. board.enabled = false;
  306. gameOver.interactable = true;
  307.  
  308. StartCoroutine(Fade(gameOver, 1f, 1f));
  309. }
  310.  
  311. private IEnumerator Fade(CanvasGroup canvasGroup, float to, float delay)
  312. {
  313. yield return new WaitForSeconds(delay);
  314.  
  315. float elapsed = 0f;
  316. float duration = 0.5f;
  317. float from = canvasGroup.alpha;
  318.  
  319. while (elapsed < duration)
  320. {
  321. canvasGroup.alpha = Mathf.Lerp(from, to, elapsed / duration);
  322. elapsed += Time.deltaTime;
  323. yield return null;
  324. }
  325.  
  326. canvasGroup.alpha = to;
  327. }
  328.  
  329. public void IncreaseScore(int points)
  330. {
  331. SetScore(score + points);
  332. }
  333.  
  334. private void SetScore(int score)
  335. {
  336. this.score = score;
  337. scoreText.text = score.ToString();
  338.  
  339. SaveHiscore();
  340. }
  341.  
  342. private void SaveHiscore()
  343. {
  344. int hiscore = LoadHiscore();
  345.  
  346. if (score > hiscore)
  347. {
  348. PlayerPrefs.SetInt("hiscore", score);
  349. }
  350. }
  351.  
  352. private int LoadHiscore()
  353. {
  354. return PlayerPrefs.GetInt("hiscore", 0);
  355. }
  356.  
  357. private int LoadMoves()
  358. {
  359. return PlayerPrefs.GetInt("moves", 0);
  360. }
  361.  
  362. private void SetMoves(int moves)
  363. {
  364. this.moves = moves;
  365. if (PlayerPrefs.HasKey("moves"))
  366. {
  367. moves = LoadMoves();
  368. }
  369. else
  370. {
  371. moves = 0; // Set to 0 if no saved value exists (first game)
  372. }
  373. movesText.text = moves.ToString();
  374. }
  375. }
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement