using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.UI;
public class MatchesChecker : MonoBehaviour
{
[SerializeField] private Cell[] cells;
[SerializeField] private Cell[,] gameField;
private List<GameObject> winGameObjects = new List<GameObject>();
private int width = 4;
private int height = 5;
[SerializeField] private Transform deadPosition;
[SerializeField] private GameObject windPartical;
public UnityEngine.UI.Button switchButton;
[SerializeField] private AudioSource audioSource;
[SerializeField] private AudioClip rockClip;
public static MatchesChecker Instance;
private void Awake()
{
if (Instance == null)
Instance = this;
}
private void Start()
{
gameField = new Cell[width, height];
for (int i = 0; i < cells.Length; i++)
{
gameField[cells[i].X, cells[i].Y] = cells[i];
}
}
public void ThreeInRow()
{
for (int i = 0; i < gameField.GetLength(0); i++)
for (int j = 0; j < gameField.GetLength(1); j++)
if (gameField[i, j].ItemInCell != null)
GetWinGameObjectThreeRow(i, j, gameField[i, j].GetComponent<Cell>().ItemInCell.gameObject);
if (winGameObjects.Count > 0)
{
if (winGameObjects.Count > 3)
{
GameManager.Instance.SetScore(20 * winGameObjects.Count);
}
else
{
GameManager.Instance.SetScore(10 * winGameObjects.Count);
}
}
foreach (var winGameObject in winGameObjects)
{
if (winGameObject != null)
{
audioSource.PlayOneShot(rockClip);
switchButton.interactable = false;
winGameObject.GetComponent<Item>().startDeadAnimation(deadPosition,windPartical,false);
Destroy(winGameObject);
}
}
winGameObjects.Clear();
}
private void GetWinGameObjectThreeRow(int x, int y, GameObject cell)
{
if (CheckVerticalLine(x, y))
{
if (!winGameObjects.Contains(cell)) winGameObjects.Add(cell);
if (!winGameObjects.Contains(gameField[x, y + 1].GetComponent<Cell>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x, y + 1].GetComponent<Cell>().ItemInCell.gameObject);
if (!winGameObjects.Contains(gameField[x, y - 1].GetComponent<Cell>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x, y - 1].GetComponent<Cell>().ItemInCell.gameObject);
}
if (HorizontalLine(x, y))
{
if (!winGameObjects.Contains(cell)) winGameObjects.Add(cell);
if (!winGameObjects.Contains(gameField[x + 1, y].GetComponent<Cell>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x + 1, y].GetComponent<Cell>().ItemInCell.gameObject);
if (!winGameObjects.Contains(gameField[x - 1, y].GetComponent<Cell>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x - 1, y].GetComponent<Cell>().ItemInCell.gameObject);
}
}
private bool CheckVerticalLine(int x, int y) =>
y + 1 < gameField.GetLength(1) && gameField[x, y + 1].ItemInCell != null && gameField[x, y + 1].GetComponent<Cell>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell>().ItemInCell.ItemID &&
y - 1 >= 0 && gameField[x, y - 1].ItemInCell != null && gameField[x, y - 1].GetComponent<Cell>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell>().ItemInCell.ItemID;
private bool HorizontalLine(int x, int y) =>
x + 1 < gameField.GetLength(0) && gameField[x + 1, y].ItemInCell != null && gameField[x + 1, y].GetComponent<Cell>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell>().ItemInCell.ItemID &&
x - 1 >= 0 && gameField[x - 1, y].ItemInCell != null && gameField[x - 1, y].GetComponent<Cell>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell>().ItemInCell.ItemID;
}