Fixed Object Pooling and cascading

This commit is contained in:
2025-12-14 19:19:40 +08:00
parent 68046b8960
commit 95b43ed772
13 changed files with 154 additions and 40 deletions

View File

@@ -3,22 +3,26 @@ using System.Linq;
using Cysharp.Threading.Tasks;
using Enums;
using Models.Interfaces;
using Presenter;
using ScriptableObjects;
using Services.Interfaces;
using UnityEngine;
using Utils;
using VContainer.Unity;
using Views;
using Object = UnityEngine.Object;
using Random = UnityEngine.Random;
namespace Services {
public class GameBoardService : IGameBoardService {
private IGameBoard gameBoard;
private GameVariables gameVariables;
private IMatchService matchService;
private IScoreService scoreService;
private IObjectPool<GemView> objectPool;
private Transform gemsHolder;
public class GameBoardService : IGameBoardService, ITickable {
private readonly IGameBoard gameBoard;
private readonly GameVariables gameVariables;
private readonly IMatchService matchService;
private readonly IScoreService scoreService;
private readonly IObjectPool<GemView> objectPool;
private readonly Transform gemsHolder;
private readonly List<GemPresenter> gemPresenters = new List<GemPresenter>();
public GameBoardService(IGameBoard gameBoard, GameVariables gameVariables, IMatchService matchService, IScoreService scoreSerivce, IObjectPool<GemView> objectPool, Transform gemsHolder) {
this.gameBoard = gameBoard;
@@ -29,8 +33,17 @@ namespace Services {
this.gemsHolder = gemsHolder;
}
public void Tick() {
int i = 0;
foreach (GemPresenter gemPresenter in gemPresenters) {
gemPresenter.Tick();
i++;
}
}
//Instantiates background tiles and calls SpawnGems
//Uses MatchService.MatchesAt to avoid matching Gems
public void Setup() {
Debug.Log("Setting up the board");
for (int x = 0; x < this.gameBoard.Width; x++)
for (int y = 0; y < this.gameBoard.Height; y++)
{
@@ -52,24 +65,31 @@ namespace Services {
}
}
//Uses the ObjectPool to spawn a gem at the given position
public void SpawnGem(Vector2Int position, GemView gemPrefab, GemType gemType) {
Debug.Log("Spawning gem at " + position + " with type " + gemType + "");
if (Random.Range(0, 100f) < this.gameVariables.bombChance)
gemPrefab = this.gameVariables.bombPrefab;
GemView gemView = this.objectPool.Get(gemType, position, this.gameVariables.dropHeight);
gemView.name = "Gem - " + position.x + ", " + position.y;
SetGem(new Vector2Int(position.x,position.y), new Gem(gemType, position));
Gem gem = new Gem(gemType, position);
gemView.Bind(gem);
this.gemPresenters.Add(new GemPresenter(gem, gemView));
SetGem(new Vector2Int(position.x,position.y), gem);
}
//Sets the gem on the GameBoard
public void SetGem(Vector2Int position, Gem gem) {
this.gameBoard.SetGemAt(new Vector2Int(position.x, position.y), gem);
}
//Gets the gem from the GameBoard
public Gem GetGem(Vector2Int position) {
return this.gameBoard.GetGemAt(position);
}
//If there are matches, destroys them and moves the gems down
public void DestroyMatches() {
for (int i = 0; i < this.matchService.CurrentMatches.Count; i++)
if (this.matchService.CurrentMatches[i] != null)
@@ -83,8 +103,6 @@ namespace Services {
public async UniTask MoveGemsDown() {
await UniTask.Delay(2);
// why the delay though?
// yield return new WaitForSeconds(.2f);
int nullCounter = 0;
for (int x = 0; x < this.gameBoard.Width; x++)
@@ -141,6 +159,7 @@ namespace Services {
CheckMisplacedGems();
}
//Checks if there are gems that are not in the board
public void CheckMisplacedGems() {
List<GemView> gemsViews = GemsViews();
@@ -157,8 +176,12 @@ namespace Services {
}
}
foreach (GemView g in gemsViews)
Object.Destroy(g.gameObject);
foreach (GemView g in gemsViews) {
RemovePresenterFor(g);
g.Unbind();
this.objectPool.Release(g);
}
}
public void DestroyMatchedGems(Vector2Int position) {
@@ -167,14 +190,32 @@ namespace Services {
if (currentGem != null)
{
GemView gemView = gemsViews.FirstOrDefault(gv => gv.Gem == currentGem);
if (gemView is null) {
return;
}
//ToDo: Destroy effect
if(this.gameVariables.destroyEffectPrefabs.Length > 0)
Object.Instantiate(this.gameVariables.destroyEffectPrefabs[(int)currentGem.Type], new Vector2(position.x, position.y), Quaternion.identity);
Object.Destroy(gemView!.gameObject);
RemovePresenterFor(gemView);
gemView.Unbind();
this.objectPool.Release(gemView);
SetGem(position, null);
}
}
private void RemovePresenterFor(GemView gemView) {
if (gemView is null) {
return;
}
List<GemPresenter> presentersToRemove = this.gemPresenters.Where(p => p.GemView == gemView).ToList();
foreach (GemPresenter presenter in presentersToRemove) {
this.gemPresenters.Remove(presenter);
}
}
private List<GemView> GemsViews() {
return this.gemsHolder.GetComponentsInChildren<GemView>().ToList();
}