AnimateSpellSystem.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Content.Shared.Magic.Components;
  2. using Content.Shared.Physics;
  3. using Robust.Shared.Containers;
  4. using Robust.Shared.Physics;
  5. using Robust.Shared.Physics.Components;
  6. using Robust.Shared.Physics.Systems;
  7. using System.Linq;
  8. namespace Content.Shared.Magic.Systems;
  9. public sealed class AnimateSpellSystem : EntitySystem
  10. {
  11. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  12. [Dependency] private readonly SharedTransformSystem _transform = default!;
  13. [Dependency] private readonly SharedContainerSystem _container = default!;
  14. public override void Initialize()
  15. {
  16. SubscribeLocalEvent<AnimateComponent, MapInitEvent>(OnAnimate);
  17. }
  18. private void OnAnimate(Entity<AnimateComponent> ent, ref MapInitEvent args)
  19. {
  20. // Physics bullshittery necessary for object to behave properly
  21. if (!TryComp<FixturesComponent>(ent, out var fixtures) || !TryComp<PhysicsComponent>(ent, out var physics))
  22. return;
  23. var xform = Transform(ent);
  24. var fixture = fixtures.Fixtures.First();
  25. _transform.Unanchor(ent); // If left anchored they are effectively stuck/immobile and not a threat
  26. _physics.SetCanCollide(ent, true, true, false, fixtures, physics);
  27. _physics.SetCollisionMask(ent, fixture.Key, fixture.Value, (int)CollisionGroup.FlyingMobMask, fixtures, physics);
  28. _physics.SetCollisionLayer(ent, fixture.Key, fixture.Value, (int)CollisionGroup.FlyingMobLayer, fixtures, physics);
  29. _physics.SetBodyType(ent, BodyType.KinematicController, fixtures, physics, xform);
  30. _physics.SetBodyStatus(ent, physics, BodyStatus.InAir, true);
  31. _physics.SetFixedRotation(ent, false, true, fixtures, physics);
  32. _physics.SetHard(ent, fixture.Value, true, fixtures);
  33. _container.AttachParentToContainerOrGrid((ent, xform)); // Items animated inside inventory now exit, they can't be picked up and so can't escape otherwise
  34. var ev = new AnimateSpellEvent();
  35. RaiseLocalEvent(ref ev);
  36. }
  37. }
  38. [ByRefEvent]
  39. public readonly record struct AnimateSpellEvent;