Пример #1
0
    public void AddNeighbour(NavCubeNode current, Vector3Int end, Vector3Int dir, float cost, NavCubeType searchType, NavCubeAgent agent)
    {
        Vector3Int pos = current.position + dir;

        NavCubeChunk.NavCubeData data = GetData(pos);

        if (agent != null)
        {
            for (int i = 0; i < agent.height; i++)
            {
                int ii = i - agent.offset;
                if (GetData(pos + Vector3Int.up * ii).type == NavCubeType.Blocked)
                {
                    return;
                }
            }
        }
        else
        {
            if (data.type == NavCubeType.Blocked)
            {
                return;
            }
        }


        float       heuristic = pos.Manhattan(end);
        NavCubeNode ncn       = new NavCubeNode(pos, current.cost + data.cost * cost, heuristic, current);

        if (closedList.Contains(ncn))
        {
            return;
        }
        if (openList.Contains(ncn))
        {
            NavCubeNode ol_ncn = openList.Find(x => x.position.Equals(pos));
            if (ol_ncn.total > ncn.total)
            {
                ol_ncn.cost      = ncn.cost;
                ol_ncn.heuristic = ncn.heuristic;
                ol_ncn.parent    = ncn.parent;
            }
            return;
        }

        if (searchType == NavCubeType.Walking)
        {
            if (data.type == NavCubeType.Walking)
            {
                openList.Add(ncn);
            }
        }
        else
        {
            openList.Add(ncn);
        }
    }
Пример #2
0
    public void AddPathLink(NavCubeNode current, Vector3Int end, NavCubeType searchType, NavCubeAgent agent)
    {
        if (links == null)
        {
            links = new List <MavCubeLink>();
            return;
        }

        foreach (MavCubeLink link in links)
        {
            Vector3Int nextPos;
            if (link.InPosA(current.position))
            {
                nextPos = current.position + link.AtoB;
            }
            else if (link.InPosB(current.position))
            {
                nextPos = current.position + link.BtoA;
            }
            else
            {
                continue;
            }
            NavCubeChunk.NavCubeData data = GetData(nextPos);
            if (agent != null)
            {
                bool noValise = false;
                for (int i = 0; i < agent.height; i++)
                {
                    int ii = i - agent.offset;
                    if (GetData(nextPos + Vector3Int.up * ii).type == NavCubeType.Blocked)
                    {
                        noValise = true;
                        break;
                    }
                }
                if (noValise)
                {
                    continue;
                }
            }
            else
            {
                if (data.type == NavCubeType.Blocked)
                {
                    continue;
                }
            }

            float       heuristic = nextPos.Manhattan(end);
            NavCubeNode ncn       = new NavCubeNode(nextPos, current.cost + link.cost * link.movingCost, heuristic, current);
            if (closedList.Contains(ncn))
            {
                continue;
            }

            if (openList.Contains(ncn))
            {
                NavCubeNode ol_ncn = openList.Find(x => x.position.Equals(nextPos));
                if (ol_ncn.total > ncn.total)
                {
                    ol_ncn.cost      = ncn.cost;
                    ol_ncn.heuristic = ncn.heuristic;
                    ol_ncn.parent    = ncn.parent;
                }
                continue;
            }

            if (searchType == NavCubeType.Walking)
            {
                if (data.type == NavCubeType.Walking)
                {
                    openList.Add(ncn);
                }
            }
            else
            {
                openList.Add(ncn);
            }
        }
    }