コード例 #1
0
    public void nameTBD(Vector3 begin, Vector3 finish)
    {
        drawnStrand = drawStrandBetween(begin, finish, true);

        int strandIndex;

        string[] splitName = drawnStrand.name.Split(new string[] { "d" }, StringSplitOptions.RemoveEmptyEntries);
        // foreach(string s in splitName){
        //  Debug.Log(s);
        // }
        strandIndex = Int32.Parse(splitName[1]) + 3;
        // Debug.Log(string.Format("strandList.Count = {0}, strandIndex = {1}", strandList.Count, strandIndex));

        if (strandIndex > strandList.Count)
        {
            return;
        }
        Strand addedStrand = strandList[strandIndex];

        Strand            startStrand, endStrand;
        bool              startStrandValid = StrandListContainsPoint(begin, out startStrand);
        bool              endStrandValid = StrandListContainsPoint(finish, out endStrand);
        List <GameObject> intersectNodes = new List <GameObject>(), splitStrands = null;

        bool existingStartNodeCopy = existingStartNode, existingEndNodeCopy = existingEndNode;

        Vector3 intersectPt = Vector3.zero;

        // bool drawnStrandIsSplit = false;
        for (int i = strandList.Count - 1; i >= 0; i--)
        {
            Strand str = strandList[i];
            // Skipping self
            if (str.Equals(addedStrand))
            {
                continue;
            }
            ;
            if (str.Equals(startStrand) && !existingStartNodeCopy)
            {
                splitStrands = SplitStrand(str, new Node[] { addedStrand.GetStartNode() });
            }
            if (str.Equals(endStrand) && !existingEndNodeCopy)
            {
                splitStrands = SplitStrand(str, new Node[] { addedStrand.GetEndNode() });
            }
            if (addedStrand.Intersects(str, out intersectPt))
            {
                splitStrands = SplitStrand(str, new Node[] { new Node(intersectPt.x, intersectPt.y) });
                GameObject newNode = Instantiate(nodePrefab, intersectPt, Quaternion.identity) as GameObject;
                // newNode.GetComponent<Renderer>().enabled = false;
                intersectNodes.Add(newNode);

                newNode.name = string.Format("Node{0}", nodeList.Count - 3);
                nodeList.Add(new Node(intersectPt.x, intersectPt.y));
            }
        }

        if (intersectNodes.Count > 0)
        {
            // drawnStrandIsSplit = true;
            Node[] iNodes = new Node[intersectNodes.Count];
            // Debug.Log(string.Format("{0} intersection points", iNodes.Length));
            for (int j = 0; j < intersectNodes.Count; j++)
            {
                GameObject goTemp = intersectNodes[j];

                iNodes[j] = new Node(goTemp.transform.position.x, goTemp.transform.position.y);
            }
            // string debugString = "Split strands are: ";
            splitStrands = SplitStrand(addedStrand, iNodes, false);
            // foreach(var split in splitStrands){
            //  debugString +=  string.Format("{0} at {1}", split.name, split.transform.position.ToString());
            // }
            // Debug.Log(debugString);

            // string dString = "";
            // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) {
            //  dString += string.Format("{0}, ", g.name);
            // }
            // Debug.Log(dString);
            // Destroy(drawnStrand);
        }
        // Debug.Log("after destroying original");
        // string debugString = "";
        // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) {
        //  debugString += string.Format("{0}, ", g.name);
        // }
        // Debug.Log(debugString);

        drawnStrand = null;
        UpdateAdjacencyMatrix();
    }
