public void Execute(CollisionEvent e) { Entity healthEntity = Entity.Null; if (brickHealths.HasComponent(e.EntityA)) { healthEntity = e.EntityA; } else if (brickHealths.HasComponent(e.EntityB)) { healthEntity = e.EntityB; } if (healthEntity != Entity.Null) { HealthInt health = brickHealths[healthEntity]; health.curr -= 1; brickHealths[healthEntity] = health; } }
protected override void OnUpdate() { // find all of the buildings NativeArray <Entity> buildings = EntityManager.CreateEntityQuery( typeof(Building), typeof(HealthInt), typeof(Radius)) .ToEntityArray(Allocator.TempJob); if (buildings.Length == 0) { buildings.Dispose(); return; } // find all the defenses NativeArray <Entity> defenses = EntityManager.CreateEntityQuery( typeof(Defense), typeof(Radius)) .ToEntityArray(Allocator.TempJob); Entity score = EntityManager.CreateEntityQuery(typeof(Score)).GetSingletonEntity(); Entities .WithReadOnly(buildings) .WithReadOnly(defenses) .WithDeallocateOnJobCompletion(buildings) .WithDeallocateOnJobCompletion(defenses) .WithAll <Missile>() .ForEach((Entity missile, int entityInQueryIndex, ref Direction dir, ref DeletionMark mark, in Translation translation, in Radius radius, in Damage damage) => { for (int i = 0; i < buildings.Length; i++) { Radius buildingRadius = GetComponentDataFromEntity <Radius>(true)[buildings[i]]; Translation buildingPos = GetComponentDataFromEntity <Translation>(true)[buildings[i]]; if (math.distance(translation.Value, buildingPos.Value) <= radius.value + buildingRadius.value) { // hit the building, reduce health HealthInt health = GetComponentDataFromEntity <HealthInt>(false)[buildings[i]]; health.curr -= (int)damage.value; SetComponent <HealthInt>(buildings[i], health); mark.value = 1; } } for (int i = 0; i < defenses.Length; i++) { NonUniformScale defenseRadius = GetComponentDataFromEntity <NonUniformScale>(true)[defenses[i]]; Translation defensePos = GetComponentDataFromEntity <Translation>(true)[defenses[i]]; if (math.distance(translation.Value, defensePos.Value) <= radius.value + defenseRadius.Value.x * 0.25f) { // missile is overlapping the defense, mark the missile as destroyed and the player scores a point mark.value = 1; Score s = GetComponent <Score>(score); s.value += 1; SetComponent <Score>(score, s); } } if (translation.Value.y < -3.5f) { // below the ground mark.value = 1; } }).Schedule();