Example #1
0
        private void ProcessBest()
        {
            float      shortestDist = distanceFromStartToTarget * maxDistToTravelMultiplier + maxDistToTravelAfterDirect;
            Vector3Int bestPos      = FailedPos;

            foreach (var tile in open)
            {
                if (tile.Value.g + tile.Value.h < shortestDist)
                {
                    bestPos      = tile.Key;
                    shortestDist = tile.Value.g + tile.Value.h;
                }
            }

            Heuristics parent;

            open.TryGetValue(bestPos, out parent);

            if (target.Distance2(ref bestPos) <= range * range)
            {
                PathComplete(bestPos);
                status = Status.succeeded;
                return;
            }

            if (bestPos == FailedPos)
            {
                status = Status.failed;
            }

            ProcessTile(bestPos);
        }
Example #2
0
        public PathFinder(Vector3Int start, Vector3Int target, World world, float range = 0.5f, int entityHeight = 1)
        {
            // Don't search the path if our target is too close
            if (start.Distance2(ref target) <= range * range)
            {
                return;
            }

            path   = new List <Vector3Int>();
            open   = new Dictionary <Vector3Int, Heuristics>();
            closed = new Dictionary <Vector3Int, Heuristics>();

            this.world        = world;
            this.range        = range;
            this.entityHeight = entityHeight;
            this.start        = start;
            this.target       = target;

            distanceFromStartToTarget = start.Distance2(ref target);
            open.Add(start, new Heuristics(0, distanceFromStartToTarget, start));
            status = Status.working;
            WorkPoolManager.Add(
                new ThreadPoolItem <PathFinder>(
                    Globals.WorkPool,
                    arg =>
            {
                arg.ComputePath();
            },
                    this
                    ),
                false);
        }