Cleanup
This commit is contained in:
@@ -4,7 +4,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Enums;
|
||||
using Models;
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ using Utils;
|
||||
using VContainer.Unity;
|
||||
using Views;
|
||||
using Object = UnityEngine.Object;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Services {
|
||||
public class GameBoardService : IGameBoardService, ITickable, IDisposable {
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Services {
|
||||
|
||||
private void TryEmitSwap(Vector2 downScreen, Vector2 upScreen)
|
||||
{
|
||||
if (this.inputCamera == null) return;
|
||||
if (this.inputCamera is null) return;
|
||||
|
||||
Vector2 downWorld = this.inputCamera.ScreenToWorldPoint(downScreen);
|
||||
Vector2 upWorld = this.inputCamera.ScreenToWorldPoint(upScreen);
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Enums;
|
||||
using UnityEngine;
|
||||
using Views;
|
||||
|
||||
namespace Services.Interfaces {
|
||||
public interface IGameBoardService {
|
||||
|
||||
@@ -11,13 +11,13 @@ namespace Services {
|
||||
private List<Gem> currentMatches = new List<Gem>();
|
||||
public List<Gem> CurrentMatches => this.currentMatches;
|
||||
|
||||
public List<BombSpawnRequest> pendingBombSpawns = new List<BombSpawnRequest>();
|
||||
private readonly List<BombSpawnRequest> pendingBombSpawns = new List<BombSpawnRequest>();
|
||||
public IReadOnlyList<BombSpawnRequest> PendingBombSpawns => this.pendingBombSpawns;
|
||||
|
||||
private Vector2Int lastSwapFrom;
|
||||
private Vector2Int lastSwapTo;
|
||||
|
||||
private IGameBoard gameBoard;
|
||||
private readonly IGameBoard gameBoard;
|
||||
|
||||
public MatchService(IGameBoard gameBoard) {
|
||||
this.gameBoard = gameBoard;
|
||||
@@ -125,9 +125,9 @@ namespace Services {
|
||||
if (this.currentMatches.All(g => g.Position != pivot))
|
||||
return;
|
||||
|
||||
// If the matched group that includes this pivot has 4+ connected gems, spawn a bomb.
|
||||
int groupSize = GetMatchedGroupSize(pivot);
|
||||
if (groupSize < 4)
|
||||
// Only create a bomb if pivot is part of a straight 4+ line of the SAME color.
|
||||
int longestLine = GetLongestMatchedLineThroughPivot(pivot, pivotGem.MatchColor);
|
||||
if (longestLine < 4)
|
||||
return;
|
||||
|
||||
// Prevent duplicates for the same cell.
|
||||
@@ -137,52 +137,30 @@ namespace Services {
|
||||
this.pendingBombSpawns.Add(new BombSpawnRequest(pivot, pivotGem.MatchColor));
|
||||
}
|
||||
|
||||
private int GetMatchedGroupSize(Vector2Int pivot) {
|
||||
Gem pivotGem = this.gameBoard.GetGemAt(pivot);
|
||||
if (pivotGem == null)
|
||||
return 0;
|
||||
private int GetLongestMatchedLineThroughPivot(Vector2Int pivot, GemType color) {
|
||||
int horizontal = 1 + CountSameColorInDirection(pivot, Vector2Int.left, color)
|
||||
+ CountSameColorInDirection(pivot, Vector2Int.right, color);
|
||||
|
||||
GemType color = pivotGem.MatchColor;
|
||||
int vertical = 1 + CountSameColorInDirection(pivot, Vector2Int.up, color)
|
||||
+ CountSameColorInDirection(pivot, Vector2Int.down, color);
|
||||
|
||||
HashSet<Vector2Int> matchedPositions = new HashSet<Vector2Int>(
|
||||
this.currentMatches
|
||||
.Where(g => g != null && g.MatchColor == color)
|
||||
.Select(g => g.Position)
|
||||
);
|
||||
return Mathf.Max(horizontal, vertical);
|
||||
}
|
||||
|
||||
if (!matchedPositions.Contains(pivot))
|
||||
return 0;
|
||||
private int CountSameColorInDirection(Vector2Int start, Vector2Int direction, GemType color) {
|
||||
int count = 0;
|
||||
Vector2Int oivot = start + direction;
|
||||
|
||||
Queue<Vector2Int> queue = new Queue<Vector2Int>();
|
||||
HashSet<Vector2Int> visited = new HashSet<Vector2Int>();
|
||||
while (oivot.x >= 0 && oivot.x < this.gameBoard.Width && oivot.y >= 0 && oivot.y < this.gameBoard.Height) {
|
||||
Gem g = this.gameBoard.GetGemAt(oivot);
|
||||
if (g == null || g.Type == GemType.Bomb || g.MatchColor != color)
|
||||
break;
|
||||
|
||||
queue.Enqueue(pivot);
|
||||
visited.Add(pivot);
|
||||
|
||||
Vector2Int[] directions = {
|
||||
Vector2Int.left,
|
||||
Vector2Int.right,
|
||||
Vector2Int.up,
|
||||
Vector2Int.down
|
||||
};
|
||||
|
||||
while (queue.Count > 0) {
|
||||
Vector2Int currentPivot = queue.Dequeue();
|
||||
|
||||
for (int i = 0; i < directions.Length; i++) {
|
||||
Vector2Int n = currentPivot + directions[i];
|
||||
if (visited.Contains(n))
|
||||
continue;
|
||||
|
||||
if (!matchedPositions.Contains(n))
|
||||
continue;
|
||||
|
||||
visited.Add(n);
|
||||
queue.Enqueue(n);
|
||||
}
|
||||
count++;
|
||||
oivot += direction;
|
||||
}
|
||||
|
||||
return visited.Count;
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user