This commit is contained in:
2025-12-15 03:44:53 +08:00
parent 25a5950e8d
commit 2a3bcf7423
12 changed files with 326 additions and 107 deletions

View File

@@ -7,6 +7,7 @@ using Models.Interfaces;
using Presenter;
using ScriptableObjects;
using Services.Interfaces;
using Structs;
using UnityEngine;
using Utils;
using VContainer.Unity;
@@ -43,10 +44,10 @@ namespace Services {
public void Tick() {
foreach (GemPresenter gemPresenter in gemPresenters) {
gemPresenter.Tick();
gemPresenter.Tick(this.gameVariables.gemSpeed);
}
this.scorePresenter.Tick();
this.scorePresenter.Tick(this.gameVariables.scoreSpeed);
}
//Instantiates background tiles and calls SpawnGems
@@ -77,16 +78,32 @@ namespace Services {
//Uses the ObjectPool to spawn a gem at the given position
private void SpawnGem(Vector2Int position, GemType gemType) {
if (Random.Range(0, 100f) < this.gameVariables.bombChance)
gemType = GemType.Bomb;
GemView gemView = this.objectPool.Get(gemType, position, this.gameVariables.dropHeight);
gemView.name = "Gem - " + position.x + ", " + position.y + ' ' + gemType;
Gem gem = new Gem(gemType, position, 50);
// If we randomly spawned a bomb, give it a random color group (so it can match by color).
int scoreValue = GemUtils.GetGemValues(gemType, this.gameVariables.gemsPrefabs).scoreValue;
Gem gem = new Gem(gemType, position, scoreValue);
gemView.Bind(gem);
this.gemPresenters.Add(new GemPresenter(gem, gemView));
SetGem(new Vector2Int(position.x,position.y), gem);
SetGem(new Vector2Int(position.x, position.y), gem);
}
private void SpawnBomb(Vector2Int position, GemType color) {
// remove existing gem/view at that position
DestroyMatchedGems(position);
GemView gemView = this.objectPool.Get(GemType.Bomb, position, this.gameVariables.dropHeight);
gemView.name = "Gem - " + position.x + ", " + position.y + ' ' + GemType.Bomb;
int scoreValue = GemUtils.GetGemValues(color, this.gameVariables.gemsPrefabs).scoreValue;
Gem bombGem = new Gem(GemType.Bomb, position, scoreValue, color);
gemView.Bind(bombGem);
this.gemPresenters.Add(new GemPresenter(bombGem, gemView));
SetGem(position, bombGem);
}
//Sets the gem on the GameBoard
@@ -121,7 +138,7 @@ namespace Services {
ApplySwap(from, to, fromGem, toGem);
await UniTask.Delay(600);
this.matchService.SetLastSwap(from, to);
this.matchService.FindAllMatches();
bool hasMatch = this.matchService.CurrentMatches.Count > 0;
@@ -131,8 +148,10 @@ namespace Services {
this.currentState = GameState.Move;
return false;
}
List<Vector2Int> protectedPositions = ApplyPendingBombSpawns();
DestroyMatches();
await DestroyMatchesAsync(protectedPositions);
this.currentState = GameState.Move;
return true;
}
@@ -146,17 +165,131 @@ namespace Services {
SetGem(posA, gemB);
SetGem(posB, gemA);
}
private List<Vector2Int> ApplyPendingBombSpawns() {
List<Vector2Int> positions = new List<Vector2Int>();
//If there are matches, destroys them and moves the gems down
private void DestroyMatches() {
for (int i = 0; i < this.matchService.CurrentMatches.Count; i++)
if (this.matchService.CurrentMatches[i] != null)
{
this.scoreService.ScoreCheck(this.matchService.CurrentMatches[i].ScoreValue);
DestroyMatchedGems(this.matchService.CurrentMatches[i].Position);
foreach (BombSpawnRequest bomSpawnRequest in this.matchService.PendingBombSpawns) {
positions.Add(bomSpawnRequest.Position);
SpawnBomb(bomSpawnRequest.Position, bomSpawnRequest.Color);
}
this.matchService.ClearPendingBombs();
return positions;
}
private async UniTask DestroyMatchesAsync(List<Vector2Int> protectedPositions) {
// Build initial queues from current matches
Queue<Vector2Int> bombsToProcess = new Queue<Vector2Int>();
List<Vector2Int> processedBombs = new List<Vector2Int>();
List<Vector2Int> regularToDestroy = new List<Vector2Int>();
for (int i = 0; i < this.matchService.CurrentMatches.Count; i++) {
Gem matchedGem = this.matchService.CurrentMatches[i];
if (matchedGem == null)
continue;
Vector2Int pos = matchedGem.Position;
// If a bomb was spawned at this cell due to 4+ creation, it must survive this destruction pass.
if (protectedPositions != null && protectedPositions.Contains(pos))
continue;
Gem current = GetGem(pos);
if (current == null)
continue;
if (current.Type == GemType.Bomb) {
bombsToProcess.Enqueue(pos);
} else {
regularToDestroy.Add(pos);
}
}
// Process bombs: neighbors first (after delay), then the bomb itself (after delay).
while (bombsToProcess.Count > 0) {
Vector2Int bombPos = bombsToProcess.Dequeue();
if (processedBombs.Contains(bombPos))
continue;
Gem bomb = GetGem(bombPos);
if (bomb is not { Type: GemType.Bomb })
continue;
processedBombs.Add(bombPos);
// Delay before destroying neighbor group
if (this.gameVariables.bombDelay > 0f) {
int msDelay = Mathf.RoundToInt(this.gameVariables.bombDelay * 1000f);
await UniTask.Delay(msDelay);
}
MoveGemsDown();
// Collect cross neighbors
foreach (Vector2Int neighborPosition in CrossNeighbors(bombPos, this.gameVariables.bombRadius)) {
if (!InBounds(neighborPosition))
continue;
Gem g = GetGem(neighborPosition);
if (g == null)
continue;
// If we encounter another bomb, queue it (so it explodes too).
if (g.Type == GemType.Bomb) {
if (!processedBombs.Contains(neighborPosition))
bombsToProcess.Enqueue(neighborPosition);
continue;
}
regularToDestroy.Add(neighborPosition);
}
// Destroy the neighbor group now
foreach (Vector2Int position in regularToDestroy.ToList()) {
Gem gem = GetGem(position);
if (gem == null)
continue;
this.scoreService.ScoreCheck(gem.ScoreValue);
DestroyMatchedGems(position);
}
regularToDestroy.Clear();
// Delay before destroying the bomb itself
if (this.gameVariables.bombSelfDelay > 0f) {
int ms = Mathf.RoundToInt(this.gameVariables.bombSelfDelay * 1000f);
await UniTask.Delay(ms);
}
// Destroy the bomb
Gem b = GetGem(bombPos);
if (b != null && b.Type == GemType.Bomb) {
this.scoreService.ScoreCheck(b.ScoreValue);
DestroyMatchedGems(bombPos);
}
}
// Destroy any remaining regular matches (non-bomb) after bomb processing
foreach (Vector2Int pos in regularToDestroy) {
Gem g = GetGem(pos);
if (g == null)
continue;
this.scoreService.ScoreCheck(g.ScoreValue);
DestroyMatchedGems(pos);
}
// Now we can cascade
await MoveGemsDown();
}
private static IEnumerable<Vector2Int> CrossNeighbors(Vector2Int center, int radius) {
// center excluded for "neighbors first"
for (int i = 1; i <= radius; i++) {
yield return center + Vector2Int.left * i;
yield return center + Vector2Int.right * i;
yield return center + Vector2Int.up * i;
yield return center + Vector2Int.down * i;
}
}
private async UniTask MoveGemsDown() {
@@ -189,14 +322,14 @@ namespace Services {
await UniTask.Delay(5);
RefillBoard();
await UniTask.Delay(5);
this.matchService.FindAllMatches();
if (this.matchService.CurrentMatches.Count > 0)
{
if (this.matchService.CurrentMatches.Count > 0) {
await UniTask.Delay(5);
DestroyMatches();
}
else
{
// In cascades, there is no "creating slot" bomb protection.
await DestroyMatchesAsync(new List<Vector2Int>());
} else {
await UniTask.Delay(5);
this.currentState = GameState.Move;
}

View File

@@ -1,12 +1,15 @@
using System.Collections.Generic;
using UnityEngine;
using Enums;
using Structs;
namespace Services.Interfaces {
public interface IMatchService {
List<Gem> CurrentMatches { get; }
IReadOnlyList<BombSpawnRequest> PendingBombSpawns { get; }
bool MatchesAt(Vector2Int positionToCheck, GemType gemTypeToCheck);
void FindAllMatches();
void MarkBombArea(Vector2Int bombPosition, int blastSize);
void SetLastSwap(Vector2Int from, Vector2Int to);
void ClearPendingBombs();
}
}

View File

@@ -3,21 +3,41 @@ using System.Linq;
using Enums;
using Models.Interfaces;
using Services.Interfaces;
using Structs;
using UnityEngine;
namespace Services {
public class MatchService : IMatchService {
private List<Gem> currentMatches = new List<Gem>();
public List<Gem> CurrentMatches => this.currentMatches;
public List<BombSpawnRequest> pendingBombSpawns = new List<BombSpawnRequest>();
public IReadOnlyList<BombSpawnRequest> PendingBombSpawns => this.pendingBombSpawns;
private Vector2Int lastSwapFrom;
private Vector2Int lastSwapTo;
private IGameBoard gameBoard;
public MatchService(IGameBoard gameBoard) {
this.gameBoard = gameBoard;
}
public void SetLastSwap(Vector2Int from, Vector2Int to) {
this.lastSwapFrom = from;
this.lastSwapTo = to;
}
public void ClearPendingBombs() {
this.pendingBombSpawns.Clear();
}
public bool MatchesAt(Vector2Int positionToCheck, GemType gemTypeToCheck) {
Gem[,] gems = this.gameBoard.GemsGrid;
if (gemTypeToCheck == GemType.Bomb)
return false;
if (positionToCheck.x > 1)
{
if (gems[positionToCheck.x - 1, positionToCheck.y].Type == gemTypeToCheck && gems[positionToCheck.x - 2, positionToCheck.y].Type == gemTypeToCheck)
@@ -35,59 +55,102 @@ namespace Services {
public void FindAllMatches() {
this.currentMatches.Clear();
this.pendingBombSpawns.Clear();
for (int x = 0; x < this.gameBoard.Width; x++)
for (int y = 0; y < this.gameBoard.Height; y++)
{
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 (currentGem == null)
continue;
if (x > 0 && x < this.gameBoard.Width - 1) {
Gem leftGem = this.gameBoard.GemsGrid[x - 1, y];
Gem rightGem = this.gameBoard.GemsGrid[x + 1, y];
if (leftGem != null && rightGem != null) {
if (leftGem.MatchColor == currentGem.MatchColor && rightGem.MatchColor == currentGem.MatchColor) {
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 (y > 0 && y < this.gameBoard.Height - 1) {
Gem aboveGem = this.gameBoard.GemsGrid[x, y - 1];
Gem bellowGem = this.gameBoard.GemsGrid[x, y + 1];
if (aboveGem != null && bellowGem != null) {
if (aboveGem.MatchColor == currentGem.MatchColor && bellowGem.MatchColor == currentGem.MatchColor) {
this.currentMatches.Add(currentGem);
this.currentMatches.Add(aboveGem);
this.currentMatches.Add(bellowGem);
}
}
}
}
if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();
if (this.currentMatches.Count > 0)
this.currentMatches = this.currentMatches.Distinct().ToList();
DetectBombSpawnFromLastSwap();
CheckForBombs();
}
private void DetectBombSpawnFromLastSwap() {
Vector2Int from = this.lastSwapFrom;
Vector2Int to = this.lastSwapTo;
TryCreateBombSpawnAt(from);
TryCreateBombSpawnAt(to);
}
private void TryCreateBombSpawnAt(Vector2Int pivot) {
Gem pivotGem = this.gameBoard.GetGemAt(pivot);
if (pivotGem == null)
return;
// If it's already a bomb, don't create another.
if (pivotGem.Type == GemType.Bomb)
return;
if (this.currentMatches.All(g => g.Position != pivot))
return;
int horizontal = CountLine(pivot, Vector2Int.left) + 1 + CountLine(pivot, Vector2Int.right);
int vertical = CountLine(pivot, Vector2Int.up) + 1 + CountLine(pivot, Vector2Int.down);
int best = Mathf.Max(horizontal, vertical);
if (best < 4)
return;
// Spawn a bomb on the creating slot with the same color group.
// Prevent duplicates for the same cell.
if (this.pendingBombSpawns.Any(b => b.Position == pivot))
return;
this.pendingBombSpawns.Add(new BombSpawnRequest(pivot, pivotGem.MatchColor));
}
private int CountLine(Vector2Int start, Vector2Int direction) {
Gem startGem = this.gameBoard.GetGemAt(start);
if (startGem == null)
return 0;
GemType color = startGem.MatchColor;
int count = 0;
Vector2Int position = start + direction;
while (position.x >= 0 && position.x < this.gameBoard.Width && position.y >= 0 && position.y < this.gameBoard.Height) {
Gem g = this.gameBoard.GetGemAt(position);
if (g == null || g.MatchColor != color)
break;
count++;
position += direction;
}
return count;
}
private void CheckForBombs() {
Gem[,] gems = this.gameBoard.GemsGrid;
@@ -118,32 +181,38 @@ namespace Services {
Gem neighbor = gems[newX, newY];
if (neighbor?.Type == GemType.Bomb)
MarkBombArea(new Vector2Int(newX, newY), 1);
MarkBombCross(new Vector2Int(newX, newY), 1);
}
}
}
public void MarkBombArea(Vector2Int bombPosition, int blastSize) {
private void MarkBombCross(Vector2Int bombPosition, int radius) {
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]);
}
}
}
void Mark(Vector2Int p) {
if (p.x < 0 || p.x >= width || p.y < 0 || p.y >= height)
return;
Gem g = gems[p.x, p.y];
if (g == null)
return;
this.currentMatches.Add(g);
}
Mark(bombPosition);
for (int i = 1; i <= radius; i++) {
Mark(bombPosition + Vector2Int.left * i);
Mark(bombPosition + Vector2Int.right * i);
Mark(bombPosition + Vector2Int.up * i);
Mark(bombPosition + Vector2Int.down * i);
}
this.currentMatches = this.currentMatches.Distinct().ToList();
}
}
}