I am currently playing around with a project similar to the game Collapse.
This is my version of the grid that get created on startup.
My question is, how would I check for tiles that are adjacent to each other? For example, In the image above, there are 8 red tiles touching one another on the bottom left. If I were to click the red tile in the very corner, all 8 of those tiles should be cleared and new ones should fall down. I have researched and looked hard for a solution and have yet to find one so and help would be appreciated. I do know quite a bit of coding, Im just diving in to something new to widen my knowledge.
This is my Grid Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Grid : MonoBehaviour {
public enum TileType
{
NORMAL,
COUNT,
};
[System.Serializable]
public struct TilePrefab
{
public TileType type;
public GameObject prefab;
};
public int xDim;
public int yDim;
public TilePrefab[] tilePrefabs;
public GameObject backgroundPrefab;
private Dictionary<TileType, GameObject> tilePrefabDict;
private Tile[,] tile;
void Start(){
tilePrefabDict = new Dictionary<TileType, GameObject>();
for (int i = 0; i < tilePrefabs.Length; i++) {
if(!tilePrefabDict.ContainsKey(tilePrefabs*.type)){*
tilePrefabDict.Add (tilePrefabs .type, tilePrefabs .prefab);
* }*
* }*
* for (int x = 0; x < xDim; x++) {*
* for (int y = 0; y < yDim; y++) {*
* GameObject background = (GameObject)Instantiate (backgroundPrefab, GetWorldPosition(x, y, 0), Quaternion.identity);*
* background.transform.SetParent (transform);*
* }*
* }*
* tile = new Tile[xDim, yDim];*
* for (int x = 0; x < xDim; x++) {*
* for (int y = 0; y < yDim; y++) {*
* GameObject newTile = (GameObject)Instantiate (tilePrefabDict [TileType.NORMAL], GetWorldPosition(x,y, -1), Quaternion.identity);*
* newTile.name = “Piece(” + x + “,” + y + “)”;*
* newTile.transform.SetParent (transform);*
* tile [x, y] = newTile.GetComponent ();*
* tile [x, y].Init (x, y, this, TileType.NORMAL);*
* if (tile [x, y].IsColored ()) {*
* tile [x, y].ColorComponent.SetColor ((TileColor.ColorType)Random.Range(0,tile[x,y].ColorComponent.NumColors));*
* }*
* }*
* }*
* }*
* Vector3 GetWorldPosition(int x, int y, int z){*
* return new Vector3 (transform.position.x - xDim / 2 + x,*
* transform.position.y + yDim / 2 - y, z);*
* }*
}
And here is my Tile Script:
using UnityEngine;
using System.Collections;
public class Tile : MonoBehaviour {
* private int x;*
* private int y;*
* public int X{*
* get{ return x;}*
* }*
* public int Y{*
* get{ return y;}*
* }*
* private Grid.TileType type;*
* public Grid.TileType Type{*
* get{ return type;}*
* }*
* private Grid grid;*
* public Grid GridRef{*
* get { return grid;}*
* }*
* private TileColor colorComponent;*
* public TileColor ColorComponent{*
* get{ return colorComponent;}*
* }*
* public void Init(int _x, int _y, Grid _grid, Grid.TileType _type){
x = _x;
y = _y;
grid = grid;
type = type;*
* }*
* void Awake(){*
* colorComponent = GetComponent ();*
* }*
* public bool IsColored(){*
* return colorComponent != null;*
* }*
* void OnMouseDown(){*
* Debug.Log (“Has been clicked!”);*
* }*
}
Again, any help would be appreciated and if you can explain the issue I am having with any example, that would be great. I am more of a visual learner with this kind of stuff.