Example #1
0
    void Awake()
    {
        instance = this;

        size = Config.MAPSIZE * 4;
        node = new Node[size, size];
        for (int loop = 0; loop < size; loop++) {
            for(int loop2 = 0; loop2 < size; loop2++){
                node[loop, loop2] = new Node();
                node[loop, loop2].pos = new Vector2(loop, loop2);
            }
        }

        //배열 번호 - size / 2 == pos

        for (int loop = 1; loop < size - 1; loop++) {
            for(int loop2 = 1; loop2 < size - 1; loop2++){

                for(int loop3 = -1; loop3 < 2; loop3++){
                    for(int loop4 = -1; loop4 < 2; loop4++){
                        if(loop3 == 0 && loop4 == 0)continue;
                        if(Physics2D.Raycast(node[loop, loop2].pos, new Vector2(loop3, loop4), new Vector2(loop3, loop4).magnitude + 0.1f)){
                            node[loop, loop2].walkable[loop3 + 1, loop4 + 1] = false;
                        }
                    }
                }
            }
        }
    }
Example #2
0
    void Start()
    {
        awake = true;
        runFrom = GameObject.Find ("Player");
        map_path = Map_Path.Instance;

        gameObject.AddComponent<CircleCollider2D> ();

        speed = 1f;

        StartCoroutine (State_Idle ());
    }
Example #3
0
    public List<Vector2> FindPath(Vector3 start, Vector3 end)
    {
        map_path = Map_Path.Instance;
        start = new Vector3 (Mathf.RoundToInt (start.x), Mathf.RoundToInt (start.y));
        end = new Vector3 (Mathf.RoundToInt (end.x), Mathf.RoundToInt (end.y));

        PrepareMap_path (new Vector2(start.x, start.y));

        curx = middle;
        cury = middle;

        openList = new List<Node> ();

        int count = detSize * detSize;
        while (count > 0) {
            count--;
            if( curx < 1 || cury < 1 || curx > detSize - 2 || cury > detSize - 2){
                continue;
            }

            for(int loop = -1; loop < 2; loop++){
                for(int loop2 = -1; loop2 < 2; loop2++){
                    if(loop == 0 && loop2 == 0)continue;
                    NodePathSet(curx + loop, loop, cury + loop2, loop2, end);
                }
            }

            if(openList.Count != 0){
                Node smallest = openList[0];
                int smallNum = 0;
                for(int loop = 0; loop < openList.Count; loop++){
                    if(smallest.F > openList[loop].F){
                        smallest = openList[loop];
                        smallNum = loop;
                    }
                }
                openList.RemoveAt(smallNum);
                nodePath[curx, cury].open = false;

                Vector2 toMove = new Vector2();
                toMove = smallest.pos - nodePath[curx, cury].pos;
                curx += (int)toMove.x;
                cury += (int)toMove.y;
            }
            if((int)(nodePath[curx, cury].pos.x) == (int)(end.x) && (int)(nodePath[curx, cury].pos.y) == (int)(end.y)){
                break;
            }
        }

        List<Vector2> destList = new List<Vector2> ();

        count = 0;
        Node tempNode;
        tempNode = nodePath [curx, cury];
        if (tempNode.parent == null)
            return destList;
        while (count < 2000) {
            destList.Add(tempNode.pos);
            tempNode = tempNode.parent;
            if(tempNode.parent == null)break;
            count++;
        }
        if (count > 1999)
            Debug.Log ("something is wrong... RIP... count over 2000");

        return destList;
    }