Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        // Set width of edge
        LineRenderer lineRenderer = GetComponent <LineRenderer>();

        Width = InNode.transform.lossyScale.x / 500;
        lineRenderer.widthMultiplier = Width;

        // Set positions of edge based on whether the edge connects a node with itself or the position of the nodes
        LineRendererArrow lineRendererArrow = GetComponent <LineRendererArrow>();

        if (GameObject.ReferenceEquals(InNode, OutNode))
        {
            lineRendererArrow.ArrowOrigin = new Vector2(InNode.transform.position.x + Width, InNode.transform.position.y + 5.0f * Width);
            lineRendererArrow.ArrowTarget = new Vector2(OutNode.transform.position.x + Width, OutNode.transform.position.y + 2.5f * Width);

            LineRenderer loopLineRenderer = this.gameObject.transform.GetChild(0).gameObject.GetComponent <LineRenderer>();
            loopLineRenderer.SetPositions(new Vector3[] {
                new Vector2(InNode.transform.position.x + Width, InNode.transform.position.y + 5.0f * Width)
                , new Vector2(InNode.transform.position.x + 5.0f * Width, InNode.transform.position.y + 5.0f * Width)
                , new Vector2(InNode.transform.position.x + 5.0f * Width, InNode.transform.position.y + Width)
                , new Vector2(InNode.transform.position.x + 2.5f * Width, InNode.transform.position.y + Width)
            });
        }
        else if (Mathf.Abs(InNode.transform.position.x - OutNode.transform.position.x) <= 5.0f * Width)
        {
            if (InNode.transform.position.y < OutNode.transform.position.y)
            {
                lineRendererArrow.ArrowOrigin = new Vector2(InNode.transform.position.x - Width, InNode.transform.position.y + 2.5f * Width);
                lineRendererArrow.ArrowTarget = new Vector2(OutNode.transform.position.x + Width, OutNode.transform.position.y - 2.5f * Width);
            }
            else
            {
                lineRendererArrow.ArrowOrigin = new Vector2(InNode.transform.position.x - Width, InNode.transform.position.y - 2.5f * Width);
                lineRendererArrow.ArrowTarget = new Vector2(OutNode.transform.position.x + Width, OutNode.transform.position.y + 2.5f * Width);
            }
        }
        else if (InNode.transform.position.x < OutNode.transform.position.x)
        {
            lineRendererArrow.ArrowOrigin = new Vector2(InNode.transform.position.x + 2.5f * Width, InNode.transform.position.y + Width);
            lineRendererArrow.ArrowTarget = new Vector2(OutNode.transform.position.x - 2.5f * Width, OutNode.transform.position.y - Width);
        }
        else if (InNode.transform.position.x > OutNode.transform.position.x)
        {
            lineRendererArrow.ArrowOrigin = new Vector2(InNode.transform.position.x - 2.5f * Width, InNode.transform.position.y + Width);
            lineRendererArrow.ArrowTarget = new Vector2(OutNode.transform.position.x + 2.5f * Width, OutNode.transform.position.y - Width);
        }
        lineRendererArrow.UpdateArrow();

        // Make the edge appear on both a normal edge and recurrent edge. The objects are invisible
        // when they are first created and made visible when their position has been changed accordingly
        gameObject.GetComponent <LineRenderer>().enabled = true;
        if (gameObject.transform.childCount > 0)
        {
            gameObject.transform.GetChild(0).GetComponent <LineRenderer>().enabled = true;
        }
    }
Exemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        distanceText.text = "Distance: " + calcDist;

        Vector3    clickPosition = -Vector3.one;
        Ray        ray           = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            clickPosition = hit.point;
        }

        int x = Mathf.FloorToInt(clickPosition.x);
        int y = Mathf.FloorToInt(clickPosition.z);



        if (x > -1 && y > -1)                        //if mouse is within bounds
        {
            if (Input.GetMouseButton(0) && wallMode) //LMB while in wall mode
            {
                fm.WallBlock(x, y);
            }
            else if (Input.GetMouseButton(1) && wallMode) //RMB while in wall mode
            {
                fm.WalkBlock(x, y);
            }
            else if (Input.GetMouseButton(0) && heightMode) //LMB while in height mode
            {
                fm.RaiseBlock(x, y, 0.5f);
            }
            else if (Input.GetMouseButton(1) && heightMode) //RMB while in height mode
            {
                fm.LowerBlock(x, y, 0.5f);
            }
            else if (Input.GetMouseButtonDown(0) && pathMode) //LMB on click while in path mode
            {
                if (fm.floor[x, y].value == 0)
                {
                    arrowBase.SetActive(true);
                    arrowBase.transform.position = new Vector3(x + 0.5f, 0.7f, y + 0.5f);

                    startX = x;
                    startY = y;
                }
            }
            else if (Input.GetMouseButton(0) && pathMode && startX != -1) //dragging LMB
            {
                if ((x != startX || y != startY))                         // off start node
                {
                    arrowBody.gameObject.SetActive(true);

                    arrowBody.ArrowOrigin = new Vector3(startX + 0.5f, 0.7f, startY + 0.5f);
                    arrowBody.ArrowTarget = new Vector3(x + 0.5f, 0.7f, y + 0.5f);
                    arrowBody.UpdateArrow();
                }
                else //still on start node
                {
                    arrowBody.gameObject.SetActive(false);
                }

                if (fm.floor[x, y].value != 0) //if hovering over wall
                {
                    arrowBody.GetComponent <Renderer>().material.color = lightRed;
                    arrowBase.GetComponent <Renderer>().material.color = lightRed;
                }
                else
                {
                    arrowBody.GetComponent <Renderer>().material.color = red;
                    arrowBase.GetComponent <Renderer>().material.color = red;
                }


                if (Input.GetMouseButtonDown(1)) //press RMB while holding LMB will cancel
                {
                    arrowBase.SetActive(false);
                    arrowBody.gameObject.SetActive(false);

                    startX = -1;
                    startY = -1;
                }
            }
            else if (Input.GetMouseButtonUp(0) && pathMode && startX >= 0) //released LMB
            {
                arrowBase.SetActive(false);
                arrowBody.gameObject.SetActive(false);
                endX = x;
                endY = y;

                //check bounds for inputs
                if (fm.floor[endX, endY].value == 0)
                {
                    Debug.Log("Path from [" + startX + ", " + startY + "] to [" + endX + ", " + endY + "] using algorithm " + algorithm);
                    if (algorithm == 0) //Floodfill
                    {
                        fm.FloodFill(startX, startY, endX, endY);
                    }
                    else if (algorithm == 1) //Floodfill3D
                    {
                        fm.FloodFill3D(startX, startY, endX, endY);
                    }
                    else if (algorithm == 2) //Greedy
                    {
                        fm.Greedy(startX, startY, endX, endY);
                    }
                    else if (algorithm == 3) //AStar
                    {
                        fm.AStar(startX, startY, endX, endY);
                    }
                }
                startX = -1;
                startY = -1;
            }
        }
        else if (Input.GetMouseButton(0) && pathMode) // holding LMB while not on map
        {
            arrowBody.GetComponent <Renderer>().material.color = lightRed;
            arrowBase.GetComponent <Renderer>().material.color = lightRed;
        }
        else if (Input.GetMouseButtonUp(0) && pathMode) //let go of LMB while not on map
        {
            arrowBase.SetActive(false);
            arrowBody.gameObject.SetActive(false);

            startX = -1;
            startY = -1;
        }
        else
        {
        }
    }