Properly Call FillBoard
This commit is contained in:
@@ -1,225 +1,225 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using Enums;
|
||||
|
||||
public class SC_GameLogic : MonoBehaviour
|
||||
{
|
||||
private Dictionary<string, GameObject> unityObjects; //this is a dictionary of all game objects in the scene
|
||||
private int score = 0; //current score
|
||||
private float displayScore = 0; //for animation, i think
|
||||
private GameBoard gameBoard; //game board object
|
||||
private GameState currentState = GameState.Move; //current game state
|
||||
public GameState CurrentState { get { return this.currentState; } }
|
||||
|
||||
#region MonoBehaviour
|
||||
private void Awake()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
StartGame();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
this.displayScore = Mathf.Lerp(this.displayScore, this.gameBoard.Score, SC_GameVariables.Instance.scoreSpeed * Time.deltaTime);
|
||||
this.unityObjects["Txt_Score"].GetComponent<TMPro.TextMeshProUGUI>().text = this.displayScore.ToString("0");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Logic
|
||||
private void Init()
|
||||
{
|
||||
this.unityObjects = new Dictionary<string, GameObject>();
|
||||
GameObject[] _obj = GameObject.FindGameObjectsWithTag("UnityObject");
|
||||
foreach (GameObject g in _obj) this.unityObjects.Add(g.name,g);
|
||||
|
||||
this.gameBoard = new GameBoard(7, 7);
|
||||
Setup();
|
||||
}
|
||||
|
||||
//GameBoardService
|
||||
private void Setup()
|
||||
{
|
||||
for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
{
|
||||
Vector2 _pos = new Vector2(x, y);
|
||||
GameObject _bgTile = Instantiate(SC_GameVariables.Instance.bgTilePrefabs, _pos, Quaternion.identity);
|
||||
_bgTile.transform.SetParent(this.unityObjects["GemsHolder"].transform);
|
||||
_bgTile.name = "BG Tile - " + x + ", " + y;
|
||||
|
||||
int _gemToUse = Random.Range(0, SC_GameVariables.Instance.gems.Length);
|
||||
|
||||
int iterations = 0;
|
||||
while (this.gameBoard.MatchesAt(new Vector2Int(x, y), SC_GameVariables.Instance.gems[_gemToUse]) && iterations < 100)
|
||||
{
|
||||
_gemToUse = Random.Range(0, SC_GameVariables.Instance.gems.Length);
|
||||
iterations++;
|
||||
}
|
||||
SpawnGem(new Vector2Int(x, y), SC_GameVariables.Instance.gems[_gemToUse]);
|
||||
}
|
||||
}
|
||||
public void StartGame()
|
||||
{
|
||||
this.unityObjects["Txt_Score"].GetComponent<TextMeshProUGUI>().text = this.score.ToString("0");
|
||||
}
|
||||
|
||||
//GameBoardService
|
||||
private void SpawnGem(Vector2Int _Position, SC_Gem _GemToSpawn)
|
||||
{
|
||||
if (Random.Range(0, 100f) < SC_GameVariables.Instance.bombChance)
|
||||
_GemToSpawn = SC_GameVariables.Instance.bomb;
|
||||
|
||||
SC_Gem _gem = Instantiate(_GemToSpawn, new Vector3(_Position.x, _Position.y + SC_GameVariables.Instance.dropHeight, 0f), Quaternion.identity);
|
||||
_gem.transform.SetParent(this.unityObjects["GemsHolder"].transform);
|
||||
_gem.name = "Gem - " + _Position.x + ", " + _Position.y;
|
||||
this.gameBoard.SetGem(_Position.x,_Position.y, _gem);
|
||||
_gem.SetupGem(this,_Position);
|
||||
}
|
||||
|
||||
//GameBoardService
|
||||
public void SetGem(int _X,int _Y, SC_Gem _Gem)
|
||||
{
|
||||
this.gameBoard.SetGem(_X,_Y, _Gem);
|
||||
}
|
||||
|
||||
//GameBoardService
|
||||
public SC_Gem GetGem(int _X, int _Y)
|
||||
{
|
||||
return this.gameBoard.GetGem(_X, _Y);
|
||||
}
|
||||
public void SetState(GameState _CurrentState)
|
||||
{
|
||||
this.currentState = _CurrentState;
|
||||
}
|
||||
|
||||
//GameBoardService
|
||||
public void DestroyMatches()
|
||||
{
|
||||
for (int i = 0; i < this.gameBoard.CurrentMatches.Count; i++)
|
||||
if (this.gameBoard.CurrentMatches[i] != null)
|
||||
{
|
||||
ScoreCheck(this.gameBoard.CurrentMatches[i]);
|
||||
DestroyMatchedGemsAt(this.gameBoard.CurrentMatches[i].posIndex);
|
||||
}
|
||||
|
||||
StartCoroutine(DecreaseRowCo());
|
||||
}
|
||||
|
||||
//I think, this moves gems down after a match?
|
||||
//MoveGemsDown
|
||||
//GameBoardService
|
||||
private IEnumerator DecreaseRowCo()
|
||||
{
|
||||
yield return new WaitForSeconds(.2f);
|
||||
|
||||
int nullCounter = 0;
|
||||
for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
{
|
||||
for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
{
|
||||
SC_Gem _curGem = this.gameBoard.GetGem(x, y);
|
||||
if (_curGem == null)
|
||||
{
|
||||
nullCounter++;
|
||||
}
|
||||
else if (nullCounter > 0)
|
||||
{
|
||||
_curGem.posIndex.y -= nullCounter;
|
||||
SetGem(x, y - nullCounter, _curGem);
|
||||
SetGem(x, y, null);
|
||||
}
|
||||
}
|
||||
nullCounter = 0;
|
||||
}
|
||||
|
||||
StartCoroutine(FilledBoardCo());
|
||||
}
|
||||
|
||||
//IScoreService
|
||||
public void ScoreCheck(SC_Gem gemToCheck)
|
||||
{
|
||||
this.gameBoard.Score += gemToCheck.scoreValue;
|
||||
}
|
||||
|
||||
//GameBoardService - DestroyMatchedGems
|
||||
private void DestroyMatchedGemsAt(Vector2Int _Pos)
|
||||
{
|
||||
SC_Gem _curGem = this.gameBoard.GetGem(_Pos.x,_Pos.y);
|
||||
if (_curGem != null)
|
||||
{
|
||||
Instantiate(_curGem.destroyEffect, new Vector2(_Pos.x, _Pos.y), Quaternion.identity);
|
||||
|
||||
Destroy(_curGem.gameObject);
|
||||
SetGem(_Pos.x,_Pos.y, null);
|
||||
}
|
||||
}
|
||||
|
||||
//GameBoardService - FillBoard
|
||||
|
||||
private IEnumerator FilledBoardCo()
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
RefillBoard();
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
this.gameBoard.FindAllMatches();
|
||||
if (this.gameBoard.CurrentMatches.Count > 0)
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
DestroyMatches();
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
this.currentState = GameState.Move;
|
||||
}
|
||||
}
|
||||
|
||||
//GameBoardService
|
||||
private void RefillBoard()
|
||||
{
|
||||
for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
{
|
||||
for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
{
|
||||
SC_Gem _curGem = this.gameBoard.GetGem(x,y);
|
||||
if (_curGem == null)
|
||||
{
|
||||
int gemToUse = Random.Range(0, SC_GameVariables.Instance.gems.Length);
|
||||
SpawnGem(new Vector2Int(x, y), SC_GameVariables.Instance.gems[gemToUse]);
|
||||
}
|
||||
}
|
||||
}
|
||||
CheckMisplacedGems();
|
||||
}
|
||||
//gets all gem game objects
|
||||
//if the gem is not in GameBoard, destroy it
|
||||
private void CheckMisplacedGems()
|
||||
{
|
||||
List<SC_Gem> foundGems = new List<SC_Gem>();
|
||||
foundGems.AddRange(FindObjectsOfType<SC_Gem>());
|
||||
for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
{
|
||||
for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
{
|
||||
SC_Gem _curGem = this.gameBoard.GetGem(x, y);
|
||||
if (foundGems.Contains(_curGem))
|
||||
foundGems.Remove(_curGem);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (SC_Gem g in foundGems)
|
||||
Destroy(g.gameObject);
|
||||
}
|
||||
public void FindAllMatches()
|
||||
{
|
||||
this.gameBoard.FindAllMatches();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
// using System.Collections;
|
||||
// using System.Collections.Generic;
|
||||
// using TMPro;
|
||||
// using UnityEngine;
|
||||
// using Enums;
|
||||
//
|
||||
// public class SC_GameLogic : MonoBehaviour
|
||||
// {
|
||||
// private Dictionary<string, GameObject> unityObjects; //this is a dictionary of all game objects in the scene
|
||||
// private int score = 0; //current score
|
||||
// private float displayScore = 0; //for animation, i think
|
||||
// private GameBoard gameBoard; //game board object
|
||||
// private GameState currentState = GameState.Move; //current game state
|
||||
// public GameState CurrentState { get { return this.currentState; } }
|
||||
//
|
||||
// #region MonoBehaviour
|
||||
// private void Awake()
|
||||
// {
|
||||
// Init();
|
||||
// }
|
||||
//
|
||||
// private void Start()
|
||||
// {
|
||||
// StartGame();
|
||||
// }
|
||||
//
|
||||
// private void Update()
|
||||
// {
|
||||
// this.displayScore = Mathf.Lerp(this.displayScore, this.gameBoard.Score, SC_GameVariables.Instance.scoreSpeed * Time.deltaTime);
|
||||
// this.unityObjects["Txt_Score"].GetComponent<TMPro.TextMeshProUGUI>().text = this.displayScore.ToString("0");
|
||||
// }
|
||||
// #endregion
|
||||
//
|
||||
// #region Logic
|
||||
// private void Init()
|
||||
// {
|
||||
// this.unityObjects = new Dictionary<string, GameObject>();
|
||||
// GameObject[] _obj = GameObject.FindGameObjectsWithTag("UnityObject");
|
||||
// foreach (GameObject g in _obj) this.unityObjects.Add(g.name,g);
|
||||
//
|
||||
// this.gameBoard = new GameBoard(7, 7);
|
||||
// Setup();
|
||||
// }
|
||||
//
|
||||
// //GameBoardService
|
||||
// private void Setup()
|
||||
// {
|
||||
// for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
// for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
// {
|
||||
// Vector2 _pos = new Vector2(x, y);
|
||||
// GameObject _bgTile = Instantiate(SC_GameVariables.Instance.bgTilePrefabs, _pos, Quaternion.identity);
|
||||
// _bgTile.transform.SetParent(this.unityObjects["GemsHolder"].transform);
|
||||
// _bgTile.name = "BG Tile - " + x + ", " + y;
|
||||
//
|
||||
// int _gemToUse = Random.Range(0, SC_GameVariables.Instance.gems.Length);
|
||||
//
|
||||
// int iterations = 0;
|
||||
// while (this.gameBoard.MatchesAt(new Vector2Int(x, y), SC_GameVariables.Instance.gems[_gemToUse]) && iterations < 100)
|
||||
// {
|
||||
// _gemToUse = Random.Range(0, SC_GameVariables.Instance.gems.Length);
|
||||
// iterations++;
|
||||
// }
|
||||
// SpawnGem(new Vector2Int(x, y), SC_GameVariables.Instance.gems[_gemToUse]);
|
||||
// }
|
||||
// }
|
||||
// public void StartGame()
|
||||
// {
|
||||
// this.unityObjects["Txt_Score"].GetComponent<TextMeshProUGUI>().text = this.score.ToString("0");
|
||||
// }
|
||||
//
|
||||
// //GameBoardService
|
||||
// private void SpawnGem(Vector2Int _Position, SC_Gem _GemToSpawn)
|
||||
// {
|
||||
// if (Random.Range(0, 100f) < SC_GameVariables.Instance.bombChance)
|
||||
// _GemToSpawn = SC_GameVariables.Instance.bomb;
|
||||
//
|
||||
// SC_Gem _gem = Instantiate(_GemToSpawn, new Vector3(_Position.x, _Position.y + SC_GameVariables.Instance.dropHeight, 0f), Quaternion.identity);
|
||||
// _gem.transform.SetParent(this.unityObjects["GemsHolder"].transform);
|
||||
// _gem.name = "Gem - " + _Position.x + ", " + _Position.y;
|
||||
// this.gameBoard.SetGem(_Position.x,_Position.y, _gem);
|
||||
// _gem.SetupGem(this,_Position);
|
||||
// }
|
||||
//
|
||||
// //GameBoardService
|
||||
// public void SetGem(int _X,int _Y, SC_Gem _Gem)
|
||||
// {
|
||||
// this.gameBoard.SetGem(_X,_Y, _Gem);
|
||||
// }
|
||||
//
|
||||
// //GameBoardService
|
||||
// public SC_Gem GetGem(int _X, int _Y)
|
||||
// {
|
||||
// return this.gameBoard.GetGem(_X, _Y);
|
||||
// }
|
||||
// public void SetState(GameState _CurrentState)
|
||||
// {
|
||||
// this.currentState = _CurrentState;
|
||||
// }
|
||||
//
|
||||
// //GameBoardService
|
||||
// public void DestroyMatches()
|
||||
// {
|
||||
// for (int i = 0; i < this.gameBoard.CurrentMatches.Count; i++)
|
||||
// if (this.gameBoard.CurrentMatches[i] != null)
|
||||
// {
|
||||
// ScoreCheck(this.gameBoard.CurrentMatches[i]);
|
||||
// DestroyMatchedGemsAt(this.gameBoard.CurrentMatches[i].posIndex);
|
||||
// }
|
||||
//
|
||||
// StartCoroutine(DecreaseRowCo());
|
||||
// }
|
||||
//
|
||||
// //I think, this moves gems down after a match?
|
||||
// //MoveGemsDown
|
||||
// //GameBoardService
|
||||
// private IEnumerator DecreaseRowCo()
|
||||
// {
|
||||
// yield return new WaitForSeconds(.2f);
|
||||
//
|
||||
// int nullCounter = 0;
|
||||
// for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
// {
|
||||
// for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
// {
|
||||
// SC_Gem _curGem = this.gameBoard.GetGem(x, y);
|
||||
// if (_curGem == null)
|
||||
// {
|
||||
// nullCounter++;
|
||||
// }
|
||||
// else if (nullCounter > 0)
|
||||
// {
|
||||
// _curGem.posIndex.y -= nullCounter;
|
||||
// SetGem(x, y - nullCounter, _curGem);
|
||||
// SetGem(x, y, null);
|
||||
// }
|
||||
// }
|
||||
// nullCounter = 0;
|
||||
// }
|
||||
//
|
||||
// StartCoroutine(FilledBoardCo());
|
||||
// }
|
||||
//
|
||||
// //IScoreService
|
||||
// public void ScoreCheck(SC_Gem gemToCheck)
|
||||
// {
|
||||
// this.gameBoard.Score += gemToCheck.scoreValue;
|
||||
// }
|
||||
//
|
||||
// //GameBoardService - DestroyMatchedGems
|
||||
// private void DestroyMatchedGemsAt(Vector2Int _Pos)
|
||||
// {
|
||||
// SC_Gem _curGem = this.gameBoard.GetGem(_Pos.x,_Pos.y);
|
||||
// if (_curGem != null)
|
||||
// {
|
||||
// Instantiate(_curGem.destroyEffect, new Vector2(_Pos.x, _Pos.y), Quaternion.identity);
|
||||
//
|
||||
// Destroy(_curGem.gameObject);
|
||||
// SetGem(_Pos.x,_Pos.y, null);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// //GameBoardService - FillBoard
|
||||
//
|
||||
// private IEnumerator FilledBoardCo()
|
||||
// {
|
||||
// yield return new WaitForSeconds(0.5f);
|
||||
// RefillBoard();
|
||||
// yield return new WaitForSeconds(0.5f);
|
||||
// this.gameBoard.FindAllMatches();
|
||||
// if (this.gameBoard.CurrentMatches.Count > 0)
|
||||
// {
|
||||
// yield return new WaitForSeconds(0.5f);
|
||||
// DestroyMatches();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// yield return new WaitForSeconds(0.5f);
|
||||
// this.currentState = GameState.Move;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// //GameBoardService
|
||||
// private void RefillBoard()
|
||||
// {
|
||||
// for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
// {
|
||||
// for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
// {
|
||||
// SC_Gem _curGem = this.gameBoard.GetGem(x,y);
|
||||
// if (_curGem == null)
|
||||
// {
|
||||
// int gemToUse = Random.Range(0, SC_GameVariables.Instance.gems.Length);
|
||||
// SpawnGem(new Vector2Int(x, y), SC_GameVariables.Instance.gems[gemToUse]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// CheckMisplacedGems();
|
||||
// }
|
||||
// //gets all gem game objects
|
||||
// //if the gem is not in GameBoard, destroy it
|
||||
// private void CheckMisplacedGems()
|
||||
// {
|
||||
// List<SC_Gem> foundGems = new List<SC_Gem>();
|
||||
// foundGems.AddRange(FindObjectsOfType<SC_Gem>());
|
||||
// for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
// {
|
||||
// for (int y = 0; y < this.gameBoard.Height; y++)
|
||||
// {
|
||||
// SC_Gem _curGem = this.gameBoard.GetGem(x, y);
|
||||
// if (foundGems.Contains(_curGem))
|
||||
// foundGems.Remove(_curGem);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// foreach (SC_Gem g in foundGems)
|
||||
// Destroy(g.gameObject);
|
||||
// }
|
||||
// public void FindAllMatches()
|
||||
// {
|
||||
// this.gameBoard.FindAllMatches();
|
||||
// }
|
||||
//
|
||||
// #endregion
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user