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

View File

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

View File

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

View File

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

View File

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