Пример #1
0
        public static Path FindPath(
            ISearchCache searchCache,
            Vec2I start,
            Func <Vec2I, bool> passableFn,
            Func <Vec2I, bool> destinationFn,
            Func <Vec2I, float> heuristicFn)
        {
            var cache = (searchCache as SearchCache);

            cache.Reset();

            cache.Open(null, start, 0, 0);
            while (cache.hasOpen)
            {
                Node n = cache.NextOpen();
                if (destinationFn(n.pos))
                {
                    return(ConstructPath(n));
                }

                foreach (Vec2I dir in directions)
                {
                    Vec2I pos = n.pos + dir;
                    if (!MathExt.InGrid(cache.size, pos) || cache.Closed(pos) || !passableFn(pos))
                    {
                        continue;
                    }

                    if (dir.x != 0 && dir.y != 0)
                    {
                        if (!passableFn(n.pos + new Vec2I(dir.x, 0)) ||
                            !passableFn(n.pos + new Vec2I(0, dir.y)))
                        {
                            continue;
                        }
                    }

                    float g = n.g + Vec2I.Distance(pos, n.pos);

                    Node openNode = cache.GetOpenNode(pos);
                    if (openNode != null)
                    {
                        if (g < openNode.g)
                        {
                            openNode.parent = n;
                            openNode.g      = g;
                            cache.UpdateOpenNode(openNode);
                        }
                    }
                    else
                    {
                        cache.Open(n, pos, g, heuristicFn(pos));
                    }
                }
            }

            return(null);
        }
Пример #2
0
 public bool ValidTile(Vec2I tile) => MathExt.InGrid(size, tile);