void PopulateGrid() { int _numOf_Coordinates = LevelXML.GetNodeList("LevelsContainer>0>Coordinates>0>Level" + GamePrefs.CURRENT_LEVEL + ">0>value").Count; for (int i = 0; i < _numOf_Coordinates; i++) { string _attribute_x_ValueString = LevelXML.GetValue("LevelsContainer>0>Coordinates>0>Level" + GamePrefs.CURRENT_LEVEL + ">0>value>" + i + ">@attribute_i"); int _attribute_x_ValueInt; int.TryParse(_attribute_x_ValueString, out _attribute_x_ValueInt); string _attribute_y_ValueString = LevelXML.GetValue("LevelsContainer>0>Coordinates>0>Level" + GamePrefs.CURRENT_LEVEL + ">0>value>" + i + ">@attribute_j"); int _attribute_y_ValueInt; int.TryParse(_attribute_y_ValueString, out _attribute_y_ValueInt); BubbleProperties b_property = GameManager.FindObjectOfType <GameManager>().MatrixGrid[_attribute_x_ValueInt][_attribute_y_ValueInt].GetComponent <BubbleProperties>(); GameManager.FindObjectOfType <GameManager>().MatrixGrid[_attribute_x_ValueInt][_attribute_y_ValueInt].GetComponent <BubbleProperties>().isEmpty = false; switch (LevelXML.GetValue("LevelsContainer>0>Coordinates>0>Level" + GamePrefs.CURRENT_LEVEL + ">0>value>" + i + ">@attribute_color")) { case "Green": b_property.BubbleColor = ColorProperty.Green; break; case "Yellow": b_property.BubbleColor = ColorProperty.Yellow; break; case "Blue": b_property.BubbleColor = ColorProperty.Blue; break; case "Red": b_property.BubbleColor = ColorProperty.Red; break; case "Purple": b_property.BubbleColor = ColorProperty.Purple; break; case "Black": b_property.BubbleColor = ColorProperty.Black; break; } } }
void OnCollisionEnter2D(Collision2D other) { if (!atRest) { if (other.gameObject.tag == "TopBorder" || other.gameObject.tag == "BubbleTag") { this.gameObject.GetComponent <Rigidbody2D>().velocity = Vector3.zero; this.gameObject.GetComponent <Rigidbody2D>().isKinematic = true; this.gameObject.GetComponent <Collider2D>().isTrigger = true; if (other.gameObject.tag == "TopBorder") { nearestDistance = new Dictionary <float, Transform>(); List <float> ForSort = new List <float>(); for (int i = 0; i < 10; i++) { if (gameManager.MatrixGrid[0][i].transform.gameObject.GetComponent <BubbleProperties>().isEmpty) { nearestDistance.Add(Vector3.Distance(gameManager.MatrixGrid[0][i].position, this.transform.position), gameManager.MatrixGrid[0][i]); ForSort.Add(Vector3.Distance(gameManager.MatrixGrid[0][i].position, this.transform.position)); } } ForSort.Sort(); this.transform.position = nearestDistance[ForSort[0]].position; this.GetComponent <CircleCollider2D>().enabled = false; FuseInGrid(ForSort[0]); } if (other.gameObject.tag == "BubbleTag") { BubbleProperties currentBubble = other.gameObject.GetComponent <BubbleProperties>(); nearestDistance = new Dictionary <float, Transform>(); List <float> ForSort = new List <float>(); try { if (currentBubble.Top_Left.GetComponent <BubbleProperties>().isEmpty) { nearestDistance.Add(Vector3.Distance(currentBubble.Top_Left.position, this.transform.position), currentBubble.Top_Left); ForSort.Add(Vector3.Distance(currentBubble.Top_Left.position, this.transform.position)); } } catch (System.Exception e) { } try { if (currentBubble.Top_Right.GetComponent <BubbleProperties>().isEmpty) { nearestDistance.Add(Vector3.Distance(currentBubble.Top_Right.position, this.transform.position), currentBubble.Top_Right); ForSort.Add(Vector3.Distance(currentBubble.Top_Right.position, this.transform.position)); } } catch (System.Exception e) { } try { if (currentBubble.Left.GetComponent <BubbleProperties>().isEmpty) { nearestDistance.Add(Vector3.Distance(currentBubble.Left.position, this.transform.position), currentBubble.Left); ForSort.Add(Vector3.Distance(currentBubble.Left.position, this.transform.position)); } } catch (System.Exception e) { } try { if (currentBubble.Right.GetComponent <BubbleProperties>().isEmpty) { nearestDistance.Add(Vector3.Distance(currentBubble.Right.position, this.transform.position), currentBubble.Right); ForSort.Add(Vector3.Distance(currentBubble.Right.position, this.transform.position)); } } catch (System.Exception e) { } try { if (currentBubble.Bottom_Left.GetComponent <BubbleProperties>().isEmpty) { nearestDistance.Add(Vector3.Distance(currentBubble.Bottom_Left.position, this.transform.position), currentBubble.Bottom_Left); ForSort.Add(Vector3.Distance(currentBubble.Bottom_Left.position, this.transform.position)); } } catch (System.Exception e) { } try { if (currentBubble.Bottom_Right.GetComponent <BubbleProperties>().isEmpty) { nearestDistance.Add(Vector3.Distance(currentBubble.Bottom_Right.position, this.transform.position), currentBubble.Bottom_Right); ForSort.Add(Vector3.Distance(currentBubble.Bottom_Right.position, this.transform.position)); } } catch (System.Exception e) { } ForSort.Sort(); if (ForSort[0] != null) { this.transform.position = nearestDistance[ForSort[0]].position; this.GetComponent <CircleCollider2D>().enabled = false; FuseInGrid(ForSort[0]); } } atRest = true; gameManager.OnGridPlacement(); } } }
void FindConnectedNodes(Transform operationalNode) { BubbleProperties b_property = operationalNode.GetComponent <BubbleProperties>(); if (!b_property.isEmpty && !(connectedStack.Contains(operationalNode))) { connectedStack.Push(operationalNode); // Push this node in stack as non empty connected node connected_Flag_Cell[b_property.i][b_property.j] = true; try { if (!connectedStack.Contains(b_property.Top_Left) && b_property.Top_Left != null && !b_property.Top_Left.GetComponent <BubbleProperties>().isEmpty) { FindConnectedNodes(b_property.Top_Left); } } catch (System.Exception e) { } try { if (!connectedStack.Contains(b_property.Top_Right) && b_property.Top_Right != null && !b_property.Top_Right.GetComponent <BubbleProperties>().isEmpty) { FindConnectedNodes(b_property.Top_Right); } } catch (System.Exception e) { } try { if (!connectedStack.Contains(b_property.Left) && b_property.Left != null && !b_property.Left.GetComponent <BubbleProperties>().isEmpty) { FindConnectedNodes(b_property.Left); } } catch (System.Exception e) { } try { if (!connectedStack.Contains(b_property.Right) && b_property.Right != null && !b_property.Right.GetComponent <BubbleProperties>().isEmpty) { FindConnectedNodes(b_property.Right); } } catch (System.Exception e) { } try { if (!connectedStack.Contains(b_property.Bottom_Left) && b_property.Bottom_Left != null && !b_property.Bottom_Left.GetComponent <BubbleProperties>().isEmpty) { FindConnectedNodes(b_property.Bottom_Left); } } catch (System.Exception e) { } try { if (!connectedStack.Contains(b_property.Bottom_Right) && b_property.Bottom_Right != null && !b_property.Bottom_Right.GetComponent <BubbleProperties>().isEmpty) { FindConnectedNodes(b_property.Bottom_Right); } } catch (System.Exception e) { } } return; }