Urgent PLease:How to find no adjecnt blocks after possible blocks destroyed.PLease find a script below..
using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic;
[RequireComponent(typeof(AudioSource))] public class GameController : MonoBehaviour {
public Object box;
GameObject[] allBox;
public Transform timesUp_ui;
public Text point;
public Text moves_text;
public Text outTime;
public float timer = 10;
public float check = 5.0f;
public string eachname;
int point_Count;
int rowSizeNum;
int colSizeNum;
public int clickCount = 0;
Vector3 pos_Cam;
Vector2 pos_BOX;
public AudioClip impact;
public AudioClip gravity;
AudioSource audio;
public static bool canEmit = false;
private boxController boxController;
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource>();
timesUp_ui.gameObject.SetActive (false);
pos_Cam = new Vector3 (0, 0, 0);
pos_BOX = new Vector2 (0, 0);
rowSizeNum = 10;
colSizeNum = 15;
point_Count = 0;
allBox = new GameObject[rowSizeNum*colSizeNum];
for(int i=0;i<colSizeNum;i++)
{
for(int j=0;j<rowSizeNum;j++)
{
allBox[(i * rowSizeNum) + j] = (GameObject)Instantiate(box, new Vector2(j - 2, i - 2), Quaternion.identity);
eachname = "allBox " + " i " + i + " j " + j;
Debug.Log("eachname"+eachname);
boxController = allBox[(i * rowSizeNum) + j].GetComponent<boxController>();
boxController.ChooseColor((int)(Random.value*3));
}
}
}
// Update is called once per frame
void Update()
{
UpdateMousePos ();
if (timer > 0) {
timer -= Time.deltaTime;
}
if (timer <= 0) {
Debug.Log("gameover");
timesUp_ui.gameObject.SetActive (true);
}
outTime.text = "" + (int)(timer);
check -= Time.deltaTime;
for (int i = rowSizeNum*colSizeNum-1; i >= 0; i--)
{
CheckGravity(i);
}
point.text = "" + point_Count;
moves_text.text = "" + clickCount;
}
void UpdateMousePos(){
//Debug.Log ("UpdateMousePos");
pos_Cam = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos_BOX.x = pos_Cam.x;
pos_BOX.y = pos_Cam.y;
if (Input.GetMouseButtonDown (0) )
{
Debug.Log("mm");
int num = CollisionCheck(pos_BOX);
if (num != -1) {
Debug.Log("num");
RegisterClick (num);
}
}
}
public int CollisionCheck(Vector2 pos){
for (int i=0; i< (colSizeNum*rowSizeNum); i++) {
if(allBox[i].GetComponent<Collider2D>().OverlapPoint(pos))
{
return i;
}
}
return -1;
}
public void RegisterClick(int index)
{
//Debug.Log ("register");
List<int> adjList = GetAllAdjacent (index);
if (adjList.Count >= 3) {
clickCount+=1;
Debug.Log(adjList.Count);
for (int i=0; i<adjList.Count; i++) {
point_Count += adjList.Count;
allBox [adjList [i]].GetComponent<AudioSource> ().Play ();
allBox [adjList [i]].GetComponent<boxController> ().SetDestroyed ();
}
} else {
GetComponent<AudioSource>().PlayOneShot(impact, 0.7F);
}
}
List<int> RecursiveAdjacent (List<int> inList, int index)
{
//Debug.Log ("recursive");
if ((index + 1 < rowSizeNum*colSizeNum) && !inList.Contains(index + 1) && ((int)(index / rowSizeNum) == (int)((index + 1) / rowSizeNum)) && (!allBox[index + 1].GetComponent<boxController> ().GetDestroyed()))
{
if(allBox [index].GetComponent<boxController> ().GetComponent<Renderer>().material.color == allBox[index + 1].GetComponent<boxController> ().GetComponent<Renderer>().material.color)
{
inList.Add(index+1);
inList= RecursiveAdjacent(inList,index+1);
}
}
if ((index - 1 >= 0) && !inList.Contains(index - 1) && ((int)(index / rowSizeNum) == (int)((index - 1) / rowSizeNum)) && (!allBox[index - 1].GetComponent<boxController>().GetDestroyed()))
{
if(allBox[index].GetComponent<boxController>().GetComponent<Renderer>().material.color == allBox [index - 1].GetComponent<boxController> ().GetComponent<Renderer>().material.color)
{
inList.Add(index-1);
inList= RecursiveAdjacent(inList,index-1);
}
}
if ((index + rowSizeNum < rowSizeNum*colSizeNum) && !inList.Contains(index + rowSizeNum) && (!allBox[index + rowSizeNum].GetComponent<boxController>().GetDestroyed()))
{
if (allBox[index].GetComponent<boxController>().GetComponent<Renderer>().material.color == allBox[index + rowSizeNum].GetComponent<boxController>().GetComponent<Renderer>().material.color)
{
inList.Add(index + rowSizeNum);
inList = RecursiveAdjacent(inList, index + rowSizeNum);
}
}
if ((index - rowSizeNum >= 0) && !inList.Contains(index - rowSizeNum) && (!allBox[index - rowSizeNum].GetComponent<boxController>().GetDestroyed()))
{
if (allBox[index].GetComponent<boxController>().GetComponent<Renderer>().material.color == allBox[index - rowSizeNum].GetComponent<boxController>().GetComponent<Renderer>().material.color)
{
inList.Add(index - rowSizeNum);
inList = RecursiveAdjacent(inList, index - rowSizeNum);
}
}
return inList;
}
List<int> GetAllAdjacent (int index)
{
//Debug.Log ("getall");
List<int> adjacents= new List<int>();
adjacents.Add (index);
adjacents = RecursiveAdjacent (adjacents, index);
return adjacents;
}
void CheckGravity(int index )
{
//Debug.Log ("check");
if (!allBox [index].GetComponent<boxController> ().GetDestroyed ()) {
if ((index >= rowSizeNum) ) {
if (allBox [index - rowSizeNum].GetComponent<boxController> ().GetDestroyed ()) {
check -= Time.deltaTime;
if(check<=0)
{
allBox [index - rowSizeNum].GetComponent<boxController> ().GetComponent<Renderer> ().material.color
= allBox [index].GetComponent<boxController> ().GetComponent<Renderer> ().material.color;
check = 0.1f;
GetComponent<AudioSource>().PlayOneShot(gravity, 0.7F);
allBox [index].GetComponent<boxController> ().SetDestroyed ();
allBox [index - rowSizeNum].GetComponent<boxController> ().UndoDestroyed ();
canEmit = true;
}
}
}
}
}
}
Comment