void DebugOut() { Debug.Log("deleted Nodex index = " + nodeIndex); Debug.Log("remaining node indecies: number of indices = " + loop.nodes.Count); string indecies = ""; foreach (Node node in loop.nodes) { indecies = indecies + "," + node.getNodeIndex(); } Debug.Log(indecies + "\n"); GameObject[] nodeInstances = GameObject.FindGameObjectsWithTag("node"); Debug.Log("remaining node instance indecies: number of indices = " + nodeInstances.Length); indecies = ""; foreach (GameObject gameObject in nodeInstances) { if (gameObject == null) { NodeComponent nodeComponent = gameObject.GetComponent <NodeComponent>(); indecies = indecies + "," + nodeComponent.GetNodeIndex() + "(null)"; } else { NodeComponent nodeComponent = gameObject.GetComponent <NodeComponent>(); indecies = indecies + "," + nodeComponent.GetNodeIndex(); } } Debug.Log(indecies + "\n"); Debug.Log("remaining edge indecies: number of indices = " + loop.edges.Length); indecies = ""; foreach (Edge edge in loop.edges) { if (edge == null) { continue; } int leftNodeIndex = edge.GetLeftNodeIndex(); int rightNodeIndex = edge.GetRightNodeIndex(); indecies = indecies + ",(" + leftNodeIndex + "," + rightNodeIndex + ")"; } Debug.Log(indecies + "\n"); }
bool isStillAlive(GameObject inGameObject, Collision2D collision) { NodeComponent nodeComponent = inGameObject.GetComponent <NodeComponent>(); NodeComponent nodeComponentOther = collision.gameObject.GetComponent <NodeComponent>(); if (nodeComponent.GetNodeIndex() != -1 && nodeComponentOther.GetNodeIndex() != -1) { return(true); } else { return(false); } }
// // Entire methods after this line are not necessary for executing program, but just for debugging purpose. // bool checkPrefabNodeHasUniqueNodeIndex() { GameObject[] nodeInstances = GameObject.FindGameObjectsWithTag("node"); for (int i = 0; i < nodeInstances.Length - 1; i++) { for (int j = i + 1; j < nodeInstances.Length; j++) { NodeComponent nodeComponent1 = nodeInstances[i].GetComponent <NodeComponent>(); NodeComponent nodeComponent2 = nodeInstances[j].GetComponent <NodeComponent>(); int nodeIndex1 = nodeComponent1.GetNodeIndex(); int nodeIndex2 = nodeComponent2.GetNodeIndex(); if (nodeIndex1 == nodeIndex2) { return(false); } } } return(true); }
void OnCollisionEnter2D(Collision2D collision) { // check gameobject is still alive. if (false == isStillAlive(gameObject, collision)) { return; } // // if tobeDestroyFlg is set, there is no more process to do. // if (GetToBeDestroyFlg() == true) { DestroyNode(gameObject, 0); // update new edges after destroying the node. loop.IterateEdgeForUpdatePrefab(UpdateEdge); // if (checkPrefabNodeHasUniqueNodeIndex() == false) // { // Application.Quit(); // } return; } // // if both either of node is pre-spawn, collision cannot be executed. // if (canCollide(collision) == false) { return; } // // Combine 2 nodes and tell the other node to be destroyed. // Vector2 middlePos = (collision.transform.position + transform.position) / 2.0f; transform.position = middlePos; NodeComponent otherNode = collision.gameObject.GetComponent <NodeComponent>(); int otherIndex = otherNode.GetNodeIndex(); // debug // int upperBound = loop.edges.GetUpperBound(0); // if (loop.nodes.Count != loop.numOfNodes || (upperBound+1) != loop.numOfDimension) // { // Application.Quit(); // } // if( nodeIndex == (upperBound+1)) // { // Application.Quit(); //} // delete the disappear edge if any. Edge edge = loop.GetEdge(nodeIndex, otherIndex); if (edge != null) { Destroy(edge.GetEdgePrefab(), 0); } // combine 2 nodes.(actually delete both and craete new one.) List <Edge> outEdgesMerge = new List <Edge>(); int newNodeIndex = loop.combineNodes(nodeIndex, otherIndex, middlePos, outEdgesMerge); // Debug.Log("deleted node indecies : " + nodeIndex + "," + otherIndex); // DebugOutNumberOfNodeAndEdge(); // create new one. Node newNode = loop.GetNode(newNodeIndex); Vector2 pos = new Vector2(newNode.GetXPos(), newNode.GetYPos()); GameObject nodePrefab = loopComponent.CreateNode(pos, newNodeIndex, newNode.getNodeValue()); newNode.SetNodePrefab(nodePrefab); // delete disappear edges. for (int i = 0; i < outEdgesMerge.Count; i++) { Destroy(outEdgesMerge[i].GetEdgePrefab(), 0.0f); } // tell the other node that these two nodes area dead. otherNode.SetToBeDestroyFlg(); // destroy myself. DestroyNode(this.gameObject, 0); // destroy the other. DestroyNode(collision.gameObject, 0); }