Fix Object Instantiating
This commit is contained in:
39
Assets/Scripts/Scopes/LevelLifetimeScope.cs
Normal file
39
Assets/Scripts/Scopes/LevelLifetimeScope.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Models;
|
||||
using Models.Interfaces;
|
||||
using ScriptableObjects;
|
||||
using Services;
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using VContainer.Unity;
|
||||
using Views;
|
||||
|
||||
namespace Scopes
|
||||
{
|
||||
public class LevelLifetimeScope : LifetimeScope
|
||||
{
|
||||
[SerializeField] private GameVariables gameVariables;
|
||||
[SerializeField] private Transform gemsHolder;
|
||||
|
||||
protected override void Configure(IContainerBuilder builder)
|
||||
{
|
||||
builder.RegisterInstance(this.gameVariables);
|
||||
builder.RegisterInstance(this.gemsHolder);
|
||||
|
||||
builder.Register<IGameBoard>(c =>
|
||||
new GameBoard(this.gameVariables.width, this.gameVariables.height),
|
||||
Lifetime.Scoped);
|
||||
|
||||
builder.Register<IMatchService, MatchService>(Lifetime.Scoped);
|
||||
builder.Register<IScoreService, ScoreService>(Lifetime.Scoped);
|
||||
|
||||
builder.Register<IObjectPool<GemView>>(c =>
|
||||
new ObjectPoolService(this.gameVariables.gemsPrefabs, this.gemsHolder, this.gameVariables.width * this.gameVariables.height),
|
||||
Lifetime.Scoped);
|
||||
|
||||
builder.Register<IGameBoardService, GameBoardService>(Lifetime.Scoped);
|
||||
|
||||
builder.RegisterEntryPoint<LevelEntryPoint>();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Scopes/LevelLifetimeScope.cs.meta
Normal file
3
Assets/Scripts/Scopes/LevelLifetimeScope.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1e22ac45ff5446daf1aaa01329759a9
|
||||
timeCreated: 1765676452
|
||||
@@ -1,10 +1,14 @@
|
||||
using Services;
|
||||
using Services.Interfaces;
|
||||
using VContainer;
|
||||
using VContainer.Unity;
|
||||
|
||||
namespace Scopes {
|
||||
public class RootLifetimeScope : LifetimeScope {
|
||||
protected override void Configure(IContainerBuilder builder) {
|
||||
|
||||
protected override void Configure(IContainerBuilder builder)
|
||||
{
|
||||
builder.RegisterComponentInHierarchy<InputService>()
|
||||
.As<IInputService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Enums;
|
||||
using UnityEngine;
|
||||
using Views;
|
||||
|
||||
@@ -14,6 +16,8 @@ namespace ScriptableObjects {
|
||||
public int dropHeight = 1;
|
||||
public float gemSpeed = 7;
|
||||
public float scoreSpeed = 5;
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
[HideInInspector]
|
||||
public int rowsSize = 7;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
@@ -7,6 +6,7 @@ using Models.Interfaces;
|
||||
using ScriptableObjects;
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
using Views;
|
||||
using Object = UnityEngine.Object;
|
||||
using Random = UnityEngine.Random;
|
||||
@@ -17,26 +17,20 @@ namespace Services {
|
||||
private GameVariables gameVariables;
|
||||
private IMatchService matchService;
|
||||
private IScoreService scoreService;
|
||||
private IObjectPool<GemView> objectPool;
|
||||
private Transform gemsHolder;
|
||||
|
||||
public GameBoardService(IGameBoard gameBoard, GameVariables gameVariables, IMatchService matchService, IScoreService scoreSerivce, Transform gemsHolder) {
|
||||
public GameBoardService(IGameBoard gameBoard, GameVariables gameVariables, IMatchService matchService, IScoreService scoreSerivce, IObjectPool<GemView> objectPool, Transform gemsHolder) {
|
||||
this.gameBoard = gameBoard;
|
||||
this.gameVariables = gameVariables;
|
||||
this.matchService = matchService;
|
||||
this.scoreService = scoreSerivce;
|
||||
this.objectPool = objectPool;
|
||||
this.gemsHolder = gemsHolder;
|
||||
}
|
||||
|
||||
private int RandomGemTypeAsInt() {
|
||||
GemType[] spawnableGems = Enum.GetValues(typeof(GemType))
|
||||
.Cast<GemType>()
|
||||
.Where(gType => gType != GemType.Bomb)
|
||||
.ToArray();
|
||||
|
||||
return Random.Range(0, spawnableGems.Length);
|
||||
}
|
||||
|
||||
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++)
|
||||
{
|
||||
@@ -44,29 +38,26 @@ namespace Services {
|
||||
GameObject backgroundTile = Object.Instantiate(this.gameVariables.bgTilePrefabs, position, Quaternion.identity);
|
||||
backgroundTile.transform.SetParent(this.gemsHolder);
|
||||
backgroundTile.name = "BG Tile - " + x + ", " + y;
|
||||
|
||||
|
||||
|
||||
int gemToUse = RandomGemTypeAsInt();
|
||||
|
||||
int gemToUse = RandomUtils.RandomGemTypeAsInt();
|
||||
|
||||
int iterations = 0;
|
||||
while (this.matchService.MatchesAt(new Vector2Int(x, y), (GemType)gemToUse) && iterations < 100)
|
||||
{
|
||||
gemToUse = RandomGemTypeAsInt();
|
||||
gemToUse = RandomUtils.RandomGemTypeAsInt();
|
||||
iterations++;
|
||||
}
|
||||
|
||||
//ToDo: change gameVariables.gemsPrefabs[gemToUse] since gemToUse is index, if the order changes it wont work
|
||||
SpawnGem(new Vector2Int(x, y), this.gameVariables.gemsPrefabs[gemToUse], (GemType)gemToUse);
|
||||
}
|
||||
}
|
||||
|
||||
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 = Object.Instantiate(gemPrefab, new Vector3(position.x, position.y + this.gameVariables.dropHeight, 0f), Quaternion.identity);
|
||||
gemView.transform.SetParent(this.gemsHolder);
|
||||
|
||||
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));
|
||||
}
|
||||
@@ -140,7 +131,7 @@ namespace Services {
|
||||
{
|
||||
Gem currentGem = this.gameBoard.GetGemAt(new Vector2Int(x,y));
|
||||
if (currentGem == null) {
|
||||
int gemToUse = RandomGemTypeAsInt();
|
||||
int gemToUse = RandomUtils.RandomGemTypeAsInt();
|
||||
SpawnGem(new Vector2Int(x, y), this.gameVariables.gemsPrefabs[gemToUse], (GemType)gemToUse);
|
||||
}
|
||||
}
|
||||
@@ -174,7 +165,8 @@ namespace Services {
|
||||
if (currentGem != null)
|
||||
{
|
||||
GemView gemView = gemsViews.FirstOrDefault(gv => gv.Gem == currentGem);
|
||||
Object.Instantiate(this.gameVariables.destroyEffectPrefabs[(int)currentGem.Type], new Vector2(position.x, position.y), Quaternion.identity);
|
||||
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);
|
||||
SetGem(position, null);
|
||||
|
||||
35
Assets/Scripts/Services/InputService.cs
Normal file
35
Assets/Scripts/Services/InputService.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Services {
|
||||
public class InputService : MonoBehaviour, IInputService
|
||||
{
|
||||
public event Action<Vector2> OnPointerDown;
|
||||
public event Action<Vector2> OnPointerUp;
|
||||
|
||||
private bool wasDown;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Mouse
|
||||
var isDown = Input.GetMouseButton(0);
|
||||
if (!wasDown && isDown)
|
||||
OnPointerDown?.Invoke(Input.mousePosition);
|
||||
|
||||
if (wasDown && !isDown)
|
||||
OnPointerUp?.Invoke(Input.mousePosition);
|
||||
|
||||
wasDown = isDown;
|
||||
|
||||
// Optional: Touch (if you want both, you can merge logic more carefully)
|
||||
if (Input.touchCount <= 0) return;
|
||||
|
||||
var t = Input.GetTouch(0);
|
||||
if (t.phase == TouchPhase.Began)
|
||||
OnPointerDown?.Invoke(t.position);
|
||||
else if (t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled)
|
||||
OnPointerUp?.Invoke(t.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Services/InputService.cs.meta
Normal file
3
Assets/Scripts/Services/InputService.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51137073af0548959f4e16ceffc344b5
|
||||
timeCreated: 1765676415
|
||||
@@ -1,8 +1,10 @@
|
||||
using Enums;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Services.Interfaces {
|
||||
public interface IObjectPool<T> where T : class {
|
||||
T Get();
|
||||
void Fill();
|
||||
void Release(T gameObject);
|
||||
T Get(GemType type, Vector2Int position, float offsetY);
|
||||
void Release(T gemView);
|
||||
void Clear();
|
||||
}
|
||||
}
|
||||
26
Assets/Scripts/Services/LevelEntryPoint.cs
Normal file
26
Assets/Scripts/Services/LevelEntryPoint.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
using VContainer.Unity;
|
||||
using Views;
|
||||
|
||||
namespace Services
|
||||
{
|
||||
public class LevelEntryPoint : IStartable
|
||||
{
|
||||
private readonly IObjectPool<GemView> gemViewPool;
|
||||
private readonly IGameBoardService gameBoardService;
|
||||
|
||||
public LevelEntryPoint(IObjectPool<GemView> gemViewPool, IGameBoardService gameBoardService)
|
||||
{
|
||||
this.gemViewPool = gemViewPool;
|
||||
this.gameBoardService = gameBoardService;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Debug.Log("Level Entry Point");
|
||||
this.gameBoardService.Setup();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Services/LevelEntryPoint.cs.meta
Normal file
3
Assets/Scripts/Services/LevelEntryPoint.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e888313cbfdb43e2b11c0e7d1d030fdb
|
||||
timeCreated: 1765676578
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Enums;
|
||||
using Models.Interfaces;
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -9,9 +10,9 @@ namespace Services {
|
||||
private List<Gem> currentMatches = new List<Gem>();
|
||||
public List<Gem> CurrentMatches => this.currentMatches;
|
||||
|
||||
private GameBoard gameBoard;
|
||||
private IGameBoard gameBoard;
|
||||
|
||||
public MatchService(GameBoard gameBoard) {
|
||||
public MatchService(IGameBoard gameBoard) {
|
||||
this.gameBoard = gameBoard;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +1,49 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Enums;
|
||||
using Services.Interfaces;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
using Views;
|
||||
using Object = UnityEngine.Object;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Services {
|
||||
public class ObjectPoolService:IObjectPool<GemView> {
|
||||
private readonly GemView prefab;
|
||||
private readonly GemView[] prefabs;
|
||||
private readonly Transform parent;
|
||||
private readonly int size;
|
||||
|
||||
private readonly Stack<GemView> pool = new Stack<GemView>();
|
||||
|
||||
public ObjectPoolService(GemView prefab, Transform parent, int size = 5) {
|
||||
this.prefab = prefab;
|
||||
public ObjectPoolService(GemView[] prefabs, Transform parent, int size = 5) {
|
||||
this.prefabs = prefabs;
|
||||
this.parent = parent;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public GemView Get() {
|
||||
return this.pool.Count == 0 ? Object.Instantiate(this.prefab, this.parent) : this.pool.Pop();
|
||||
}
|
||||
|
||||
public void Fill() {
|
||||
for (int i = 0; i < this.size; i++) {
|
||||
Object.Instantiate(this.prefab, this.parent);
|
||||
public GemView Get(GemType type, Vector2Int position, float dropHeight) {
|
||||
int typeAsInt = (int) type;
|
||||
|
||||
GemView gemView;
|
||||
if (this.pool.Count > 0) {
|
||||
gemView = this.pool.Pop();
|
||||
gemView.transform.localPosition = new Vector2(position.x, position.y + dropHeight);
|
||||
return gemView;
|
||||
}
|
||||
|
||||
gemView = Object.Instantiate(this.prefabs[typeAsInt], new Vector2(position.x, position.y + dropHeight), Quaternion.identity, this.parent);
|
||||
return gemView;
|
||||
}
|
||||
|
||||
public void Release(GemView gameObject) {
|
||||
this.pool.Push(gameObject);
|
||||
|
||||
public void Release(GemView gemView) {
|
||||
if (gemView == null)
|
||||
return;
|
||||
|
||||
gemView.gameObject.SetActive(false);
|
||||
this.pool.Push(gemView);
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
|
||||
3
Assets/Scripts/Utils.meta
Normal file
3
Assets/Scripts/Utils.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb63cba0ae32411c8c20c6228c68c922
|
||||
timeCreated: 1765680476
|
||||
17
Assets/Scripts/Utils/RandomUtils.cs
Normal file
17
Assets/Scripts/Utils/RandomUtils.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Enums;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Utils {
|
||||
public static class RandomUtils {
|
||||
public static int RandomGemTypeAsInt() {
|
||||
GemType[] spawnableGems = Enum.GetValues(typeof(GemType))
|
||||
.Cast<GemType>()
|
||||
.Where(gType => gType != GemType.Bomb)
|
||||
.ToArray();
|
||||
|
||||
return Random.Range(0, spawnableGems.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Utils/RandomUtils.cs.meta
Normal file
3
Assets/Scripts/Utils/RandomUtils.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba833efb34fd4c16a0a2ef2ad6979951
|
||||
timeCreated: 1765680493
|
||||
Reference in New Issue
Block a user