LightCollideSystem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Content.Shared.Light.Components;
  2. using Robust.Shared.Physics.Events;
  3. using Robust.Shared.Physics.Systems;
  4. namespace Content.Shared.Light.EntitySystems;
  5. public sealed class LightCollideSystem : EntitySystem
  6. {
  7. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  8. [Dependency] private readonly SlimPoweredLightSystem _lights = default!;
  9. private EntityQuery<LightOnCollideComponent> _lightQuery;
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. _lightQuery = GetEntityQuery<LightOnCollideComponent>();
  14. SubscribeLocalEvent<LightOnCollideColliderComponent, PreventCollideEvent>(OnPreventCollide);
  15. SubscribeLocalEvent<LightOnCollideColliderComponent, StartCollideEvent>(OnStart);
  16. SubscribeLocalEvent<LightOnCollideColliderComponent, EndCollideEvent>(OnEnd);
  17. SubscribeLocalEvent<LightOnCollideColliderComponent, ComponentShutdown>(OnCollideShutdown);
  18. }
  19. private void OnCollideShutdown(Entity<LightOnCollideColliderComponent> ent, ref ComponentShutdown args)
  20. {
  21. // TODO: Check this on the event.
  22. if (TerminatingOrDeleted(ent.Owner))
  23. return;
  24. // Regenerate contacts for everything we were colliding with.
  25. var contacts = _physics.GetContacts(ent.Owner);
  26. while (contacts.MoveNext(out var contact))
  27. {
  28. if (!contact.IsTouching)
  29. continue;
  30. var other = contact.OtherEnt(ent.Owner);
  31. if (_lightQuery.HasComp(other))
  32. {
  33. _physics.RegenerateContacts(other);
  34. }
  35. }
  36. }
  37. // You may be wondering what de fok this is doing here.
  38. // At the moment there's no easy way to do collision whitelists based on components.
  39. private void OnPreventCollide(Entity<LightOnCollideColliderComponent> ent, ref PreventCollideEvent args)
  40. {
  41. if (!_lightQuery.HasComp(args.OtherEntity))
  42. {
  43. args.Cancelled = true;
  44. }
  45. }
  46. private void OnEnd(Entity<LightOnCollideColliderComponent> ent, ref EndCollideEvent args)
  47. {
  48. if (args.OurFixtureId != ent.Comp.FixtureId)
  49. return;
  50. if (!_lightQuery.HasComp(args.OtherEntity))
  51. return;
  52. // TODO: Engine bug IsTouching box2d yay.
  53. var contacts = _physics.GetTouchingContacts(args.OtherEntity) - 1;
  54. if (contacts > 0)
  55. return;
  56. _lights.SetEnabled(args.OtherEntity, false);
  57. }
  58. private void OnStart(Entity<LightOnCollideColliderComponent> ent, ref StartCollideEvent args)
  59. {
  60. if (args.OurFixtureId != ent.Comp.FixtureId)
  61. return;
  62. if (!_lightQuery.HasComp(args.OtherEntity))
  63. return;
  64. _lights.SetEnabled(args.OtherEntity, true);
  65. }
  66. }