Skip to content

Commit

Permalink
Add Item Matching
Browse files Browse the repository at this point in the history
  • Loading branch information
gulraiznoorbari committed Feb 28, 2024
1 parent b9b65ef commit 0864084
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 45 deletions.
12 changes: 7 additions & 5 deletions Assets/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,11 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_gridCellPrefab: {fileID: 6987231103175690360, guid: 71bea0f861a877640875f39a0a555fa5, type: 3}
_gridCellLayer:
serializedVersion: 2
m_Bits: 8
_rows: 7
_columns: 7
_itemPrefab: {fileID: 3501801838660937944, guid: da0246536a9569a4d9bdc82dd084e31d, type: 3}
--- !u!4 &1598511433
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -468,14 +470,14 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: eefa8a3b1cce81844963fcad5c77ea11, type: 3}
m_Name:
m_EditorClassIdentifier:
_availableTilesPrefab:
- {fileID: 5748778991046782161, guid: 080678d25bcc9054da155989c4807b8a, type: 3}
- {fileID: 4401121544082299809, guid: d80d8b0829ded134ebfced75ff9d19c1, type: 3}
- {fileID: 6520477855212085761, guid: c3cb74c9a5c4e704a9392ee7319c96b7, type: 3}
_spawnPoints:
- {fileID: 2036235606}
- {fileID: 944607607}
- {fileID: 354453704}
_tilesPrefabs:
- {fileID: 5748778991046782161, guid: 080678d25bcc9054da155989c4807b8a, type: 3}
- {fileID: 4401121544082299809, guid: d80d8b0829ded134ebfced75ff9d19c1, type: 3}
- {fileID: 6520477855212085761, guid: c3cb74c9a5c4e704a9392ee7319c96b7, type: 3}
--- !u!4 &1963429256
Transform:
m_ObjectHideFlags: 0
Expand Down
70 changes: 60 additions & 10 deletions Assets/Scripts/GridManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class GridManager : MonoBehaviour
{
[Header("Grid Generation Info")]
[SerializeField] private Cell _gridCellPrefab;
[SerializeField] private LayerMask _gridCellLayer;
[SerializeField] private int _rows = 7;
[SerializeField] private int _columns = 7;

Expand All @@ -30,11 +31,6 @@ private void Start()
GenerateGrid();
}

private void Update()
{

}

private void GenerateGrid()
{
GameObject gridParent = new GameObject("Grid Cells");
Expand All @@ -45,27 +41,81 @@ private void GenerateGrid()
{
Vector3 cellPosition = new Vector3(i, 0, j);
var gridCell = Instantiate(_gridCellPrefab, cellPosition, Quaternion.identity);
gridCell.name = $"Tile_{i}x{j}";
gridCell.name = $"Cell_{i}x{j}";
gridCell.transform.parent = gridParent.transform;

var isOddPosition = (i % 2 != 0 && j % 2 == 0) || (i % 2 == 0 && j % 2 != 0);
gridCell.ChangeColor(isOddPosition);

// Mark the cell as initially unoccupied
// Mark the cell as initially unoccupied:
occupiedCells.Add(cellPosition, false);
}
}
}

public bool IsCellEmpty(Vector3 position)
public void CheckNeighborsForColorMatch(Vector3 currentTilePosition)
{
// Check immediate neighbors (up, down, left, right) and diagonals:
Vector3[] directions = {
Vector3.forward, Vector3.back, Vector3.left, Vector3.right,
new Vector3(1, 0, 1), new Vector3(1, 0, -1),
new Vector3(-1, 0, 1), new Vector3(-1, 0, -1)
};

foreach (Vector3 direction in directions)
{
Vector3 neighborPosition = currentTilePosition + direction;
bool isCellEmptyAndInbounds = IsCellEmptyAndInbounds(neighborPosition);
Debug.Log(isCellEmptyAndInbounds);
// If the neighboring cell is not empty and within bounds:
if (!isCellEmptyAndInbounds)
{
Cell neighborCell = GetCellAtPosition(neighborPosition);
if (neighborCell != null && neighborCell.transform.childCount > 0)
{
Tile neighborTile = neighborCell.transform.GetChild(0).GetComponent<Tile>();
if (neighborTile != null && neighborTile.gameObject != gameObject)
{
// Check if the neighbor tile has the same color:
if (neighborTile.GetComponent<Renderer>().material.color == GetComponent<Renderer>().material.color)
{
Destroy(neighborTile.gameObject);
Destroy(gameObject);
return;
}
}
}
}
}
}

public Cell GetCellAtPosition(Vector3 position)
{
RaycastHit hit;
if (Physics.Raycast(position + Vector3.up * 10f, Vector3.down, out hit, 50f, _gridCellLayer))
{
return hit.collider.GetComponent<Cell>();
}
return null;
}

public void MarkCellOccupied(Vector3 position)
{
if (occupiedCells.ContainsKey(position))
{
occupiedCells[position] = true;
}
}