コード例 #2
0
ファイル: Game.cs プロジェクト: vak1793/Spider-Lunch
    // Update is called once per frame
    void Update()
    {
        return;

        Vector3 mPos;
        Ray     ray;
        float   ray_distance;

        if (Input.GetMouseButtonDown(0))
        {
            if (playerIsMoving)
            {
                return;
            }

            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (plane.Raycast(ray, out ray_distance))
            {
                mPos = ray.GetPoint(ray_distance);
                mPos = new Vector3((float)System.Math.Round(mPos.x, 3), (float)System.Math.Round(mPos.y, 3), 0);
                // Debug.Log(string.Format("Clicked at ({0},{1})", mPos.x, mPos.y));
                Strand clickedStrand           = null;
                bool   clickedOnExistingStrand = StrandListContainsPoint(mPos, out clickedStrand);
                bool   clickedOnExistingNode   = GetOverlappingObjects(out node1, "Vertex", mPos, 0.1f);

                // Spider spiderScript = spider.GetComponent<Spider>();

                if (clickedOnExistingNode)
                {
                    // Debug.Log(string.Format("Clicked on existing node: {0}", node1.name));
                    existingNode = true;
                }
                else if (clickedOnExistingStrand)
                {
                    startStrand = clickedStrand;
                    // Debug.Log(string.Format("Clicked on {0}", clickedStrand.PositionString()));
                    //Debug.Log(string.Format("mPos = {0}", mPos.ToString()));
                    Vector3 pos = clickedStrand.ClosestPoint(mPos);
                    // Debug.Log(string.Format("Closest point to mPos on strand: {0}", pos.ToString()));
                    // Debug.Log(
                    //   string.Format(
                    //     "Approximated {0} as {1} on {2}",
                    //     mPos.ToString(),
                    //     pos.ToString(),
                    //     clickedStrand.PositionString()
                    //   )
                    // );
                    // spiderScript.SetPositionToMove(pos);
                    node1      = Instantiate(node, pos, Quaternion.identity) as GameObject;
                    node1.name = string.Format("Node{0}", (nodeList.Count - 4));

                    // node1.SetActive(false);
                    node1.GetComponent <Renderer>().enabled = false;
                    // Node newNode = new Node(pos.x, pos.y);
                    // nodeList.Add(newNode);
                    existingNode = false;
                    // Debug.Log("Created new node at: " + newNode.PositionString());
                }
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (plane.Raycast(ray, out ray_distance))
            {
                mPos = ray.GetPoint(ray_distance);
                mPos = new Vector3((float)System.Math.Round(mPos.x, 3), (float)System.Math.Round(mPos.y, 3), 0);

                // Debug.Log(string.Format("node1 at: ({0},{1})", node1.transform.position.x, node1.transform.position.y));
                if (node1)
                {
                    positionToMove = node1.transform.position;
                    CalculatePlayerMovePath(player.transform.position, positionToMove);
                    Renderer nodeRenderer = node1.GetComponent <Renderer>();
                    Bounds   nodeBounds   = nodeRenderer.bounds;

                    if (!nodeBounds.Contains(mPos))
                    {
                        float startX, startY, endX, endY;

                        Strand clickedStrand           = null;
                        bool   clickedOnExistingStrand = StrandListContainsPoint(mPos, out clickedStrand);
                        if (clickedOnExistingStrand)
                        {
                            nodeList.Add(new Node(node1.transform.position.x, node1.transform.position.y));
                            bool clickedOnExistingNode = GetOverlappingObjects(out node2, "Vertex", mPos, 0.1f);
                            if (!clickedOnExistingNode)
                            {
                                endStrand = clickedStrand;
                                //Debug.Log(string.Format("mPos = {0}", mPos.ToString()));
                                Vector3 pos = clickedStrand.ClosestPoint(mPos);
                                //Debug.Log(string.Format("Closest point to mPos on strand: {0}", pos.ToString()));
                                node2      = Instantiate(node, pos, Quaternion.identity) as GameObject;
                                node2.name = string.Format("Node{0}", (nodeList.Count - 4));
                                // node2.SetActive(false);
                                node2.GetComponent <Renderer>().enabled = false;
                                Node newNode = new Node(pos.x, pos.y);
                                nodeList.Add(newNode);
                                // Debug.Log("Created new node at: " + newNode.PositionString());
                            }

                            startX = node1.transform.position.x;
                            startY = node1.transform.position.y;
                            endX   = node2.transform.position.x;
                            endY   = node2.transform.position.y;

                            float lineDistance = Mathf.Sqrt(Mathf.Pow(endX - startX, 2) + Mathf.Pow(endY - startY, 2));
                            float lineAngle    = Mathf.Atan2(endY - startY, endX - startX) * (180 / Mathf.PI);
                            float spriteSize   = strand.GetComponent <SpriteRenderer>().bounds.size.x;
                            float scale        = lineDistance / spriteSize;

                            if (node2)
                            {
                                Node   n1          = new Node(startX, startY);
                                Node   n2          = new Node(endX, endY);
                                Strand strandToAdd = new Strand(n1, n2);

                                bool strandPresent = false;
                                foreach (var str in strandList)
                                {
                                    if (strandToAdd.Equals(str))
                                    {
                                        strandPresent = true;
                                    }
                                }

                                if (!strandPresent)
                                {
                                    drawnStrand = Instantiate(
                                        strand,
                                        node1.transform.position,
                                        Quaternion.AngleAxis(lineAngle, Vector3.forward)
                                        ) as GameObject;

                                    // Debug.Log(strandToAdd.PositionString());
                                    strandList.Add(strandToAdd);
                                    drawnStrand.name = string.Format("Strand{0}", (strandList.Count - 4));
                                    drawnStrand.GetComponent <Renderer>().enabled = false;
                                    // drawnStrand.SetActive(false);

                                    Vector3 originalVector = drawnStrand.transform.localScale;
                                    drawnStrand.transform.localScale = new Vector3(
                                        scale,
                                        drawnStrand.transform.localScale.y,
                                        drawnStrand.transform.localScale.z
                                        );

                                    Vector3 intersectPt = Vector3.zero;
                                    drawnStrandIsSplit = false;
                                    for (int i = strandList.Count - 1; i >= 0; i--)
                                    {
                                        Strand str = strandList[i];
                                        if (str.Equals(strandToAdd))
                                        {
                                            // Skipping self
                                            continue;
                                        }
                                        ;
                                        if (str.Equals(startStrand) && !existingNode)
                                        {
                                            splitStrands = SplitStrand(str, new Node[] { new Node(startX, startY) });
                                        }
                                        if (str.Equals(endStrand) && !clickedOnExistingNode)
                                        {
                                            splitStrands = SplitStrand(str, new Node[] { new Node(endX, endY) });
                                        }
                                        if (strandToAdd.Intersects(str, out intersectPt))
                                        {
                                            splitStrands = SplitStrand(str, new Node[] { new Node(intersectPt.x, intersectPt.y) });
                                            GameObject newNode = Instantiate(node, intersectPt, Quaternion.identity) as GameObject;
                                            newNode.GetComponent <Renderer>().enabled = false;
                                            intersectNodes.Add(newNode);
                                            newNode.name = string.Format("Node{0}", (nodeList.Count - 4));
                                            nodeList.Add(new Node(intersectPt.x, intersectPt.y));
                                        }
                                    }
                                    if (intersectNodes.Count > 0)
                                    {
                                        drawnStrandIsSplit = true;
                                        Node[] iNodes = new Node[intersectNodes.Count];
                                        // Debug.Log(string.Format("{0} intersection points", iNodes.Length));
                                        for (int j = 0; j < intersectNodes.Count; j++)
                                        {
                                            GameObject goTemp = intersectNodes[j];

                                            iNodes[j] = new Node(goTemp.transform.position.x, goTemp.transform.position.y);
                                        }
                                        splitStrands = SplitStrand(strandToAdd, iNodes, true);
                                        Destroy(drawnStrand);
                                    }

                                    newStrandDrawn = false;
                                }
                            }
                        }
                        else
                        {
                            // Debug.Log("Did not click on an existing strand for node2");
                            DeleteFirstNode();
                        }
                    }
                    else
                    {
                        DeleteFirstNode();
                    }
                }

                // node1 = null;
                // node2 = null;
            }

            //ShowNodeList();
            //ShowStrandList();
        }

        if (Input.GetMouseButton(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (node1)
            {
                // TODO: add intermediate animation
            }
        }

        if (player.transform.position != positionToMove)
        {
            // Debug.Log(string.Format("Player at {0}", player.transform.position.ToString()));
            // Debug.Log(string.Format("Destination = {0}", positionToMove.ToString()));
            // Debug.Log("player is moving");
            // Vector3 directionVector;
            if (playerMovePath.Count > 0)
            {
                playerIsMoving = true;
                Node nodeToMove = playerMovePath[0];
                nextNode = new Vector3(nodeToMove.xPos(), nodeToMove.yPos(), 0);
                // directionVector = nextNode - player.transform.position;
                // player.transform.position += Vector3.Normalize(directionVector) * (Time.deltaTime * playerMoveSpeed);
                // player.transform.position = Vector3.Lerp(player.transform.position, nextNode, 0.1f);
                player.transform.position = Vector3.MoveTowards(player.transform.position, nextNode, (Time.deltaTime * playerMoveSpeed));

                if ((player.transform.position - nextNode).magnitude < 0.2)
                {
                    playerMovePath.RemoveAt(0);
                }
                // if(player.transform.position == nextNode){
                //   playerMovePath.RemoveAt(0);
                // }
            }
            else
            {
                player.transform.position = Vector3.MoveTowards(player.transform.position, positionToMove, (Time.deltaTime * playerMoveSpeed));
                // directionVector = positionToMove - player.transform.position;
                // player.transform.position += Vector3.Normalize(directionVector) * (Time.deltaTime * playerMoveSpeed);
                // player.transform.position = Vector3.Lerp(player.transform.position, positionToMove, 0.1f);

                if ((player.transform.position - positionToMove).magnitude < 0.2)
                {
                    playerIsMoving = false;
                }
                else
                {
                    playerIsMoving = true;
                }
            }
        }
        else
        {
            playerIsMoving = false;
        }

        if (!playerIsMoving && !newStrandDrawn)
        {
            // Debug.Log("player has reached clicked point, time to draw strand");
            if (!node1 || !node2)
            {
                return;
            }
            // node1.SetActive(true);
            node1.GetComponent <Renderer>().enabled = true;
            // node2.SetActive(true);
            node2.GetComponent <Renderer>().enabled = true;
            // drawnStrand.SetActive(true);
            if (!drawnStrandIsSplit)
            {
                // Debug.Log("Strand with no intersection");
                drawnStrand.GetComponent <Renderer>().enabled = true;
            }
            else
            {
                // Debug.Log(string.Format("Strand split into {0}", splitStrands.Count));
                foreach (GameObject go in splitStrands)
                {
                    go.GetComponent <Renderer>().enabled = true;
                }
            }
            foreach (GameObject go in intersectNodes)
            {
                go.GetComponent <Renderer>().enabled = true;
            }

            // Vector3 insectSpawnPos = (node1.transform.position + node2.transform.position) / 2f;
            // insectSpawnPos[2] = -11;
            //
            // Instantiate(smallInsect, insectSpawnPos, Quaternion.identity);
            UpdateAdjacencyMatrix();
            // DisplayAdjacencyMatrix();
            newStrandDrawn            = true;
            player.transform.position = node1.transform.position;

            // reset nodes and strands for next click
            node1       = null;
            node2       = null;
            drawnStrand = null;
            intersectNodes.Clear();
            splitStrands.Clear();
        }
    }