Esempio n. 1
0
 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.W) && currentLoc.n != null)
     {
         currentLoc = currentLoc.n;
         setLoc();
     }
     if (Input.GetKeyDown(KeyCode.D) && currentLoc.e != null)
     {
         currentLoc = currentLoc.e;
         setLoc();
     }
     if (Input.GetKeyDown(KeyCode.S) && currentLoc.s != null)
     {
         currentLoc = currentLoc.s;
         setLoc();
     }
     if (Input.GetKeyDown(KeyCode.A) && currentLoc.w != null)
     {
         currentLoc = currentLoc.w;
         setLoc();
     }
     if (Input.GetKeyDown(KeyCode.Space))
     {
         StartCoroutine("walk");
     }
     if (Input.GetMouseButtonDown(0))
     {
         pathfind();
     }
 }
Esempio n. 2
0
    IEnumerator walk()
    {
        int i = 0;

        while (path.Count > i)
        {
            yield return(new WaitForSeconds(1));

            currentLoc = path[i];
            setLoc();
            i++;
        }
        path.Clear();
        Debug.Log("Finished");
    }
Esempio n. 3
0
    public int distanceFromTarget(gridObject target)
    {
        int dist = 0;

        if (this.x > target.x)
        {
            dist += this.x - target.x;
        }
        else
        {
            dist += target.x - this.x;
        }

        if (this.z > target.z)
        {
            dist += this.z - target.z;
        }
        else
        {
            dist += target.z - this.z;
        }

        return(dist);
    }
Esempio n. 4
0
    void pathfind()
    {
        path.Clear();
        closedList.Clear();
        openList.Clear();
        targetLoc = getTarget();

        if (currentLoc == targetLoc)
        {
            Debug.Log("Found path, already finished");
            return;
        }

        gridObject selected = currentLoc;

        int loop = 0;

        openList.Clear();
        openList.Add(selected);
        closedList.Add(selected);

        while ((selected != targetLoc) && loop < 1000)
        {
            if (selected.n != null && !closedList.Contains(selected.n))
            {
                openList.Add(selected.n);
            }
            if (selected.s != null && !closedList.Contains(selected.s))
            {
                openList.Add(selected.s);
            }
            if (selected.e != null && !closedList.Contains(selected.e))
            {
                openList.Add(selected.e);
            }
            if (selected.w != null && !closedList.Contains(selected.w))
            {
                openList.Add(selected.w);
            }
            gridObject best = openList [0];
            for (int i = 0; i < openList.Count; i++)
            {
                if (openList [i].distanceFromTarget(targetLoc) < best.distanceFromTarget(targetLoc))
                {
                    best = openList [i];
                }
            }
            openList.Remove(best);
            closedList.Add(best);

            selected = best;
            loop++;
        }
        path.Clear();

        closedList.Reverse();

        path.Add(closedList [0]);
        bool       pathFound = false;
        gridObject startLoc  = currentLoc;
        int        breaker   = 0;

        while (pathFound == false && breaker < 100)
        {
            breaker++;
            for (int i = (closedList.Count - 1); i >= 0; i--)
            {
                if (selected.n == closedList [i] || selected.s == closedList [i] || selected.e == closedList [i] || selected.w == closedList [i])
                {
                    path.Add(closedList [i]);
                    selected = closedList [i];
                    Debug.Log(selected.gameObject.name);
                    if (closedList [i] == startLoc)
                    {
                        Debug.Log("Hit");
                        path.Add(closedList [i]);
                        pathFound = true;
                    }
                    break;
                }
            }
        }
        path.Reverse();
    }