public bool IsCellEmptyAndInbounds(Vector3 position)
{
// Check if the position is within the bounds of the grid
// Check if the position is within the bounds of the grid:
if (position.x < 0 || position.x >= _rows || position.z < 0 || position.z >= _columns)
{
return false;
}

// Check if the position exists in the dictionary, if not, consider it as empty
// Check if the position exists in the dictionary, if not, consider it as empty:
bool isCellEmpty;
if (!occupiedCells.TryGetValue(position, out isCellEmpty))
{
Expand Down
22 changes: 15 additions & 7 deletions Assets/Scripts/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Tile : MonoBehaviour
{
private bool dragging, placed;
private Camera camera;
private Camera cam;
private Vector3 offset;
private Vector3 originalPosition;
private Vector3 tileSize;
Expand All @@ -12,14 +12,13 @@ private void Start()
{
originalPosition = transform.position;
tileSize = transform.localScale;
camera = Camera.main;
cam = Camera.main;
}

private void FixedUpdate()
{
if (!dragging) return;
transform.position = GetSnappedPosition();
//transform.position = new Vector3(GetMousePosition().x - offset.x, 0.5f, GetMousePosition().z - offset.z);
}

private void OnMouseDown()
Expand All @@ -31,7 +30,6 @@ private void OnMouseDown()
private void OnMouseUp()
{
dragging = false;
//transform.position = originalPosition;
SnapToGrid();
}

Expand All @@ -40,15 +38,25 @@ private Vector3 GetSnappedPosition()
Vector3 mousePosition = GetMousePosition();
float snapX = Mathf.Round(mousePosition.x / tileSize.x) * tileSize.x;
float snapZ = Mathf.Round(mousePosition.z / tileSize.z) * tileSize.z;
return new Vector3(snapX, 0.4f, snapZ);
return new Vector3(snapX, 0.27f, snapZ);
}

private void SnapToGrid()
{
if (placed) return;
Vector3 snappedPosition = GetSnappedPosition();
if (GridManager.instance.IsCellEmpty(snappedPosition))
if (GridManager.instance.IsCellEmptyAndInbounds(snappedPosition))
{
transform.parent = null;
transform.position = snappedPosition;
Cell cell = GridManager.instance.GetCellAtPosition(snappedPosition);
if (cell != null)
{
transform.parent = cell.transform;
placed = true;
GridManager.instance.MarkCellOccupied(snappedPosition);
GridManager.instance.CheckNeighborsForColorMatch(snappedPosition);
}
TilesSpawner.instance.RemoveFromAvailableTilesList(gameObject);
}
else
Expand All @@ -59,7 +67,7 @@ private void SnapToGrid()

private Vector3 GetMousePosition()
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit))
Expand Down
45 changes: 24 additions & 21 deletions Assets/Scripts/TilesSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
public class TilesSpawner : MonoBehaviour
{
[SerializeField] private Transform[] _spawnPoints;
[SerializeField] private GameObject[] _tilesPrefabs;

public GameObject[] _availableTilesPrefab;
private GameObject[] _availableTilesPrefabs;

public static TilesSpawner instance;

private void Awake()
Expand All @@ -26,41 +28,38 @@ private void Start()
SpawnTiles();
}

private void Update()
private void SpawnTiles()
{
if (_availableTilesPrefab.Length < 1)
for (int i = 0; i < _spawnPoints.Length; i++)
{
SpawnTiles();
}
}

public void SpawnTiles()
{
for (int i = 0; i < _availableTilesPrefab.Length; i++)
{
int randomIndex = Random.Range(0, _availableTilesPrefab.Length);
GameObject obj = Instantiate(_availableTilesPrefab[randomIndex], _spawnPoints[i]);
obj.name = _availableTilesPrefab[randomIndex].name;
//int randomIndex = Random.Range(0, _tilesPrefabs.Length);
GameObject obj = Instantiate(_tilesPrefabs[i], _spawnPoints[i]);
obj.name = _tilesPrefabs[i].name;
}
_availableTilesPrefabs = _tilesPrefabs;
}

private int FindIndexByName(string gameObjectName)
{
for (int i = 0; i < _availableTilesPrefab.Length; i++)
int index = -1;
for (int i = 0; i < _availableTilesPrefabs.Length; i++)
{
if (_availableTilesPrefab[i].name == gameObjectName)
if (_availableTilesPrefabs[i].name == gameObjectName)
{
return i;
index = i;
break;
}
}
return -1;
return index;
}

private void RemoveByIndex(int index)
{
if (index >= 0 && index < _availableTilesPrefab.Length)
if (index >= 0 && index < _availableTilesPrefabs.Length)
{
_availableTilesPrefab[index] = null;
List<GameObject> tempList = new List<GameObject>(_availableTilesPrefabs);
tempList.RemoveAt(index);
_availableTilesPrefabs = tempList.ToArray();
}
}

Expand All @@ -70,10 +69,14 @@ public void RemoveFromAvailableTilesList(GameObject gameObject)
if (index != -1)
{
RemoveByIndex(index);
if (_availableTilesPrefabs.Length == 0)
{
SpawnTiles();
}
}
else
{
Debug.LogWarning("GameObject not found in _availableTilesPrefab array: " + gameObject.name);
Debug.LogWarning("GameObject not found: " + gameObject.name);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": {
"com.unity.collab-proxy": "1.11.2",
"com.unity.ide.rider": "2.0.7",
"com.unity.ide.visualstudio": "2.0.11",
"com.unity.ide.visualstudio": "2.0.22",
"com.unity.ide.vscode": "1.2.4",
"com.unity.test-framework": "1.1.29",
"com.unity.textmeshpro": "3.0.6",
Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "https://packages.unity.com"
},
"com.unity.ide.visualstudio": {
"version": "2.0.11",
"version": "2.0.22",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down

0 comments on commit 0864084

Please sign in to comment.