コード例 #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
        private void CreateObstacles()
        {
            AstarP3Pathfinder.SetAxis(2);
            SetupPathfinding();
            var grid = World.Get <PathfindingSystem>().Grid;

            if (_targetParent != null)
            {
                foreach (Transform child in _targetParent)
                {
                    grid.SetAgentCurrentPath(child.transform.position.toPoint3ZeroY(), 5, true);
                }
            }
            if (_blockedParent != null)
            {
                foreach (Transform child in _blockedParent)
                {
                    grid.SetStationaryAgent(child.transform.position.toPoint3ZeroY(), 5, true);
                }
            }
        }