Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created April 4, 2020 15:12
Show Gist options
  • Select an option

  • Save jeffvella/fc9e674b7edd02e8a343929256087764 to your computer and use it in GitHub Desktop.

Select an option

Save jeffvella/fc9e674b7edd02e8a343929256087764 to your computer and use it in GitHub Desktop.
protected override void OnUpdate()
{
var collisionEvents = _collisionEvents;
var detectors = _buffer;
detectors.Clear();
Entities.ForEach((Entity entity, ref CollisionDetector detector, in Translation pos, in OwnerRef owner) =>
{
detectors.Add(new DetectorInfo
{
Entity = entity,
Position = pos.Value,
Range = detector.Range,
Owner = owner
});
}).Run();
Entities.WithNone<CollisionDetector>().ForEach((Entity entity, ref ActorDefinition def, ref Translation pos) =>
{
for (int i = 0; i < detectors.Length; i++)
{
var detector = detectors[i];
var distance = math.distance(detector.Position, pos.Value);
if (distance < detector.Range)
{
collisionEvents.Enqueue(new CollisionEvent
{
Source = detector,
Hit = new Hit
{
Position = detector.Position + (pos.Value - detector.Position),
Target = entity
},
});
}
}
}).WithReadOnly(detectors).WithoutBurst().Run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment