Properly Call FillBoard

This commit is contained in:
2025-12-14 11:01:04 +08:00
parent 6abccbe6d8
commit 68046b8960
5 changed files with 593 additions and 591 deletions

View File

@@ -1,188 +1,188 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Enums;
//Done, moved to MatchService and GameBoard
public class GameBoard
{
// - sets size of the grid
// - handles the score
// - holds the current matches
#region Variables
private int height = 0;
public int Height { get { return this.height; } }
private int width = 0;
public int Width { get { return this.width; } }
private SC_Gem[,] allGems;
// public Gem[,] AllGems { get { return allGems; } }
private int score = 0;
public int Score
{
get { return this.score; }
set {
this.score = value; }
}
private List<SC_Gem> currentMatches = new List<SC_Gem>();
public List<SC_Gem> CurrentMatches { get { return this.currentMatches; } }
#endregion
public GameBoard(int _Width, int _Height)
{
this.height = _Height;
this.width = _Width;
this.allGems = new SC_Gem[this.width, this.height];
}
//checks if there are 3 gems of the same type next to each other
//used during setup to avoid matches on game start
//MatchService
public bool MatchesAt(Vector2Int _PositionToCheck, SC_Gem _GemToCheck)
{
if (_PositionToCheck.x > 1)
{
if (this.allGems[_PositionToCheck.x - 1, _PositionToCheck.y].type == _GemToCheck.type && this.allGems[_PositionToCheck.x - 2, _PositionToCheck.y].type == _GemToCheck.type)
return true;
}
if (_PositionToCheck.y > 1)
{
if (this.allGems[_PositionToCheck.x, _PositionToCheck.y - 1].type == _GemToCheck.type && this.allGems[_PositionToCheck.x, _PositionToCheck.y - 2].type == _GemToCheck.type)
return true;
}
return false;
}
//places the gem in the 2d array
public void SetGem(int _X, int _Y, SC_Gem _Gem)
{
this.allGems[_X, _Y] = _Gem;
}
public SC_Gem GetGem(int _X,int _Y)
{
return this.allGems[_X, _Y];
}
//MatchService
public void FindAllMatches()
{
this.currentMatches.Clear();
for (int x = 0; x < this.width; x++)
for (int y = 0; y < this.height; y++)
{
SC_Gem currentGem = this.allGems[x, y];
if (currentGem != null)
{
if (x > 0 && x < this.width - 1)
{
SC_Gem leftGem = this.allGems[x - 1, y];
SC_Gem rightGem = this.allGems[x + 1, y];
//checking no empty spots
if (leftGem != null && rightGem != null)
{
//Match
if (leftGem.type == currentGem.type && rightGem.type == currentGem.type)
{
currentGem.isMatch = true;
leftGem.isMatch = true;
rightGem.isMatch = true;
this.currentMatches.Add(currentGem);
this.currentMatches.Add(leftGem);
this.currentMatches.Add(rightGem);
}
}
}
if (y > 0 && y < this.height - 1)
{
SC_Gem aboveGem = this.allGems[x, y - 1];
SC_Gem bellowGem = this.allGems[x, y + 1];
//checking no empty spots
if (aboveGem != null && bellowGem != null)
{
//Match
if (aboveGem.type == currentGem.type && bellowGem.type == currentGem.type)
{
currentGem.isMatch = true;
aboveGem.isMatch = true;
bellowGem.isMatch = true;
this.currentMatches.Add(currentGem);
this.currentMatches.Add(aboveGem);
this.currentMatches.Add(bellowGem);
}
}
}
}
}
if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();
CheckForBombs();
}
public void CheckForBombs()
{
for (int i = 0; i < this.currentMatches.Count; i++)
{
SC_Gem gem = this.currentMatches[i];
int x = gem.posIndex.x;
int y = gem.posIndex.y;
if (gem.posIndex.x > 0)
{
if (this.allGems[x - 1, y] != null && this.allGems[x - 1, y].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x - 1, y), this.allGems[x - 1, y].blastSize);
}
if (gem.posIndex.x + 1 < this.width)
{
if (this.allGems[x + 1, y] != null && this.allGems[x + 1, y].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x + 1, y), this.allGems[x + 1, y].blastSize);
}
if (gem.posIndex.y > 0)
{
if (this.allGems[x, y - 1] != null && this.allGems[x, y - 1].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x, y - 1), this.allGems[x, y - 1].blastSize);
}
if (gem.posIndex.y + 1 < this.height)
{
if (this.allGems[x, y + 1] != null && this.allGems[x, y + 1].type == GemType.Bomb)
MarkBombArea(new Vector2Int(x, y + 1), this.allGems[x, y + 1].blastSize);
}
}
}
public void MarkBombArea(Vector2Int bombPos, int _BlastSize)
{
string _print = "";
for (int x = bombPos.x - _BlastSize; x <= bombPos.x + _BlastSize; x++)
{
for (int y = bombPos.y - _BlastSize; y <= bombPos.y + _BlastSize; y++)
{
if (x >= 0 && x < this.width && y >= 0 && y < this.height)
{
if (this.allGems[x, y] != null)
{
_print += "(" + x + "," + y + ")" + System.Environment.NewLine;
this.allGems[x, y].isMatch = true;
this.currentMatches.Add(this.allGems[x, y]);
}
}
}
}
this.currentMatches = this.currentMatches.Distinct().ToList();
}
}
// using System.Collections;
// using System.Collections.Generic;
// using System.Linq;
// using UnityEngine;
// using Enums;
//
// //Done, moved to MatchService and GameBoard
// public class GameBoard
// {
// // - sets size of the grid
// // - handles the score
// // - holds the current matches
// #region Variables
//
// private int height = 0;
// public int Height { get { return this.height; } }
//
// private int width = 0;
// public int Width { get { return this.width; } }
//
// private SC_Gem[,] allGems;
// // public Gem[,] AllGems { get { return allGems; } }
//
// private int score = 0;
// public int Score
// {
// get { return this.score; }
// set {
// this.score = value; }
// }
//
// private List<SC_Gem> currentMatches = new List<SC_Gem>();
// public List<SC_Gem> CurrentMatches { get { return this.currentMatches; } }
// #endregion
//
// public GameBoard(int _Width, int _Height)
// {
// this.height = _Height;
// this.width = _Width;
// this.allGems = new SC_Gem[this.width, this.height];
// }
//
// //checks if there are 3 gems of the same type next to each other
// //used during setup to avoid matches on game start
// //MatchService
// public bool MatchesAt(Vector2Int _PositionToCheck, SC_Gem _GemToCheck)
// {
// if (_PositionToCheck.x > 1)
// {
// if (this.allGems[_PositionToCheck.x - 1, _PositionToCheck.y].type == _GemToCheck.type && this.allGems[_PositionToCheck.x - 2, _PositionToCheck.y].type == _GemToCheck.type)
// return true;
// }
//
// if (_PositionToCheck.y > 1)
// {
// if (this.allGems[_PositionToCheck.x, _PositionToCheck.y - 1].type == _GemToCheck.type && this.allGems[_PositionToCheck.x, _PositionToCheck.y - 2].type == _GemToCheck.type)
// return true;
// }
//
// return false;
// }
//
// //places the gem in the 2d array
// public void SetGem(int _X, int _Y, SC_Gem _Gem)
// {
// this.allGems[_X, _Y] = _Gem;
// }
//
// public SC_Gem GetGem(int _X,int _Y)
// {
// return this.allGems[_X, _Y];
// }
//
// //MatchService
// public void FindAllMatches()
// {
// this.currentMatches.Clear();
//
// for (int x = 0; x < this.width; x++)
// for (int y = 0; y < this.height; y++)
// {
// SC_Gem currentGem = this.allGems[x, y];
// if (currentGem != null)
// {
// if (x > 0 && x < this.width - 1)
// {
// SC_Gem leftGem = this.allGems[x - 1, y];
// SC_Gem rightGem = this.allGems[x + 1, y];
// //checking no empty spots
// if (leftGem != null && rightGem != null)
// {
// //Match
// if (leftGem.type == currentGem.type && rightGem.type == currentGem.type)
// {
// currentGem.isMatch = true;
// leftGem.isMatch = true;
// rightGem.isMatch = true;
// this.currentMatches.Add(currentGem);
// this.currentMatches.Add(leftGem);
// this.currentMatches.Add(rightGem);
// }
// }
// }
//
// if (y > 0 && y < this.height - 1)
// {
// SC_Gem aboveGem = this.allGems[x, y - 1];
// SC_Gem bellowGem = this.allGems[x, y + 1];
// //checking no empty spots
// if (aboveGem != null && bellowGem != null)
// {
// //Match
// if (aboveGem.type == currentGem.type && bellowGem.type == currentGem.type)
// {
// currentGem.isMatch = true;
// aboveGem.isMatch = true;
// bellowGem.isMatch = true;
// this.currentMatches.Add(currentGem);
// this.currentMatches.Add(aboveGem);
// this.currentMatches.Add(bellowGem);
// }
// }
// }
// }
// }
//
// if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();
//
// CheckForBombs();
// }
//
// public void CheckForBombs()
// {
// for (int i = 0; i < this.currentMatches.Count; i++)
// {
// SC_Gem gem = this.currentMatches[i];
// int x = gem.posIndex.x;
// int y = gem.posIndex.y;
//
// if (gem.posIndex.x > 0)
// {
// if (this.allGems[x - 1, y] != null && this.allGems[x - 1, y].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x - 1, y), this.allGems[x - 1, y].blastSize);
// }
//
// if (gem.posIndex.x + 1 < this.width)
// {
// if (this.allGems[x + 1, y] != null && this.allGems[x + 1, y].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x + 1, y), this.allGems[x + 1, y].blastSize);
// }
//
// if (gem.posIndex.y > 0)
// {
// if (this.allGems[x, y - 1] != null && this.allGems[x, y - 1].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x, y - 1), this.allGems[x, y - 1].blastSize);
// }
//
// if (gem.posIndex.y + 1 < this.height)
// {
// if (this.allGems[x, y + 1] != null && this.allGems[x, y + 1].type == GemType.Bomb)
// MarkBombArea(new Vector2Int(x, y + 1), this.allGems[x, y + 1].blastSize);
// }
// }
// }
//
// public void MarkBombArea(Vector2Int bombPos, int _BlastSize)
// {
// string _print = "";
// for (int x = bombPos.x - _BlastSize; x <= bombPos.x + _BlastSize; x++)
// {
// for (int y = bombPos.y - _BlastSize; y <= bombPos.y + _BlastSize; y++)
// {
// if (x >= 0 && x < this.width && y >= 0 && y < this.height)
// {
// if (this.allGems[x, y] != null)
// {
// _print += "(" + x + "," + y + ")" + System.Environment.NewLine;
// this.allGems[x, y].isMatch = true;
// this.currentMatches.Add(this.allGems[x, y]);
// }
// }
// }
// }
//
// this.currentMatches = this.currentMatches.Distinct().ToList();
// }
// }
//

View File

@@ -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
// }

View File

@@ -1,38 +1,38 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Done
//This is basically just the settings, this can maybe be a ScriptableObject
public class SC_GameVariables : MonoBehaviour
{
public GameObject bgTilePrefabs;
public SC_Gem bomb;
public SC_Gem[] gems;
public float bonusAmount = 0.5f;
public float bombChance = 2f;
public int dropHeight = 0;
public float gemSpeed;
public float scoreSpeed = 5;
[HideInInspector]
public int rowsSize = 7;
[HideInInspector]
public int colsSize = 7;
#region Singleton
static SC_GameVariables instance;
public static SC_GameVariables Instance
{
get
{
if (instance == null)
instance = GameObject.Find("SC_GameVariables").GetComponent<SC_GameVariables>();
return instance;
}
}
#endregion
}
// using System.Collections;
// using System.Collections.Generic;
// using UnityEngine;
//
// //Done
// //This is basically just the settings, this can maybe be a ScriptableObject
// public class SC_GameVariables : MonoBehaviour
// {
// public GameObject bgTilePrefabs;
// public SC_Gem bomb;
// public SC_Gem[] gems;
// public float bonusAmount = 0.5f;
// public float bombChance = 2f;
// public int dropHeight = 0;
// public float gemSpeed;
// public float scoreSpeed = 5;
//
// [HideInInspector]
// public int rowsSize = 7;
// [HideInInspector]
// public int colsSize = 7;
//
// #region Singleton
//
// static SC_GameVariables instance;
// public static SC_GameVariables Instance
// {
// get
// {
// if (instance == null)
// instance = GameObject.Find("SC_GameVariables").GetComponent<SC_GameVariables>();
//
// return instance;
// }
// }
//
// #endregion
// }

View File

@@ -1,140 +1,140 @@
using System.Collections;
using System.Collections.Generic;
using Enums;
using UnityEngine;
//This is a SINGULAR gem
public class SC_Gem : MonoBehaviour
{
[HideInInspector]
public Vector2Int posIndex;
private Vector2 firstTouchPosition;
private Vector2 finalTouchPosition;
private bool mousePressed;
private float swipeAngle = 0;
private SC_Gem otherGem;
public GemType type;
public bool isMatch = false;
private Vector2Int previousPos;
public GameObject destroyEffect;
public int scoreValue = 10;
public int blastSize = 1;
private SC_GameLogic scGameLogic;
void Update()
{
//if the current position doesnt match the index in GameBoard, animate it to move to the correct position
//else, we update the GameBoard with the current possition, do we need this every frame????
if (Vector2.Distance(this.transform.position, this.posIndex) > 0.01f)
this.transform.position = Vector2.Lerp(this.transform.position, this.posIndex, SC_GameVariables.Instance.gemSpeed * Time.deltaTime);
else
{
this.transform.position = new Vector3(this.posIndex.x, this.posIndex.y, 0);
this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this);
}
if (this.mousePressed && Input.GetMouseButtonUp(0))
{
this.mousePressed = false;
if (this.scGameLogic.CurrentState == GameState.Move)
{
this.finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
CalculateAngle();
}
}
}
public void SetupGem(SC_GameLogic _ScGameLogic,Vector2Int _Position)
{
this.posIndex = _Position;
this.scGameLogic = _ScGameLogic;
}
//Not every gem needs this
private void OnMouseDown()
{
if (this.scGameLogic.CurrentState == GameState.Move)
{
this.firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
this.mousePressed = true;
}
}
//Not every gem needs this
private void CalculateAngle()
{
this.swipeAngle = Mathf.Atan2(this.finalTouchPosition.y - this.firstTouchPosition.y, this.finalTouchPosition.x - this.firstTouchPosition.x);
this.swipeAngle = this.swipeAngle * 180 / Mathf.PI;
if (Vector3.Distance(this.firstTouchPosition, this.finalTouchPosition) > .5f)
MovePieces();
}
//Not every gem needs this, maybe
private void MovePieces()
{
this.previousPos = this.posIndex;
if (this.swipeAngle < 45 && this.swipeAngle > -45 && this.posIndex.x < SC_GameVariables.Instance.rowsSize - 1)
{
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x + 1, this.posIndex.y);
this.otherGem.posIndex.x--;
this.posIndex.x++;
}
else if (this.swipeAngle > 45 && this.swipeAngle <= 135 && this.posIndex.y < SC_GameVariables.Instance.colsSize - 1)
{
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y + 1);
this.otherGem.posIndex.y--;
this.posIndex.y++;
}
else if (this.swipeAngle < -45 && this.swipeAngle >= -135 && this.posIndex.y > 0)
{
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y - 1);
this.otherGem.posIndex.y++;
this.posIndex.y--;
}
else if (this.swipeAngle > 135 || this.swipeAngle < -135 && this.posIndex.x > 0)
{
this.otherGem = this.scGameLogic.GetGem(this.posIndex.x - 1, this.posIndex.y);
this.otherGem.posIndex.x++;
this.posIndex.x--;
}
this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this);
this.scGameLogic.SetGem(this.otherGem.posIndex.x, this.otherGem.posIndex.y, this.otherGem);
StartCoroutine(CheckMoveCo());
}
//Why are we checking matches on the Gem itself
public IEnumerator CheckMoveCo()
{
this.scGameLogic.SetState(GameState.Wait);
yield return new WaitForSeconds(.5f);
this.scGameLogic.FindAllMatches();
if (this.otherGem != null)
{
if (this.isMatch == false && this.otherGem.isMatch == false)
{
this.otherGem.posIndex = this.posIndex;
this.posIndex = this.previousPos;
this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this);
this.scGameLogic.SetGem(this.otherGem.posIndex.x, this.otherGem.posIndex.y, this.otherGem);
yield return new WaitForSeconds(.5f);
this.scGameLogic.SetState(GameState.Move);
}
else
{
this.scGameLogic.DestroyMatches();
}
}
}
}
// using System.Collections;
// using System.Collections.Generic;
// using Enums;
// using UnityEngine;
//
// //This is a SINGULAR gem
// public class SC_Gem : MonoBehaviour
// {
// [HideInInspector]
// public Vector2Int posIndex;
//
// private Vector2 firstTouchPosition;
// private Vector2 finalTouchPosition;
// private bool mousePressed;
// private float swipeAngle = 0;
// private SC_Gem otherGem;
//
// public GemType type;
// public bool isMatch = false;
// private Vector2Int previousPos;
// public GameObject destroyEffect;
// public int scoreValue = 10;
//
// public int blastSize = 1;
// private SC_GameLogic scGameLogic;
//
// void Update()
// {
// //if the current position doesnt match the index in GameBoard, animate it to move to the correct position
// //else, we update the GameBoard with the current possition, do we need this every frame????
// if (Vector2.Distance(this.transform.position, this.posIndex) > 0.01f)
// this.transform.position = Vector2.Lerp(this.transform.position, this.posIndex, SC_GameVariables.Instance.gemSpeed * Time.deltaTime);
// else
// {
// this.transform.position = new Vector3(this.posIndex.x, this.posIndex.y, 0);
// this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this);
// }
//
// if (this.mousePressed && Input.GetMouseButtonUp(0))
// {
// this.mousePressed = false;
// if (this.scGameLogic.CurrentState == GameState.Move)
// {
// this.finalTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// CalculateAngle();
// }
// }
// }
//
// public void SetupGem(SC_GameLogic _ScGameLogic,Vector2Int _Position)
// {
// this.posIndex = _Position;
// this.scGameLogic = _ScGameLogic;
// }
//
// //Not every gem needs this
// private void OnMouseDown()
// {
// if (this.scGameLogic.CurrentState == GameState.Move)
// {
// this.firstTouchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// this.mousePressed = true;
// }
// }
//
// //Not every gem needs this
// private void CalculateAngle()
// {
// this.swipeAngle = Mathf.Atan2(this.finalTouchPosition.y - this.firstTouchPosition.y, this.finalTouchPosition.x - this.firstTouchPosition.x);
// this.swipeAngle = this.swipeAngle * 180 / Mathf.PI;
//
// if (Vector3.Distance(this.firstTouchPosition, this.finalTouchPosition) > .5f)
// MovePieces();
// }
//
// //Not every gem needs this, maybe
// private void MovePieces()
// {
// this.previousPos = this.posIndex;
//
// if (this.swipeAngle < 45 && this.swipeAngle > -45 && this.posIndex.x < SC_GameVariables.Instance.rowsSize - 1)
// {
// this.otherGem = this.scGameLogic.GetGem(this.posIndex.x + 1, this.posIndex.y);
// this.otherGem.posIndex.x--;
// this.posIndex.x++;
//
// }
// else if (this.swipeAngle > 45 && this.swipeAngle <= 135 && this.posIndex.y < SC_GameVariables.Instance.colsSize - 1)
// {
// this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y + 1);
// this.otherGem.posIndex.y--;
// this.posIndex.y++;
// }
// else if (this.swipeAngle < -45 && this.swipeAngle >= -135 && this.posIndex.y > 0)
// {
// this.otherGem = this.scGameLogic.GetGem(this.posIndex.x, this.posIndex.y - 1);
// this.otherGem.posIndex.y++;
// this.posIndex.y--;
// }
// else if (this.swipeAngle > 135 || this.swipeAngle < -135 && this.posIndex.x > 0)
// {
// this.otherGem = this.scGameLogic.GetGem(this.posIndex.x - 1, this.posIndex.y);
// this.otherGem.posIndex.x++;
// this.posIndex.x--;
// }
//
// this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this);
// this.scGameLogic.SetGem(this.otherGem.posIndex.x, this.otherGem.posIndex.y, this.otherGem);
//
// StartCoroutine(CheckMoveCo());
// }
//
// //Why are we checking matches on the Gem itself
// public IEnumerator CheckMoveCo()
// {
// this.scGameLogic.SetState(GameState.Wait);
//
// yield return new WaitForSeconds(.5f);
// this.scGameLogic.FindAllMatches();
//
// if (this.otherGem != null)
// {
// if (this.isMatch == false && this.otherGem.isMatch == false)
// {
// this.otherGem.posIndex = this.posIndex;
// this.posIndex = this.previousPos;
//
// this.scGameLogic.SetGem(this.posIndex.x, this.posIndex.y, this);
// this.scGameLogic.SetGem(this.otherGem.posIndex.x, this.otherGem.posIndex.y, this.otherGem);
//
// yield return new WaitForSeconds(.5f);
// this.scGameLogic.SetState(GameState.Move);
// }
// else
// {
// this.scGameLogic.DestroyMatches();
// }
// }
// }
// }

View File

@@ -105,6 +105,8 @@ namespace Services {
}
nullCounter = 0;
}
await FillBoard();
}
public async UniTask FillBoard() {