CardboardBoxSystem.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Numerics;
  2. using Content.Shared.Body.Components;
  3. using Content.Shared.CardboardBox;
  4. using Content.Shared.CardboardBox.Components;
  5. using Content.Shared.Examine;
  6. using Content.Shared.Movement.Components;
  7. using Robust.Client.GameObjects;
  8. namespace Content.Client.CardboardBox;
  9. public sealed class CardboardBoxSystem : SharedCardboardBoxSystem
  10. {
  11. [Dependency] private readonly EntityLookupSystem _entityLookup = default!;
  12. [Dependency] private readonly TransformSystem _transform = default!;
  13. [Dependency] private readonly ExamineSystemShared _examine = default!;
  14. private EntityQuery<BodyComponent> _bodyQuery;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. _bodyQuery = GetEntityQuery<BodyComponent>();
  19. SubscribeNetworkEvent<PlayBoxEffectMessage>(OnBoxEffect);
  20. }
  21. private void OnBoxEffect(PlayBoxEffectMessage msg)
  22. {
  23. var source = GetEntity(msg.Source);
  24. if (!TryComp<CardboardBoxComponent>(source, out var box))
  25. return;
  26. var xformQuery = GetEntityQuery<TransformComponent>();
  27. if (!xformQuery.TryGetComponent(source, out var xform))
  28. return;
  29. var sourcePos = _transform.GetMapCoordinates(source, xform);
  30. //Any mob that can move should be surprised?
  31. //God mind rework needs to come faster so it can just check for mind
  32. //TODO: Replace with Mind Query when mind rework is in.
  33. var mobMoverEntities = new List<EntityUid>();
  34. var mover = GetEntity(msg.Mover);
  35. //Filter out entities in range to see that they're a mob and add them to the mobMoverEntities hash for faster lookup
  36. var movers = new HashSet<Entity<MobMoverComponent>>();
  37. _entityLookup.GetEntitiesInRange(xform.Coordinates, box.Distance, movers);
  38. foreach (var moverComp in movers)
  39. {
  40. var uid = moverComp.Owner;
  41. if (uid == mover)
  42. continue;
  43. mobMoverEntities.Add(uid);
  44. }
  45. //Play the effect for the mobs as long as they can see the box and are in range.
  46. foreach (var mob in mobMoverEntities)
  47. {
  48. var mapPos = _transform.GetMapCoordinates(mob);
  49. if (!_examine.InRangeUnOccluded(sourcePos, mapPos, box.Distance, null))
  50. continue;
  51. // no effect for anything too exotic
  52. if (!_bodyQuery.HasComp(mob))
  53. continue;
  54. var ent = Spawn(box.Effect, mapPos);
  55. if (!xformQuery.TryGetComponent(ent, out var entTransform) || !TryComp<SpriteComponent>(ent, out var sprite))
  56. continue;
  57. sprite.Offset = new Vector2(0, 1);
  58. _transform.SetParent(ent, entTransform, mob);
  59. }
  60. }
  61. }