Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SocialPlatforms.Impl;
- public class TileBoard : MonoBehaviour
- {
- public GameManager gameManager;
- public Tile tilePrefab;
- public TileState[] tileStates;
- private TileGrid grid;
- private List<Tile> tiles;
- private bool waiting;
- SwipeDetector swipeDetector;
- public int movesCount = 0;
- private void Awake()
- {
- grid = GetComponentInChildren<TileGrid>();
- tiles = new List<Tile>(96);
- swipeDetector = GetComponent<SwipeDetector>();
- }
- public void ClearBoard()
- {
- foreach (var cell in grid.cells)
- {
- cell.tile = null;
- }
- foreach (var tile in tiles)
- {
- Destroy(tile.gameObject);
- }
- tiles.Clear();
- }
- public void CreateTile()
- {
- Tile tile = Instantiate(tilePrefab, grid.transform);
- tile.SetState(tileStates[0], 2);
- tile.Spawn(grid.GetRandomEmptyCell());
- tiles.Add(tile);
- }
- private void Update()
- {
- //PC
- if (!waiting)
- {
- if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
- {
- MoveTiles(Vector2Int.up, 0, 1, 1, 1);
- }
- else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
- {
- MoveTiles(Vector2Int.down, 0, 1, grid.height - 2, -1);
- }
- else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
- {
- MoveTiles(Vector2Int.left, 1, 1, 0, 1);
- }
- else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
- {
- MoveTiles(Vector2Int.right, grid.width - 2, -1, 0, 1);
- }
- }
- //Mobile Swipe
- if (!waiting)
- {
- if (SwipeDetector.instance.swipeDirection == "Up")
- {
- MoveTiles(Vector2Int.up, 0, 1, 1, 1);
- }
- else if (SwipeDetector.instance.swipeDirection == "Down")
- {
- MoveTiles(Vector2Int.down, 0, 1, grid.height - 2, -1);
- }
- else if (SwipeDetector.instance.swipeDirection == "Left")
- {
- MoveTiles(Vector2Int.left, 1, 1, 0, 1);
- }
- else if (SwipeDetector.instance.swipeDirection == "Right")
- {
- MoveTiles(Vector2Int.right, grid.width - 2, -1, 0, 1);
- }
- }
- SwipeDetector.instance.swipeDirection = null;
- }
- private void MoveTiles(Vector2Int direction, int startX, int incrementX, int startY, int incrementY)
- {
- bool changed = false;
- for(int x = startX; x >= 0 && x < grid.width; x += incrementX)
- {
- for(int y = startY; y >= 0 && y < grid.height; y += incrementY)
- {
- TileCell cell = grid.GetCell(x, y);
- if (cell.occupied)
- {
- changed |= MoveTile(cell.tile, direction);
- }
- }
- }
- if (changed)
- {
- StartCoroutine(WaitForChanges());
- movesCount++;
- PlayerPrefs.SetInt("moves", movesCount);
- }
- }
- private bool MoveTile(Tile tile, Vector2Int direction)
- {
- TileCell newCell = null;
- TileCell adjacent = grid.GetAdjacentCell(tile.cell, direction);
- while (adjacent != null)
- {
- if (adjacent.occupied)
- {
- if (CanMerge(tile, adjacent.tile))
- {
- Merge(tile, adjacent.tile);
- return true;
- }
- break;
- }
- newCell = adjacent;
- adjacent = grid.GetAdjacentCell(adjacent, direction);
- }
- if (newCell != null)
- {
- tile.MoveTo(newCell);
- return true;
- }
- return false;
- }
- private bool CanMerge(Tile a, Tile b)
- {
- return a.number == b.number && !b.locked;
- }
- private void Merge(Tile a, Tile b)
- {
- tiles.Remove(a);
- a.Merge(b.cell);
- int index = Mathf.Clamp(IndexOf(b.state) + 1, 0, tileStates.Length - 1);
- int number = b.number * 2;
- b.SetState(tileStates[index], number);
- gameManager.IncreaseScore(number);
- }
- private int IndexOf(TileState state)
- {
- for (int i = 0; i < tileStates.Length; i++)
- {
- if (state == tileStates[i])
- {
- return i;
- }
- }
- return -1;
- }
- private IEnumerator WaitForChanges()
- {
- waiting = true;
- yield return new WaitForSeconds(0.1f);
- waiting = false;
- foreach (var tile in tiles)
- {
- tile.locked = false;
- }
- if (tiles.Count != grid.size)
- {
- CreateTile();
- }
- if (CheckForGameOver())
- {
- gameManager.GameOver();
- }
- }
- private bool CheckForGameOver()
- {
- if (tiles.Count != grid.size)
- {
- return false;
- }
- foreach (var tile in tiles)
- {
- TileCell up = grid.GetAdjacentCell(tile.cell, Vector2Int.up);
- TileCell down = grid.GetAdjacentCell(tile.cell, Vector2Int.down);
- TileCell left = grid.GetAdjacentCell(tile.cell, Vector2Int.left);
- TileCell right = grid.GetAdjacentCell(tile.cell, Vector2Int.right);
- if (up != null && CanMerge(tile, up.tile))
- {
- return false;
- }
- if (down != null && CanMerge(tile, down.tile))
- {
- return false;
- }
- if (left != null && CanMerge(tile, left.tile))
- {
- return false;
- }
- if (right != null && CanMerge(tile, right.tile))
- {
- return false;
- }
- }
- return true;
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class GameManager : MonoBehaviour
- {
- public TileBoard board;
- public CanvasGroup gameOver;
- public TextMeshProUGUI scoreText;
- public TextMeshProUGUI hiscoreText;
- public TextMeshProUGUI movesText;
- private int score;
- private int moves;
- private void Start()
- {
- if (PlayerPrefs.HasKey("moves"))
- {
- PlayerPrefs.SetInt("moves", 0);
- }
- NewGame();
- }
- private void Update()
- {
- SetMoves(0);
- }
- public void NewGame()
- {
- SetScore(0);
- //SetMoves(0);
- hiscoreText.text = LoadHiscore().ToString();
- gameOver.alpha = 0f;
- gameOver.interactable = false;
- board.ClearBoard();
- board.CreateTile();
- board.CreateTile();
- board.enabled = true;
- if (PlayerPrefs.HasKey("moves"))
- {
- PlayerPrefs.SetInt("moves", 0);
- }
- moves = 0;
- }
- public void GameOver()
- {
- board.enabled = false;
- gameOver.interactable = true;
- StartCoroutine(Fade(gameOver, 1f, 1f));
- }
- private IEnumerator Fade(CanvasGroup canvasGroup, float to, float delay)
- {
- yield return new WaitForSeconds(delay);
- float elapsed = 0f;
- float duration = 0.5f;
- float from = canvasGroup.alpha;
- while (elapsed < duration)
- {
- canvasGroup.alpha = Mathf.Lerp(from, to, elapsed / duration);
- elapsed += Time.deltaTime;
- yield return null;
- }
- canvasGroup.alpha = to;
- }
- public void IncreaseScore(int points)
- {
- SetScore(score + points);
- }
- private void SetScore(int score)
- {
- this.score = score;
- scoreText.text = score.ToString();
- SaveHiscore();
- }
- private void SaveHiscore()
- {
- int hiscore = LoadHiscore();
- if (score > hiscore)
- {
- PlayerPrefs.SetInt("hiscore", score);
- }
- }
- private int LoadHiscore()
- {
- return PlayerPrefs.GetInt("hiscore", 0);
- }
- private int LoadMoves()
- {
- return PlayerPrefs.GetInt("moves", 0);
- }
- private void SetMoves(int inMoves)
- {
- this.moves = inMoves;
- if (PlayerPrefs.HasKey("moves"))
- {
- moves = LoadMoves();
- }
- else
- {
- moves = 0; // Set to 0 if no saved value exists (first game)
- }
- movesText.text = moves.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement