public void FindPathToNearestPoint1Test() { var points = AStarPathSearch.FindPathToNearestPoint(_closeMap1, _startPosition, _targetPosition); Assert.NotNull(points); Assert.AreEqual(5, points.Count); }
private void UpdateGame(float deltaTime) { foreach (var unit in _units.Values) { unit.Update(deltaTime); } UpdateMap(); foreach (var unit in _units.Values) { if (unit.Unit.Position == unit.Unit.TargetPosition || unit.Position != unit.Target) { continue; } if (unit.Path == null || unit.Path.Any(p => p != unit.Unit.Position && _map[p.X, p.Y] != 0)) { var path = AStarPathSearch.FindPath(_map, unit.Unit.Position, unit.Unit.TargetPosition); if (path != null) { unit.Path = path.GetRange(1, path.Count - 1); } else { var pathToNearestPoint = AStarPathSearch.FindPathToNearestPoint(_map, unit.Unit.Position, unit.OriginTarget); if (pathToNearestPoint.Count == 1 && pathToNearestPoint[0] == unit.Unit.Position) { unit.Unit.TargetPosition = unit.Unit.Position; continue; } unit.Path = pathToNearestPoint.GetRange(1, pathToNearestPoint.Count - 1); unit.Unit.TargetPosition = unit.Path[unit.Path.Count - 1]; } } if (unit.Path.Count == 0) { continue; } _map[unit.Unit.Position.X, unit.Unit.Position.Y] = 0; unit.Unit.Position = unit.Path.First(p => p != unit.Unit.Position); _map[unit.Unit.Position.X, unit.Unit.Position.Y] = 1; unit.Path.Remove(unit.Unit.Position); unit.SetTarget(new PositionF(unit.Unit.Position.X * CellSize, unit.Unit.Position.Y * CellSize)); } }