public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { UnityEngine.MeshRenderer meshRenderer = this.Map.GetComponent <UnityEngine.MeshRenderer>(); dstManager.AddComponentData(entity, new PhysicsCollider { Value = BoxCollider.Create ( CollisionGeomeotrySingleton.Instance.CreateOrGetBoxGeometry(meshRenderer.bounds.size), CollisionFilterSingleton.Instance.BelongsToNonTraversableFilter ) }); Entity worldBoundsEntity = dstManager.CreateEntity(dstManager.CreateWorldBoundsArchetype()); WorldBounds worldBounds = WorldBoundsStaticAccessor.WorldBounds; dstManager.SetComponentData(worldBoundsEntity, worldBounds); using (NativeArray <Entity> pathNodes = dstManager.CreateEntity(dstManager.CreatePathnodeArchetype(), worldBounds.XZGridSize.x * worldBounds.XZGridSize.y, Allocator.Temp)) { for (int x = 0; x < worldBounds.XZGridSize.x; ++x) { for (int y = 0; y < worldBounds.XZGridSize.y; ++y) { int index = AStarUtility.CalculateIndex(x, y, worldBounds.XZGridSize.x); dstManager.SetComponentData(pathNodes[index], new PathNode { X = x, Y = y, Index = index, GCost = int.MaxValue, HCost = int.MaxValue, IsTraversable = true, //Phyisics check in MonsterPathfindingGridSystem.cs but not in FindPath Method of AStar.cs saves performance! PreviousNodeIndex = -1 }); } } } }
public static IEnumerator Run ( Node start, Func <Node, float> heuristic, Func <Node, bool> satisfies, Func <Node, IEnumerable <NodeCost> > expand, Action <IEnumerable <Node> > callback ) { var initialState = new AStarState <Node>(); initialState.open.Add(start); initialState.gs[start] = 0; initialState.fs[start] = heuristic(start); initialState.previous[start] = null; initialState.current = start; var state = initialState; while (state.open.Count > 0 && !state.finished) { state = state.Clone(); var candidate = state.open.OrderBy(x => state.fs[x]).First(); state.current = candidate; if (satisfies(candidate)) { state.finished = true; } else { state.open.Remove(candidate); state.closed.Add(candidate); var neighbours = expand(candidate); if (neighbours == null || !neighbours.Any()) { continue; } var gCandidate = state.gs[candidate]; foreach (var neighbour in neighbours) { if (neighbour.endpoint.In(state.closed)) { continue; } var gNeighbour = gCandidate + neighbour.cost; state.open.Add(neighbour.endpoint); if (gNeighbour > state.gs.DefaultGet(neighbour.endpoint, () => gNeighbour)) { continue; } state.previous[neighbour.endpoint] = candidate; state.gs[neighbour.endpoint] = gNeighbour; state.fs[neighbour.endpoint] = gNeighbour + heuristic(neighbour.endpoint); } } yield return(null); } if (!state.finished) { callback(null); } else { var seq = AStarUtility.Generate(state.current, n => state.previous[n]) .TakeWhile(n => n != null) .Reverse(); callback(seq); } }