Implement Bombs
This commit is contained in:
@@ -1,50 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using Enums;
|
||||
using Services.Interfaces;
|
||||
using Structs;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
using Views;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Services {
|
||||
public class ObjectPoolService:IObjectPool<GemView> {
|
||||
private readonly GemView[] prefabs;
|
||||
private readonly GemTypeValues[] gemValues;
|
||||
private readonly Transform parent;
|
||||
|
||||
private readonly Stack<GemView> pool = new Stack<GemView>();
|
||||
private readonly Dictionary<GemType, Stack<GemView>> gemTypeToPools = new Dictionary<GemType, Stack<GemView>>();
|
||||
|
||||
public ObjectPoolService(GemView[] prefabs, Transform parent) {
|
||||
this.prefabs = prefabs;
|
||||
public ObjectPoolService(GemTypeValues[] gemValues, Transform parent) {
|
||||
this.gemValues = gemValues;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public GemView Get(GemType type, Vector2Int position, float dropHeight) {
|
||||
int typeAsInt = (int) type;
|
||||
if (!this.gemTypeToPools.ContainsKey(type)) {
|
||||
this.gemTypeToPools.Add(type, new Stack<GemView>());
|
||||
}
|
||||
|
||||
GemView gemView;
|
||||
float randomOffset = Random.Range(1f, 2.5f);
|
||||
Vector2 vector2Position = new Vector2(position.x, position.y + dropHeight * randomOffset);
|
||||
if (this.pool.Count > 0) {
|
||||
gemView = this.pool.Pop();
|
||||
if (this.gemTypeToPools[type].Count > 0) {
|
||||
gemView = this.gemTypeToPools[type].Pop();
|
||||
|
||||
|
||||
gemView.transform.localPosition = vector2Position;
|
||||
return gemView;
|
||||
}
|
||||
|
||||
gemView = Object.Instantiate(this.prefabs[typeAsInt], vector2Position, Quaternion.identity, this.parent);
|
||||
gemView = Object.Instantiate(GemUtils.GetGemValues(type, this.gemValues).gemPrefab, vector2Position, Quaternion.identity, this.parent);
|
||||
return gemView;
|
||||
}
|
||||
|
||||
public void Release(GemView gemView) {
|
||||
if (gemView == null)
|
||||
if (gemView is null)
|
||||
return;
|
||||
|
||||
Object.Instantiate(GemUtils.GetGemValues(gemView.Gem.Type, this.gemValues).explosionPrefab, gemView.transform.position, Quaternion.identity, this.parent);
|
||||
|
||||
if (!this.gemTypeToPools.ContainsKey(gemView.Gem.Type)) {
|
||||
this.gemTypeToPools.Add(gemView.Gem.Type, new Stack<GemView>());
|
||||
}
|
||||
|
||||
gemView.gameObject.SetActive(false);
|
||||
this.pool.Push(gemView);
|
||||
this.gemTypeToPools[gemView.Gem.Type].Push(gemView);
|
||||
gemView.Unbind();
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
this.pool.Clear();
|
||||
foreach (Stack<GemView> pool in this.gemTypeToPools.Values) {
|
||||
pool.Clear();
|
||||
}
|
||||
|
||||
this.gemTypeToPools.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user