Example #1
0
    void GetCost(tileMap.Node A, tileMap.Node B, int step)
    {
        int distance = Mathf.Abs(B.x - A.x) + Mathf.Abs(B.y - A.y);
        int cost     = step + distance;

        if (cost <= node.map [A.x, A.y].cost)
        {
            node.map [A.x, A.y].cost = cost;
            node.map [A.x, A.y].step = step;
        }
    }
Example #2
0
 void RotateSelf(tileMap.Node A, tileMap.Node B)
 {
     if (B.x > A.x)
     {
         rotate = 270f;                              //right
     }
     else if (B.x < A.x)
     {
         rotate = 90f;                               //left
     }
     else if (B.y > A.y)
     {
         rotate = 0f;                                //up
     }
     else if (B.y < A.y)
     {
         rotate = 180f;                              //down
     }
     transform.eulerAngles = new Vector3(0, 0, rotate);
 }
Example #3
0
    void GetPath(tileMap.Node A, tileMap.Node B)
    {
        ResetNode();

        int  cost, up, down, right, left, x, y, step = 0;
        bool check;

        tileMap.Node current, child;

        open.Add(A);
        current = open [step];
        GetCost(current, B, step);

        for (;;)
        {
            if (open.Count <= 0)               //path tidak ditemukan
            {
                if (enemy && enemy.anim)
                {
                    enemy.anim.SetBool("isMove", false);
                }
                break;
            }
            else if (Equals(current, B))               //path ditemukan
            {
                if (enemy && enemy.anim)
                {
                    enemy.anim.SetBool("isMove", true);
                }
                while (!Equals(current, A))
                {
                    path.Add(current);
                    current = node.map [current.x, current.y].parent;
                }
                break;
            }

            cost = 9999;
            for (int i = 0; i < open.Count; i++)
            {
                if (node.map [open [i].x, open [i].y].cost < cost)
                {
                    cost    = node.map [open [i].x, open [i].y].cost;
                    current = open [i];
                }
            }

            open.Remove(current);
            close.Add(current);

            up    = current.y + 1;
            down  = current.y - 1;
            right = current.x + 1;
            left  = current.x - 1;
            x     = current.x;
            y     = current.y;

            if (up < node.mapHeight && node.map [x, up].isOpen)               //up path
            {
                child.x = x;
                child.y = up;
                check   = false;
                if (!close.Contains(child))
                {
                    if (!open.Contains(child))
                    {
                        check = true;
                        open.Add(child);
                    }
                    else if (node.map [x, y].step < node.map [x, up].step)
                    {
                        check = true;
                    }

                    if (check)
                    {
                        node.map [x, up].parent = current;
                        step = node.map [x, y].step + 1;
                        GetCost(child, B, step);
                    }
                }
            }

            if (down >= 0 && node.map [x, down].isOpen)                //bottom paths
            {
                child.x = x;
                child.y = down;
                check   = false;
                if (!close.Contains(child))
                {
                    if (!open.Contains(child))
                    {
                        check = true;
                        open.Add(child);
                    }
                    else if (node.map [x, y].step < node.map [x, down].step)
                    {
                        check = true;
                    }

                    if (check)
                    {
                        node.map [x, down].parent = current;
                        step = node.map [x, y].step + 1;
                        GetCost(child, B, step);
                    }
                }
            }

            if (right < node.mapWidth && node.map [right, y].isOpen)              //right path
            {
                child.x = right;
                child.y = y;
                check   = false;
                if (!close.Contains(child))
                {
                    if (!open.Contains(child))
                    {
                        check = true;
                        open.Add(child);
                    }
                    else if (node.map [x, y].step < node.map [right, y].step)
                    {
                        check = true;
                    }

                    if (check)
                    {
                        node.map [right, y].parent = current;
                        step = node.map [x, y].step + 1;
                        GetCost(child, B, step);
                    }
                }
            }

            if (left >= 0 && node.map [left, y].isOpen)               //left path
            {
                child.x = left;
                child.y = y;
                check   = false;
                if (!close.Contains(child))
                {
                    if (!open.Contains(child))
                    {
                        check = true;
                        open.Add(child);
                    }
                    else if (node.map [x, y].step < node.map [left, y].step)
                    {
                        check = true;
                    }

                    if (check)
                    {
                        node.map [left, y].parent = current;
                        step = node.map [x, y].step + 1;
                        GetCost(child, B, step);
                    }
                }
            }
        }
    }