예제 #1
0
        private void CreatePathfindTest()
        {
            SetupPathfinding();
            _pathfinderTest = new List <DebugPathfind>();
            var pathfinder = new AstarP3Pathfinder();

            AstarP3Pathfinder.SetAxis(2);
            var grid     = World.Get <PathfindingSystem>().Grid;
            var nodePath = new List <Point3>();
            var start    = transform.position.toPoint3();
            var result   = pathfinder.Run(PathfindingRequest.Create(grid, 0, start, (transform.position + _target).toPoint3(), null, false, nodePath));

            if (nodePath.Count == 0)
            {
                Debug.Log(pathfinder.KeyedDict.Count + " " + result);
                grid.ClearAll();
                return;
            }
            var startCost = pathfinder.KeyedDict[nodePath[0]].TotalCost;
            var endNode   = pathfinder.KeyedDict[nodePath.LastElement()];

            endNode.EndCost   = 0;
            endNode.StartCost = Vector3.Distance(nodePath[0].toVector3(), endNode.Value.toVector3());
            float maxCost = startCost;

            foreach (var p3Node in pathfinder.KeyedDict)
            {
                if (p3Node.Value.TotalCost > maxCost)
                {
                    maxCost = p3Node.Value.TotalCost;
                }
            }
            foreach (var p3Node in pathfinder.KeyedDict)
            {
                Color nodeColor;
                if (nodePath.Contains(p3Node.Value.Value))
                {
                    nodeColor = Color.green;
                }
                else
                {
                    nodeColor = Color.Lerp(Color.white, Color.red, Mathf.InverseLerp(startCost, maxCost, p3Node.Value.TotalCost));
                }
                var node = new DebugPathfind(p3Node.Value.Value.toVector3(), nodeColor, p3Node.Value.StartCost, p3Node.Value.EndCost);
                _pathfinderTest.Add(node);
            }
            grid.ClearAll();
        }
예제 #2
0
        public static PathfindingRequest Create(IPathfindingGrid grid, int id, Point3 start, Point3 end, PathFound foundEvent, bool overSized, List <Point3> path)
        {
            PathfindingRequest r;

            if (_pooled.Count > 0)
            {
                r             = _pooled.Dequeue();
                r.Start       = start;
                r.End         = end;
                r.ReturnEvent = foundEvent;
                r.Path        = path;
                r.ID          = id;
                r.Grid        = grid;
                r.IsOversized = overSized;
            }
            else
            {
                r = new PathfindingRequest(grid, id, start, end, foundEvent, overSized, path);
            }
            World.Get <PathfindingSystem>().Enqueue(r);
            return(r);
        }