using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MatchesChecker2 : MonoBehaviour
{
[SerializeField] private Cell2[] cells2;
[SerializeField] private Cell2[,] gameField;
private List<GameObject> winGameObjects = new List<GameObject>();
private int width = 4;
private int height = 5;
public static MatchesChecker2 Instance;
[SerializeField] private Transform deadPosition;
[SerializeField] private GameObject windPartical;
[SerializeField] private AudioSource audioSource;
[SerializeField] private AudioClip rockClip;
private void Awake()
{
if (Instance == null)
Instance = this;
}
private void Start()
{
gameField = new Cell2[width, height];
for (int i = 0; i < cells2.Length; i++)
{
gameField[cells2[i].X, cells2[i].Y] = cells2[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<Cell2>().ItemInCell.gameObject);
if (winGameObjects.Count > 0)
{
if (winGameObjects.Count > 3)
{
print("match>3");
GameManager.Instance.SetScore(20 * winGameObjects.Count);
}
else
{
print("match=3");
GameManager.Instance.SetScore(10 * winGameObjects.Count);
}
}
foreach (var winGameObject in winGameObjects)
{
audioSource.PlayOneShot(rockClip);
winGameObject.GetComponent<Item>().startDeadAnimation(deadPosition, windPartical, true);
Destroy(winGameObject);
}
winGameObjects.Clear();
}
private void GetWinGameObjectThreeRow(int x, int y, GameObject cell)
{
//var x = cell.GetComponent<Cell>().X;
//var y = cell.GetComponent<Cell>().Y;
//if (CheckStay(x,y))
//{
if (CheckVerticalLine(x, y))
{
if (!winGameObjects.Contains(cell)) winGameObjects.Add(cell);
if (!winGameObjects.Contains(gameField[x, y + 1].GetComponent<Cell2>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x, y + 1].GetComponent<Cell2>().ItemInCell.gameObject);
if (!winGameObjects.Contains(gameField[x, y - 1].GetComponent<Cell2>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x, y - 1].GetComponent<Cell2>().ItemInCell.gameObject);
}
if (HorizontalLine(x, y))
{
if (!winGameObjects.Contains(cell)) winGameObjects.Add(cell);
if (!winGameObjects.Contains(gameField[x + 1, y].GetComponent<Cell2>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x + 1, y].GetComponent<Cell2>().ItemInCell.gameObject);
if (!winGameObjects.Contains(gameField[x - 1, y].GetComponent<Cell2>().ItemInCell.gameObject)) winGameObjects.Add(gameField[x - 1, y].GetComponent<Cell2>().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<Cell2>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell2>().ItemInCell.ItemID &&
y - 1 >= 0 && gameField[x, y - 1].ItemInCell != null && gameField[x, y - 1].GetComponent<Cell2>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell2>().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<Cell2>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell2>().ItemInCell.ItemID &&
x - 1 >= 0 && gameField[x - 1, y].ItemInCell != null && gameField[x - 1, y].GetComponent<Cell2>().ItemInCell.ItemID == gameField[x, y].GetComponent<Cell2>().ItemInCell.ItemID;
//private bool CheckStay(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 &&
//}
}