149 lines
5.8 KiB
C#
149 lines
5.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Enums;
|
|
using Models.Interfaces;
|
|
using Services.Interfaces;
|
|
using UnityEngine;
|
|
|
|
namespace Services {
|
|
public class MatchService : IMatchService {
|
|
private List<Gem> currentMatches = new List<Gem>();
|
|
public List<Gem> CurrentMatches => this.currentMatches;
|
|
|
|
private IGameBoard gameBoard;
|
|
|
|
public MatchService(IGameBoard gameBoard) {
|
|
this.gameBoard = gameBoard;
|
|
}
|
|
|
|
public bool MatchesAt(Vector2Int positionToCheck, GemType gemTypeToCheck) {
|
|
Gem[,] gems = this.gameBoard.GemsGrid;
|
|
if (positionToCheck.x > 1)
|
|
{
|
|
if (gems[positionToCheck.x - 1, positionToCheck.y].Type == gemTypeToCheck && gems[positionToCheck.x - 2, positionToCheck.y].Type == gemTypeToCheck)
|
|
return true;
|
|
}
|
|
|
|
if (positionToCheck.y > 1)
|
|
{
|
|
if (gems[positionToCheck.x, positionToCheck.y - 1].Type == gemTypeToCheck && gems[positionToCheck.x, positionToCheck.y - 2].Type == gemTypeToCheck)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void FindAllMatches() {
|
|
this.currentMatches.Clear();
|
|
|
|
for (int x = 0; x < this.gameBoard.Width; x++)
|
|
for (int y = 0; y < this.gameBoard.Height; y++)
|
|
{
|
|
Gem currentGem = this.gameBoard.GemsGrid[x, y];
|
|
if (currentGem != null)
|
|
{
|
|
if (x > 0 && x < this.gameBoard.Width - 1)
|
|
{
|
|
Gem leftGem = this.gameBoard.GemsGrid[x - 1, y];
|
|
Gem rightGem = this.gameBoard.GemsGrid[x + 1, y];
|
|
//checking no empty spots
|
|
if (leftGem != null && rightGem != null)
|
|
{
|
|
//Match
|
|
if (leftGem.Type == currentGem.Type && rightGem.Type == currentGem.Type)
|
|
{
|
|
currentGem.isMatch = true;
|
|
leftGem.isMatch = true;
|
|
rightGem.isMatch = true;
|
|
this.currentMatches.Add(currentGem);
|
|
this.currentMatches.Add(leftGem);
|
|
this.currentMatches.Add(rightGem);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (y > 0 && y < this.gameBoard.Height - 1)
|
|
{
|
|
Gem aboveGem = this.gameBoard.GemsGrid[x, y - 1];
|
|
Gem bellowGem = this.gameBoard.GemsGrid[x, y + 1];
|
|
//checking no empty spots
|
|
if (aboveGem != null && bellowGem != null)
|
|
{
|
|
//Match
|
|
if (aboveGem.Type == currentGem.Type && bellowGem.Type == currentGem.Type)
|
|
{
|
|
currentGem.isMatch = true;
|
|
aboveGem.isMatch = true;
|
|
bellowGem.isMatch = true;
|
|
this.currentMatches.Add(currentGem);
|
|
this.currentMatches.Add(aboveGem);
|
|
this.currentMatches.Add(bellowGem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();
|
|
|
|
CheckForBombs();
|
|
}
|
|
|
|
private void CheckForBombs() {
|
|
Gem[,] gems = this.gameBoard.GemsGrid;
|
|
int width = this.gameBoard.Width;
|
|
int height = this.gameBoard.Height;
|
|
|
|
for (int i = 0; i < this.currentMatches.Count; i++)
|
|
{
|
|
Gem gem = this.currentMatches[i];
|
|
int x = gem.Position.x;
|
|
int y = gem.Position.y;
|
|
|
|
Vector2Int[] directions =
|
|
{
|
|
Vector2Int.left,
|
|
Vector2Int.right,
|
|
Vector2Int.down,
|
|
Vector2Int.up
|
|
};
|
|
|
|
foreach (Vector2Int direction in directions)
|
|
{
|
|
int newX = x + direction.x;
|
|
int newY = y + direction.y;
|
|
|
|
if (newX < 0 || newX >= width || newY < 0 || newY >= height)
|
|
continue;
|
|
|
|
Gem neighbor = gems[newX, newY];
|
|
if (neighbor?.Type == GemType.Bomb)
|
|
MarkBombArea(new Vector2Int(newX, newY), 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void MarkBombArea(Vector2Int bombPosition, int blastSize) {
|
|
Gem[,] gems = this.gameBoard.GemsGrid;
|
|
int width = this.gameBoard.Width;
|
|
int height = this.gameBoard.Height;
|
|
|
|
for (int x = bombPosition.x - blastSize; x <= bombPosition.x + blastSize; x++)
|
|
{
|
|
for (int y = bombPosition.y - blastSize; y <= bombPosition.y + blastSize; y++)
|
|
{
|
|
if (x >= 0 && x < width && y >= 0 && y < height)
|
|
{
|
|
if (gems[x, y] != null)
|
|
{
|
|
gems[x, y].isMatch = true;
|
|
this.currentMatches.Add(gems[x, y]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
this.currentMatches = this.currentMatches.Distinct().ToList();
|
|
}
|
|
}
|
|
} |