public void Init(IPathFindingRules pathFindingRules, AStarSearch search) { _pathFindingRules = pathFindingRules; _search = search; UnBuild(); Build(); }
public List <Node> Search(Node start, Node goal, IPathFindingRules rules, PathFindingAgent agent) { _openSet.Clear(); _closedSet.Clear(); _openSet.Add(start); var foundGoal = false; while (_openSet.Count > 0) { Node node = _openSet.RemoveFirst(); _closedSet.Add(node); if (node == goal) { foundGoal = true; break; } var transitions = rules.GetTransitions(_pathFindingGrid, agent, node); foreach (var transition in transitions) { Node neighbour = transition.Node; if (/*!neighbour.IsWalkable ||*/ _closedSet.Contains(neighbour)) { continue; } int newCost = node.GCost + transition.Cost; if (newCost < neighbour.GCost || !_openSet.Contains(neighbour)) { neighbour.GCost = newCost; neighbour.HCost = rules.GetHeuristic(neighbour, goal, agent); neighbour.Parent = node; neighbour.Transition = transition.Transition; if (_openSet.Contains(neighbour)) { _openSet.UpdateItem(neighbour); } else { _openSet.Add(neighbour); } } } } return(foundGoal ? RetracePath(start, goal) : null); }
void Start() { _grid = new Node[_gridSizeY, _gridSizeX]; _position = transform.position; Vector2 bottomLeftCell = GetBottomLeftCellCenter(); for (var y = 0; y < _gridSizeY; ++y) { for (var x = 0; x < _gridSizeX; ++x) { Vector2 cellCenter = bottomLeftCell + new Vector2(x * _cellSize, y * _cellSize); bool isOccupiedCell = IsOccupiedCell(cellCenter); _grid[y, x] = new Node(!isOccupiedCell, cellCenter, x, y); } } _pathFindingRules = new PlatformerRules(_agent); _search = new AStarSearch(this); }