public bool ClosestRandomPointOnCircle(AiNavQuery query, ref Random random, float range, out float3 closest) { float2 current = new float2(DtCrowdAgent.Position.x, DtCrowdAgent.Position.z); float2 pos = RandomPointInCircle(current, range, ref random); float3 target = new float3(pos.x, DtCrowdAgent.Position.y, pos.y); float3 extent = new float3(range, range, range); return(query.SamplePosition(target, extent, out closest)); }
public void RegisterQuery(AiNavQuery query) { if (query.Id <= 0) { throw new InvalidOperationException(string.Format("AiQuery invalid id {0}", query.Id)); } Queries[query.Id] = query; }
public int AddAgentAtRandomPosition(DtAgentParams agentParams, AiNavQuery query) { float3 onMesh = default; if (Navigation.Query.GetRandomPosition(DtNavMesh, ref onMesh) == 1) { return(AddAgent(onMesh, agentParams)); } return(-1); }
private void OnSurfaceCreated(int surfaceId) { if (surfaceId != SurfaceId) { return; } var navSystem = EcsWorld.Active.GetExistingSystem <AiNavSystem>(); Controller = navSystem.GetSurfaceController(SurfaceId); Query = new AiNavQuery(Controller.NavMesh, 2048); Debug.Assert(Query.IsValid()); }
public CrowdController(AiNavMesh navMesh, SurfaceController surfaceController) { NavMesh = navMesh; AiNavSurfaceController = surfaceController; AiCrowd = new AiCrowd(NavMesh.DtNavMesh); Query = new AiNavQuery(NavMesh, 2048); AiCrowd.Update(Time.time); UpdateClock = new GameClock(CrowdTicksPerSecond); AgentCount = new NativeArray <int>(1, Allocator.Persistent); ReadOnlyAgents = new NativeHashMap <int, NavAgentDebug>(MaxAgents, Allocator.Persistent); Path = new NativeArray <float3>(1024, Allocator.Persistent); }
private void Start() { var navSystem = EcsWorld.Active.GetExistingSystem <AiNavSystem>(); Controller = navSystem.GetSurfaceController(SurfaceId); if (Controller == null) { Destroy(gameObject); return; } var query = new AiNavQuery(Controller.NavMesh, 2048); float3 extent = new float3(5f, 5f, 5f); float3 position = transform.position; if (!query.GetRandomPosition(ref position)) { query.Dispose(); Debug.Log("Spawn position not found"); Destroy(gameObject); return; } query.Dispose(); DtAgentParams agentParams = DtAgentParams.Default; agentParams.MaxSpeed = 6f; if (Controller.CrowdController.TryAddAgent(position, agentParams, out NavAgent agent)) { var world = EcsWorld.Active; Entity entity = world.EntityManager.CreateEntity(); world.EntityManager.AddComponentData(entity, agent); world.EntityManager.AddComponentData(entity, new AgentPathData()); world.EntityManager.AddBuffer <AgentPathBuffer>(entity); transform.position = position; CrowdIndex = agent.CrowdIndex; } else { Debug.Log("Failed adding agent"); Destroy(gameObject); return; } }
// Queries still linked to a navmesh that is being disposed are marked invalid on the native side. // Disposing them here would be bad, we just need to make sure on the native side it's not trying to use the navmesh which at this point is an invalid pointer. // This will make all calls using the invalidated navquery fail from here on out. public void UnregisterNavMesh(int id) { if (!RegisteredNavigationMeshes.Contains(id)) { throw new InvalidOperationException(string.Format("AiNavMesh not found {0}", id)); } var keys = Queries.Keys.ToList(); foreach (int key in Queries.Keys.ToList()) { AiNavQuery query = Queries[key]; if (query.NavMeshId == id) { query.Invalidate(); Queries.Remove(key); } } RegisteredNavigationMeshes.Remove(id); }