Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 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 movesCount = 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. movesCount++;
  119. PlayerPrefs.SetInt("moves", movesCount);
  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. using System.Collections;
  259. using System.Collections.Generic;
  260. using UnityEngine;
  261. using TMPro;
  262.  
  263. public class GameManager : MonoBehaviour
  264. {
  265. public TileBoard board;
  266. public CanvasGroup gameOver;
  267. public TextMeshProUGUI scoreText;
  268. public TextMeshProUGUI hiscoreText;
  269. public TextMeshProUGUI movesText;
  270.  
  271.  
  272. private int score;
  273. private int moves;
  274.  
  275.  
  276. private void Start()
  277. {
  278. if (PlayerPrefs.HasKey("moves"))
  279. {
  280. PlayerPrefs.SetInt("moves", 0);
  281. }
  282. NewGame();
  283. }
  284.  
  285. private void Update()
  286. {
  287. SetMoves(0);
  288. }
  289.  
  290. public void NewGame()
  291. {
  292. SetScore(0);
  293. //SetMoves(0);
  294. hiscoreText.text = LoadHiscore().ToString();
  295.  
  296. gameOver.alpha = 0f;
  297. gameOver.interactable = false;
  298.  
  299. board.ClearBoard();
  300. board.CreateTile();
  301. board.CreateTile();
  302. board.enabled = true;
  303.  
  304. if (PlayerPrefs.HasKey("moves"))
  305. {
  306. PlayerPrefs.SetInt("moves", 0);
  307. }
  308. moves = 0;
  309. }
  310.  
  311. public void GameOver()
  312. {
  313. board.enabled = false;
  314. gameOver.interactable = true;
  315.  
  316. StartCoroutine(Fade(gameOver, 1f, 1f));
  317. }
  318.  
  319. private IEnumerator Fade(CanvasGroup canvasGroup, float to, float delay)
  320. {
  321. yield return new WaitForSeconds(delay);
  322.  
  323. float elapsed = 0f;
  324. float duration = 0.5f;
  325. float from = canvasGroup.alpha;
  326.  
  327. while (elapsed < duration)
  328. {
  329. canvasGroup.alpha = Mathf.Lerp(from, to, elapsed / duration);
  330. elapsed += Time.deltaTime;
  331. yield return null;
  332. }
  333.  
  334. canvasGroup.alpha = to;
  335. }
  336.  
  337. public void IncreaseScore(int points)
  338. {
  339. SetScore(score + points);
  340. }
  341.  
  342. private void SetScore(int score)
  343. {
  344. this.score = score;
  345. scoreText.text = score.ToString();
  346.  
  347. SaveHiscore();
  348. }
  349.  
  350. private void SaveHiscore()
  351. {
  352. int hiscore = LoadHiscore();
  353.  
  354. if (score > hiscore)
  355. {
  356. PlayerPrefs.SetInt("hiscore", score);
  357. }
  358. }
  359.  
  360. private int LoadHiscore()
  361. {
  362. return PlayerPrefs.GetInt("hiscore", 0);
  363. }
  364.  
  365. private int LoadMoves()
  366. {
  367. return PlayerPrefs.GetInt("moves", 0);
  368. }
  369.  
  370. private void SetMoves(int inMoves)
  371. {
  372. this.moves = inMoves;
  373. if (PlayerPrefs.HasKey("moves"))
  374. {
  375. moves = LoadMoves();
  376. }
  377. else
  378. {
  379. moves = 0; // Set to 0 if no saved value exists (first game)
  380. }
  381. movesText.text = moves.ToString();
  382. }
  383. }
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement