Create All Needed Scripts

This commit is contained in:
2025-12-14 09:23:38 +08:00
parent 980b26dd5e
commit 6fe70bd113
53 changed files with 1988 additions and 195 deletions

View File

@@ -2,15 +2,16 @@
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Enums;
public class SC_GameLogic : MonoBehaviour
{
private Dictionary<string, GameObject> unityObjects;
private int score = 0;
private float displayScore = 0;
private GameBoard gameBoard;
private GlobalEnums.GameState currentState = GlobalEnums.GameState.move;
public GlobalEnums.GameState CurrentState { get { return currentState; } }
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()
@@ -25,36 +26,37 @@ public class SC_GameLogic : MonoBehaviour
private void Update()
{
displayScore = Mathf.Lerp(displayScore, gameBoard.Score, SC_GameVariables.Instance.scoreSpeed * Time.deltaTime);
unityObjects["Txt_Score"].GetComponent<TMPro.TextMeshProUGUI>().text = displayScore.ToString("0");
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()
{
unityObjects = new Dictionary<string, GameObject>();
this.unityObjects = new Dictionary<string, GameObject>();
GameObject[] _obj = GameObject.FindGameObjectsWithTag("UnityObject");
foreach (GameObject g in _obj)
unityObjects.Add(g.name,g);
foreach (GameObject g in _obj) this.unityObjects.Add(g.name,g);
gameBoard = new GameBoard(7, 7);
this.gameBoard = new GameBoard(7, 7);
Setup();
}
//GameBoardService
private void Setup()
{
for (int x = 0; x < gameBoard.Width; x++)
for (int y = 0; y < gameBoard.Height; y++)
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(unityObjects["GemsHolder"].transform);
_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 (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);
iterations++;
@@ -64,52 +66,64 @@ public class SC_GameLogic : MonoBehaviour
}
public void StartGame()
{
unityObjects["Txt_Score"].GetComponent<TextMeshProUGUI>().text = score.ToString("0");
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(unityObjects["GemsHolder"].transform);
_gem.transform.SetParent(this.unityObjects["GemsHolder"].transform);
_gem.name = "Gem - " + _Position.x + ", " + _Position.y;
gameBoard.SetGem(_Position.x,_Position.y, _gem);
this.gameBoard.SetGem(_Position.x,_Position.y, _gem);
_gem.SetupGem(this,_Position);
}
//GameBoardService
public void SetGem(int _X,int _Y, SC_Gem _Gem)
{
gameBoard.SetGem(_X,_Y, _Gem);
this.gameBoard.SetGem(_X,_Y, _Gem);
}
//GameBoardService
public SC_Gem GetGem(int _X, int _Y)
{
return gameBoard.GetGem(_X, _Y);
return this.gameBoard.GetGem(_X, _Y);
}
public void SetState(GlobalEnums.GameState _CurrentState)
public void SetState(GameState _CurrentState)
{
currentState = _CurrentState;
this.currentState = _CurrentState;
}
//GameBoardService
public void DestroyMatches()
{
for (int i = 0; i < gameBoard.CurrentMatches.Count; i++)
if (gameBoard.CurrentMatches[i] != null)
for (int i = 0; i < this.gameBoard.CurrentMatches.Count; i++)
if (this.gameBoard.CurrentMatches[i] != null)
{
ScoreCheck(gameBoard.CurrentMatches[i]);
DestroyMatchedGemsAt(gameBoard.CurrentMatches[i].posIndex);
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 < gameBoard.Width; x++)
for (int x = 0; x < this.gameBoard.Width; x++)
{
for (int y = 0; y < gameBoard.Height; y++)
for (int y = 0; y < this.gameBoard.Height; y++)
{
SC_Gem _curGem = gameBoard.GetGem(x, y);
SC_Gem _curGem = this.gameBoard.GetGem(x, y);
if (_curGem == null)
{
nullCounter++;
@@ -127,13 +141,16 @@ public class SC_GameLogic : MonoBehaviour
StartCoroutine(FilledBoardCo());
}
//IScoreService
public void ScoreCheck(SC_Gem gemToCheck)
{
gameBoard.Score += gemToCheck.scoreValue;
this.gameBoard.Score += gemToCheck.scoreValue;
}
//GameBoardService - DestroyMatchedGems
private void DestroyMatchedGemsAt(Vector2Int _Pos)
{
SC_Gem _curGem = gameBoard.GetGem(_Pos.x,_Pos.y);
SC_Gem _curGem = this.gameBoard.GetGem(_Pos.x,_Pos.y);
if (_curGem != null)
{
Instantiate(_curGem.destroyEffect, new Vector2(_Pos.x, _Pos.y), Quaternion.identity);
@@ -142,14 +159,16 @@ public class SC_GameLogic : MonoBehaviour
SetGem(_Pos.x,_Pos.y, null);
}
}
//GameBoardService - FillBoard
private IEnumerator FilledBoardCo()
{
yield return new WaitForSeconds(0.5f);
RefillBoard();
yield return new WaitForSeconds(0.5f);
gameBoard.FindAllMatches();
if (gameBoard.CurrentMatches.Count > 0)
this.gameBoard.FindAllMatches();
if (this.gameBoard.CurrentMatches.Count > 0)
{
yield return new WaitForSeconds(0.5f);
DestroyMatches();
@@ -157,16 +176,18 @@ public class SC_GameLogic : MonoBehaviour
else
{
yield return new WaitForSeconds(0.5f);
currentState = GlobalEnums.GameState.move;
this.currentState = GameState.Move;
}
}
//GameBoardService
private void RefillBoard()
{
for (int x = 0; x < gameBoard.Width; x++)
for (int x = 0; x < this.gameBoard.Width; x++)
{
for (int y = 0; y < gameBoard.Height; y++)
for (int y = 0; y < this.gameBoard.Height; y++)
{
SC_Gem _curGem = gameBoard.GetGem(x,y);
SC_Gem _curGem = this.gameBoard.GetGem(x,y);
if (_curGem == null)
{
int gemToUse = Random.Range(0, SC_GameVariables.Instance.gems.Length);
@@ -176,15 +197,17 @@ public class SC_GameLogic : MonoBehaviour
}
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 < gameBoard.Width; x++)
for (int x = 0; x < this.gameBoard.Width; x++)
{
for (int y = 0; y < gameBoard.Height; y++)
for (int y = 0; y < this.gameBoard.Height; y++)
{
SC_Gem _curGem = gameBoard.GetGem(x, y);
SC_Gem _curGem = this.gameBoard.GetGem(x, y);
if (foundGems.Contains(_curGem))
foundGems.Remove(_curGem);
}
@@ -195,7 +218,7 @@ public class SC_GameLogic : MonoBehaviour
}
public void FindAllMatches()
{
gameBoard.FindAllMatches();
this.gameBoard.FindAllMatches();
}
#endregion