Fix Object Instantiating
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user