using TinyKitchen; using Unity.Collections; using Unity.Entities; using Unity.Jobs; using Unity.Physics; using Unity.Physics.Systems; namespace TinyRacing.Systems { /// /// Detect collision with environment /// [UpdateAfter(typeof(EndFramePhysicsSystem))] public class CollideWithEnvironment : JobComponentSystem { BuildPhysicsWorld m_BuildPhysicsWorldSystem; StepPhysicsWorld m_StepPhysicsWorldSystem; EndSimulationEntityCommandBufferSystem m_EntityCommandBufferSystem; protected override void OnCreate() { base.OnCreate(); m_BuildPhysicsWorldSystem = World.GetOrCreateSystem(); m_StepPhysicsWorldSystem = World.GetOrCreateSystem(); m_EntityCommandBufferSystem = World.GetOrCreateSystem(); } protected override JobHandle OnUpdate(JobHandle inputDeps) { MakeBouncingSound job = new MakeBouncingSound { FoodGroup = GetComponentDataFromEntity(), EntityCommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer() }; JobHandle jobHandle = job.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, inputDeps); m_EntityCommandBufferSystem.AddJobHandleForProducer(jobHandle); return jobHandle; } struct MakeBouncingSound : ICollisionEventsJob { [ReadOnly] public ComponentDataFromEntity FoodGroup; public EntityCommandBuffer EntityCommandBuffer; public Entity GetEntityFromComponentGroup(Entity entityA, Entity entityB, ComponentDataFromEntity componentGroup) where T : struct, IComponentData { if (componentGroup.HasComponent(entityA)) return entityA; if (componentGroup.HasComponent(entityB)) return entityB; return Entity.Null; } public void Execute(CollisionEvent collisionEvent) { var entityA = collisionEvent.EntityA; var entityB = collisionEvent.EntityB; var foodEntity = GetEntityFromComponentGroup(entityA, entityB, FoodGroup); if (foodEntity != Entity.Null) { var foodInstance = FoodGroup[foodEntity]; if (foodInstance.isLaunched) { foodInstance.playBouncingAudio = true; EntityCommandBuffer.SetComponent(foodEntity, foodInstance); } } } } } }