From 3786863b0058de75fed8c71d19b40bcf4d307573 Mon Sep 17 00:00:00 2001 From: Jesus Castro Date: Wed, 17 Dec 2025 00:55:30 +0800 Subject: [PATCH] Cleanup --- Assets/Scripts/Models/GameBoard.cs | 6 +- Assets/Scripts/Models/Gem.cs | 6 +- Assets/Scripts/Presenter/GemPresenter.cs | 6 +- Assets/Scripts/Presenter/ScorePresenter.cs | 5 +- Assets/Scripts/Scopes/LevelLifetimeScope.cs | 5 +- .../ScriptableObjects/GameVariables.cs | 4 -- Assets/Scripts/Services/BombService.cs | 1 - Assets/Scripts/Services/GameBoardService.cs | 1 - Assets/Scripts/Services/InputService.cs | 2 +- .../Services/Interfaces/IGameBoardService.cs | 2 - Assets/Scripts/Services/MatchService.cs | 66 +++++++------------ Assets/Scripts/Views/GemView.cs | 1 - Assets/Scripts/Views/ScoreView.cs | 5 +- 13 files changed, 37 insertions(+), 73 deletions(-) diff --git a/Assets/Scripts/Models/GameBoard.cs b/Assets/Scripts/Models/GameBoard.cs index a807444..a8dc6d8 100644 --- a/Assets/Scripts/Models/GameBoard.cs +++ b/Assets/Scripts/Models/GameBoard.cs @@ -3,10 +3,10 @@ using UnityEngine; namespace Services { public class GameBoard : IGameBoard { - private int height, width; + private readonly int height, width; public int Height => this.height; public int Width => this.width; - private Gem[,] gemsGrid; + private readonly Gem[,] gemsGrid; public Gem[,] GemsGrid => this.gemsGrid; public GameBoard(int width, int height) { @@ -17,7 +17,7 @@ namespace Services { public Gem GetGemAt(Vector2Int pos) { Gem gameObject = this.gemsGrid[pos.x, pos.y]; - return gameObject != null ? gameObject : null; + return gameObject; } public void SetGemAt(Vector2Int pos, Gem gameObject) { diff --git a/Assets/Scripts/Models/Gem.cs b/Assets/Scripts/Models/Gem.cs index f118c86..8b19954 100644 --- a/Assets/Scripts/Models/Gem.cs +++ b/Assets/Scripts/Models/Gem.cs @@ -4,15 +4,15 @@ using UnityEngine; namespace Services { public class Gem { - private GemType type; + private readonly GemType type; private Vector2Int position; public GemType Type => this.type; public Vector2Int Position => this.position; - private int scoreValue; + private readonly int scoreValue; public int ScoreValue => this.scoreValue; - private GemType colorType; + private readonly GemType colorType; public GemType MatchColor => this.type == GemType.Bomb ? this.colorType : this.type; public Gem(GemType type, Vector2Int position, GemTypeValues gemValue, GemType? colorType = null) { diff --git a/Assets/Scripts/Presenter/GemPresenter.cs b/Assets/Scripts/Presenter/GemPresenter.cs index 7205a6f..1b17e04 100644 --- a/Assets/Scripts/Presenter/GemPresenter.cs +++ b/Assets/Scripts/Presenter/GemPresenter.cs @@ -1,13 +1,11 @@ using Services; -using UnityEngine; using Utils; -using VContainer.Unity; using Views; namespace Presenter { public class GemPresenter { - private Gem gem; - private GemView gemView; + private readonly Gem gem; + private readonly GemView gemView; public Gem Gem => this.gem; public GemView GemView => this.gemView; diff --git a/Assets/Scripts/Presenter/ScorePresenter.cs b/Assets/Scripts/Presenter/ScorePresenter.cs index 2581acd..df43450 100644 --- a/Assets/Scripts/Presenter/ScorePresenter.cs +++ b/Assets/Scripts/Presenter/ScorePresenter.cs @@ -1,12 +1,11 @@ using System; using Services.Interfaces; -using VContainer.Unity; using Views; namespace Presenter { public class ScorePresenter : IDisposable{ - private IScoreService scoreService; - private ScoreView scoreView; + private readonly IScoreService scoreService; + private readonly ScoreView scoreView; public ScorePresenter(IScoreService scoreService, ScoreView scoreView) { this.scoreService = scoreService; this.scoreView = scoreView; diff --git a/Assets/Scripts/Scopes/LevelLifetimeScope.cs b/Assets/Scripts/Scopes/LevelLifetimeScope.cs index 24e5815..e646406 100644 --- a/Assets/Scripts/Scopes/LevelLifetimeScope.cs +++ b/Assets/Scripts/Scopes/LevelLifetimeScope.cs @@ -1,4 +1,3 @@ -using Models; using Models.Interfaces; using Presenter; using ScriptableObjects; @@ -23,14 +22,14 @@ namespace Scopes builder.RegisterComponentInHierarchy(); - builder.Register(c => + builder.Register(_ => new GameBoard(this.gameVariables.width, this.gameVariables.height), Lifetime.Scoped); builder.Register(Lifetime.Scoped); builder.Register(Lifetime.Scoped); - builder.Register>(c => + builder.Register>(_ => new ObjectPoolService(this.gameVariables.gemsPrefabs, this.gemsHolder), Lifetime.Scoped); diff --git a/Assets/Scripts/ScriptableObjects/GameVariables.cs b/Assets/Scripts/ScriptableObjects/GameVariables.cs index dd529bb..1d6406e 100644 --- a/Assets/Scripts/ScriptableObjects/GameVariables.cs +++ b/Assets/Scripts/ScriptableObjects/GameVariables.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using Enums; using Structs; using UnityEngine; -using Views; namespace ScriptableObjects { [CreateAssetMenu(fileName = "GameVariables", menuName = "Game Variables")] diff --git a/Assets/Scripts/Services/BombService.cs b/Assets/Scripts/Services/BombService.cs index c89a572..ed7012d 100644 --- a/Assets/Scripts/Services/BombService.cs +++ b/Assets/Scripts/Services/BombService.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using Cysharp.Threading.Tasks; using Enums; -using Models; using Services.Interfaces; using UnityEngine; diff --git a/Assets/Scripts/Services/GameBoardService.cs b/Assets/Scripts/Services/GameBoardService.cs index 6c22c54..1b7e062 100644 --- a/Assets/Scripts/Services/GameBoardService.cs +++ b/Assets/Scripts/Services/GameBoardService.cs @@ -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 { diff --git a/Assets/Scripts/Services/InputService.cs b/Assets/Scripts/Services/InputService.cs index 9d802de..fde99a8 100644 --- a/Assets/Scripts/Services/InputService.cs +++ b/Assets/Scripts/Services/InputService.cs @@ -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); diff --git a/Assets/Scripts/Services/Interfaces/IGameBoardService.cs b/Assets/Scripts/Services/Interfaces/IGameBoardService.cs index 1b8f724..143e627 100644 --- a/Assets/Scripts/Services/Interfaces/IGameBoardService.cs +++ b/Assets/Scripts/Services/Interfaces/IGameBoardService.cs @@ -1,7 +1,5 @@ using Cysharp.Threading.Tasks; -using Enums; using UnityEngine; -using Views; namespace Services.Interfaces { public interface IGameBoardService { diff --git a/Assets/Scripts/Services/MatchService.cs b/Assets/Scripts/Services/MatchService.cs index d634f1f..f45f738 100644 --- a/Assets/Scripts/Services/MatchService.cs +++ b/Assets/Scripts/Services/MatchService.cs @@ -11,13 +11,13 @@ namespace Services { private List currentMatches = new List(); public List CurrentMatches => this.currentMatches; - public List pendingBombSpawns = new List(); + private readonly List pendingBombSpawns = new List(); public IReadOnlyList 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 matchedPositions = new HashSet( - 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 queue = new Queue(); - HashSet visited = new HashSet(); + 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; } } } \ No newline at end of file diff --git a/Assets/Scripts/Views/GemView.cs b/Assets/Scripts/Views/GemView.cs index 61a71bc..18adfca 100644 --- a/Assets/Scripts/Views/GemView.cs +++ b/Assets/Scripts/Views/GemView.cs @@ -1,6 +1,5 @@ using System.Threading; using Cysharp.Threading.Tasks; -using Enums; using Services; using Structs; using UnityEngine; diff --git a/Assets/Scripts/Views/ScoreView.cs b/Assets/Scripts/Views/ScoreView.cs index 0c8fba2..e5907f3 100644 --- a/Assets/Scripts/Views/ScoreView.cs +++ b/Assets/Scripts/Views/ScoreView.cs @@ -1,12 +1,11 @@ -using System; using TMPro; using UnityEngine; namespace Views { public class ScoreView : MonoBehaviour { private TextMeshProUGUI scoreText; - private float displayScore = 0; - private int actualScore = 0; + private float displayScore; + private int actualScore; private void Awake() { this.scoreText = GetComponentInChildren();