// NOTE: If `NativeMultiHashMap.Clear()` is too slow we can try // using `NativeHashMap` + entities with dynamic buffers instead. void ClearMapsIfCreated() { if (HasCreatedMaps) { ColliderMap.Clear(); TargetMap.Clear(); LargeColliders.Clear(); } }
protected override void OnUpdate() { // Every frame we switch the previous frame's currentCollisions to this frame's // previous collisions. Then we clear the previous frame's previousCollisions and // make it this frame's fresh currentCollisions table. var currentCollisions = m_MapSwitch ? m_FrameCollisions0 : m_FrameCollisions1; var previousCollisions = m_MapSwitch ? m_FrameCollisions1 : m_FrameCollisions0; m_MapSwitch = !m_MapSwitch; currentCollisions.Clear(); m_PossibleCollisions.Clear(); // Despite having UpdateAfter(SpatialPartitionSystem) as attribute we still need to // combine the dependencies between them to ensure public native collection is ready // UpdateAfter attribute only handles the IComponentData dependencies and not public native // collections. var partitionHandle = JobHandle.CombineDependencies(Dependency, m_PartitionSystem.PartitionJobHandle); var broadPhase = new BroadPhaseJob { PossibleCollisions = m_PossibleCollisions.AsParallelWriter(), TargetMap = m_PartitionSystem.TargetMap, }.Schedule(m_PartitionSystem.ColliderMap, 1, partitionHandle); var narrowPhase = new NarrowPhaseJob { CurrentFrameCollisions = currentCollisions.AsParallelWriter(), PreviousFrameCollisions = previousCollisions, ColliderNormalFromEntity = GetBufferFromEntity <EdgeNormal>(true), ColliderVertexFromEntity = GetBufferFromEntity <ColliderVertex>(true), Positions = GetComponentDataFromEntity <Position2D>(true), CircleColliderFromEntity = GetComponentDataFromEntity <CircleCollider>(true), CollisionBufferFromEntity = GetBufferFromEntity <CollisionBuffer>(false), }.Schedule(m_PossibleCollisions, 1, broadPhase); CollisionJobHandle = narrowPhase; }