Beispiel #1
0
    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);
    }