コード例 #1
0
    void FindPlayer()
    {
        if (precision <= 0)
        {
            precision = 1;
        }
        find = new Queue <PathPoint>();
        find.Enqueue(new PathPoint(transform.position));
        PathPoint pp = find.Peek();

        Debug.Log(Vector2.Distance(player.transform.position, pp.GetPos()));
        while (Vector2.Distance(player.transform.position, pp.GetPos()) > precision && Vector2.Distance(player.transform.position, pp.GetPos()) <= 11f * precision)
        {
            pp = find.Peek();
            if (Vector2.Distance(player.transform.position, pp.GetPos()) <= precision)
            {
                path = new Stack <PathPoint>();
                PathPoint back = pp;
                while (back.LastPoint() != null)
                {
                    path.Push(back);
                    //Debug.Log(back.LastPoint().GetPos());
                    back = back.LastPoint();
                }
                StartCoroutine(Move());
                return;
            }
            Debug.Log(find.Count);
            for (int i = 0; i < 4; i++)
            {
                PathPoint nextPP = new PathPoint(pp.GetPos() + dirs[i] * precision);
                //距离缩短
                if (Vector2.Distance(player.transform.position, pp.GetPos()) > Vector2.Distance(player.transform.position, nextPP.GetPos()))
                {
                    //无障碍物
                    if (true)
                    {
                        find.Enqueue(nextPP);
                        nextPP.SetLastPoint(pp);
                    }
                    //有障碍物
                    else
                    {
                        continue;
                    }
                }
            }
            if (find.Peek() != null)
            {
                find.Dequeue();
            }
        }
    }