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,16 +2,21 @@
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 height; } }
public int Height { get { return this.height; } }
private int width = 0;
public int Width { get { return width; } }
public int Width { get { return this.width; } }
private SC_Gem[,] allGems;
// public Gem[,] AllGems { get { return allGems; } }
@@ -19,62 +24,68 @@ public class GameBoard
private int score = 0;
public int Score
{
get { return score; }
set { score = value; }
get { return this.score; }
set {
this.score = value; }
}
private List<SC_Gem> currentMatches = new List<SC_Gem>();
public List<SC_Gem> CurrentMatches { get { return currentMatches; } }
public List<SC_Gem> CurrentMatches { get { return this.currentMatches; } }
#endregion
public GameBoard(int _Width, int _Height)
{
height = _Height;
width = _Width;
allGems = new SC_Gem[width, 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 (allGems[_PositionToCheck.x - 1, _PositionToCheck.y].type == _GemToCheck.type &&
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;
}
if (_PositionToCheck.y > 1)
{
if (allGems[_PositionToCheck.x, _PositionToCheck.y - 1].type == _GemToCheck.type &&
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 false;
}
//places the gem in the 2d array
public void SetGem(int _X, int _Y, SC_Gem _Gem)
{
allGems[_X, _Y] = _Gem;
this.allGems[_X, _Y] = _Gem;
}
public SC_Gem GetGem(int _X,int _Y)
{
return allGems[_X, _Y];
return this.allGems[_X, _Y];
}
//MatchService
public void FindAllMatches()
{
currentMatches.Clear();
this.currentMatches.Clear();
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
for (int x = 0; x < this.width; x++)
for (int y = 0; y < this.height; y++)
{
SC_Gem currentGem = allGems[x, y];
SC_Gem currentGem = this.allGems[x, y];
if (currentGem != null)
{
if (x > 0 && x < width - 1)
if (x > 0 && x < this.width - 1)
{
SC_Gem leftGem = allGems[x - 1, y];
SC_Gem rightGem = allGems[x + 1, y];
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)
{
@@ -84,17 +95,17 @@ public class GameBoard
currentGem.isMatch = true;
leftGem.isMatch = true;
rightGem.isMatch = true;
currentMatches.Add(currentGem);
currentMatches.Add(leftGem);
currentMatches.Add(rightGem);
this.currentMatches.Add(currentGem);
this.currentMatches.Add(leftGem);
this.currentMatches.Add(rightGem);
}
}
}
if (y > 0 && y < height - 1)
if (y > 0 && y < this.height - 1)
{
SC_Gem aboveGem = allGems[x, y - 1];
SC_Gem bellowGem = allGems[x, y + 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)
{
@@ -104,51 +115,50 @@ public class GameBoard
currentGem.isMatch = true;
aboveGem.isMatch = true;
bellowGem.isMatch = true;
currentMatches.Add(currentGem);
currentMatches.Add(aboveGem);
currentMatches.Add(bellowGem);
this.currentMatches.Add(currentGem);
this.currentMatches.Add(aboveGem);
this.currentMatches.Add(bellowGem);
}
}
}
}
}
if (currentMatches.Count > 0)
currentMatches = currentMatches.Distinct().ToList();
if (this.currentMatches.Count > 0) this.currentMatches = this.currentMatches.Distinct().ToList();
CheckForBombs();
}
public void CheckForBombs()
{
for (int i = 0; i < currentMatches.Count; i++)
for (int i = 0; i < this.currentMatches.Count; i++)
{
SC_Gem gem = currentMatches[i];
SC_Gem gem = this.currentMatches[i];
int x = gem.posIndex.x;
int y = gem.posIndex.y;
if (gem.posIndex.x > 0)
{
if (allGems[x - 1, y] != null && allGems[x - 1, y].type == GlobalEnums.GemType.bomb)
MarkBombArea(new Vector2Int(x - 1, y), allGems[x - 1, y].blastSize);
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 < width)
if (gem.posIndex.x + 1 < this.width)
{
if (allGems[x + 1, y] != null && allGems[x + 1, y].type == GlobalEnums.GemType.bomb)
MarkBombArea(new Vector2Int(x + 1, y), allGems[x + 1, y].blastSize);
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 (allGems[x, y - 1] != null && allGems[x, y - 1].type == GlobalEnums.GemType.bomb)
MarkBombArea(new Vector2Int(x, y - 1), allGems[x, y - 1].blastSize);
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 < height)
if (gem.posIndex.y + 1 < this.height)
{
if (allGems[x, y + 1] != null && allGems[x, y + 1].type == GlobalEnums.GemType.bomb)
MarkBombArea(new Vector2Int(x, y + 1), allGems[x, y + 1].blastSize);
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);
}
}
}
@@ -160,18 +170,19 @@ public class GameBoard
{
for (int y = bombPos.y - _BlastSize; y <= bombPos.y + _BlastSize; y++)
{
if (x >= 0 && x < width && y >= 0 && y < height)
if (x >= 0 && x < this.width && y >= 0 && y < this.height)
{
if (allGems[x, y] != null)
if (this.allGems[x, y] != null)
{
_print += "(" + x + "," + y + ")" + System.Environment.NewLine;
allGems[x, y].isMatch = true;
currentMatches.Add(allGems[x, y]);
this.allGems[x, y].isMatch = true;
this.currentMatches.Add(this.allGems[x, y]);
}
}
}
}
currentMatches = currentMatches.Distinct().ToList();
this.currentMatches = this.currentMatches.Distinct().ToList();
}
}