Esempio n. 5
0
    void pathfind()
    {
        path.Clear();
        closedList.Clear();
        openList.Clear();
        targetLoc = getTarget();

        if (currentLoc == targetLoc)
        {
            Debug.Log("Found path, already finished");
            return;
        }

        gridObject selected = currentLoc;

        int loop = 0;

        openList.Clear();
        openList.Add(selected);

        while ((selected != targetLoc) && loop < 1000)
        {
            if (selected.n != null && !closedList.Contains(selected.n))
            {
                openList.Add(selected.n);
            }
            if (selected.s != null && !closedList.Contains(selected.s))
            {
                openList.Add(selected.s);
            }
            if (selected.e != null && !closedList.Contains(selected.e))
            {
                openList.Add(selected.e);
            }
            if (selected.w != null && !closedList.Contains(selected.w))
            {
                openList.Add(selected.w);
            }
            gridObject best = openList [0];
            for (int i = 0; i < openList.Count; i++)
            {
                if (openList [i].distanceFromTarget(targetLoc) < best.distanceFromTarget(targetLoc))
                {
                    best = openList [i];
                }
            }
            openList.Remove(best);
            closedList.Add(best);

            selected = best;
            loop++;
        }
        path.Clear();

        closedList.Reverse();

        Debug.Log(closedList.Capacity);

        path.Add(closedList [0]);
        for (int i = 1; i < closedList.Count; i++)
        {
            Debug.Log(i);
            if (closedList[i] == targetLoc)
            {
                path.Add(closedList[i]);
                break;
            }

            if (selected.n == closedList[i])
            {
                path.Add(closedList[i]);
                selected = closedList[i];
                Debug.Log(closedList[i].gameObject.name);
            }
            else if (selected.s == closedList[i])
            {
                path.Add(closedList[i]);
                selected = closedList[i];
                Debug.Log(closedList[i].gameObject.name);
            }
            else if (selected.e == closedList[i])
            {
                path.Add(closedList[i]);
                selected = closedList[i];
                Debug.Log(closedList[i].gameObject.name);
            }
            else if (selected.w == closedList[i])
            {
                path.Add(closedList[i]);
                selected = closedList[i];
                Debug.Log(closedList[i].gameObject.name);
            }
        }

        path.Reverse();
    }
Esempio n. 6
0
    void connect()
    {
        /*
         * RaycastHit hitN;
         * RaycastHit hitS;
         * RaycastHit hitE;
         * RaycastHit hitW;
         *
         * string derp = hitN.collider.gameObject.name + "']";
         *
         * Physics.Raycast (gameObject.transform.position, Vector3.forward, out hitN, 1f);
         * if (hitN.collider != null && hitN.collider.gameObject.tag == "Nav") {
         *              hitN.collider.GetComponent<gridObject>().s = this;
         *              n = hitN.collider.GetComponent<gridObject>();
         * }
         * Physics.Raycast (gameObject.transform.position, Vector3.back, out hitS, 1f);
         * if (hitS.collider != null && hitS.collider.gameObject.tag == "Nav") {
         *              hitS.collider.GetComponent<gridObject>().n = this;
         *              s = hitS.collider.GetComponent<gridObject>();
         * }
         * Physics.Raycast (gameObject.transform.position, Vector3.left, out hitW, 1f);
         * if (hitW.collider != null && hitW.collider.gameObject.tag == "Nav") {
         *              hitW.collider.GetComponent<gridObject>().e = this;
         *              w = hitW.collider.GetComponent<gridObject>();
         * }
         * Physics.Raycast (gameObject.transform.position, Vector3.right, out hitE, 1f);
         * if (hitE.collider != null && hitE.collider.gameObject.tag == "Nav") {
         *              hitE.collider.GetComponent<gridObject>().w = this;
         *              e = hitE.collider.GetComponent<gridObject>();
         * }
         */

        GameObject node = GameObject.Find(x + " " + y + " " + (z + 1));

        if (node != null)
        {
            node.GetComponent <gridObject> ().s = this;
            n = node.GetComponent <gridObject> ();
        }
        else
        {
            n = null;
        }
        node = GameObject.Find(x + " " + y + " " + (z - 1));
        if (node != null)
        {
            node.GetComponent <gridObject>().n = this;
            s = node.GetComponent <gridObject>();
        }
        else
        {
            s = null;
        }
        node = GameObject.Find((x - 1) + " " + y + " " + z);
        if (node != null)
        {
            node.GetComponent <gridObject>().e = this;
            w = node.GetComponent <gridObject>();
        }
        else
        {
            w = null;
        }
        node = GameObject.Find((x + 1) + " " + y + " " + z);
        if (node != null)
        {
            node.GetComponent <gridObject>().w = this;
            e = node.GetComponent <gridObject>();
        }
        else
        {
            e = null;
        }
    }