From 10615c233c1fecc652e558e5579dd7f98a766423 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 31 May 2021 17:13:40 +1000 Subject: [PATCH] ECS thrown items (#4110) --- .../Components/Items/ThrownItemComponent.cs | 38 +------------ .../EntitySystems/ThrownItemSystem.cs | 54 +++++++++++++++++++ Content.Shared/Physics/CollisionGroup.cs | 2 +- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs b/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs index f96b7aaad5..e28276f9ff 100644 --- a/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs +++ b/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs @@ -1,49 +1,13 @@ #nullable enable -using Content.Shared.GameObjects.EntitySystems; -using Content.Shared.Interfaces.GameObjects.Components; -using Content.Shared.Physics; using Robust.Shared.GameObjects; -using Robust.Shared.Physics; -using Robust.Shared.Physics.Collision; -using Robust.Shared.Physics.Dynamics; namespace Content.Shared.GameObjects.Components.Items { [RegisterComponent] - public class ThrownItemComponent : Component, IStartCollide, ICollideSpecial, IThrown, ILand + public class ThrownItemComponent : Component { public override string Name => "ThrownItem"; public IEntity? Thrower { get; set; } - - private Fixture? _fixture; - - void IStartCollide.CollideWith(Fixture ourFixture, Fixture otherFixture, in Manifold manifold) - { - if (otherFixture.Body.Owner == Thrower) return; - EntitySystem.Get().ThrowCollideInteraction(Thrower, ourFixture.Body, otherFixture.Body); - } - - bool ICollideSpecial.PreventCollide(IPhysBody collidedwith) - { - return collidedwith.Owner == Thrower; - } - - void IThrown.Thrown(ThrownEventArgs eventArgs) - { - if (!Owner.TryGetComponent(out PhysicsComponent? physicsComponent) || - physicsComponent.Fixtures.Count != 1) return; - - var shape = physicsComponent.Fixtures[0].Shape; - _fixture = new Fixture(physicsComponent, shape) {CollisionLayer = (int) CollisionGroup.ThrownItem, Hard = false}; - physicsComponent.AddFixture(_fixture); - } - - void ILand.Land(LandEventArgs eventArgs) - { - if (!Owner.TryGetComponent(out PhysicsComponent? physicsComponent) || _fixture == null) return; - - physicsComponent.RemoveFixture(_fixture); - } } } diff --git a/Content.Shared/GameObjects/EntitySystems/ThrownItemSystem.cs b/Content.Shared/GameObjects/EntitySystems/ThrownItemSystem.cs index 9911e453a1..d52700685f 100644 --- a/Content.Shared/GameObjects/EntitySystems/ThrownItemSystem.cs +++ b/Content.Shared/GameObjects/EntitySystems/ThrownItemSystem.cs @@ -4,7 +4,9 @@ using System.Linq; using Content.Shared.GameObjects.Components.Items; using Content.Shared.Physics.Pull; using Content.Shared.Interfaces.GameObjects.Components; +using Content.Shared.Physics; using Robust.Shared.GameObjects; +using Robust.Shared.Log; using Robust.Shared.Physics; using Robust.Shared.Physics.Dynamics; @@ -17,13 +19,65 @@ namespace Content.Shared.GameObjects.EntitySystems { private List _throwCollide = new(); + private const string ThrowingFixture = "throw-fixture"; + public override void Initialize() { base.Initialize(); SubscribeLocalEvent(HandleSleep); + SubscribeLocalEvent(HandleCollision); + SubscribeLocalEvent(PreventCollision); + SubscribeLocalEvent(ThrowItem); + SubscribeLocalEvent(LandItem); SubscribeLocalEvent(HandlePullStarted); } + private void LandItem(EntityUid uid, ThrownItemComponent component, LandEvent args) + { + if (!component.Owner.TryGetComponent(out PhysicsComponent? physicsComponent)) return; + + var fixture = physicsComponent.GetFixture(ThrowingFixture); + if (fixture == null) + { + Logger.Error($"Tried to remove throwing fixture for {component.Owner} but none found?"); + return; + } + physicsComponent.RemoveFixture(fixture); + } + + private void ThrowItem(EntityUid uid, ThrownItemComponent component, ThrownEvent args) + { + if (!component.Owner.TryGetComponent(out PhysicsComponent? physicsComponent) || + physicsComponent.Fixtures.Count != 1) return; + + if (physicsComponent.GetFixture(ThrowingFixture) != null) + { + Logger.Error($"Found existing throwing fixture on {component.Owner}"); + return; + } + + var shape = physicsComponent.Fixtures[0].Shape; + var fixture = new Fixture(physicsComponent, shape) {CollisionLayer = (int) CollisionGroup.ThrownItem, Hard = false, Name = ThrowingFixture}; + physicsComponent.AddFixture(fixture); + } + + private void HandleCollision(EntityUid uid, ThrownItemComponent component, StartCollideEvent args) + { + var thrower = component.Thrower; + var otherBody = args.OtherFixture.Body; + + if (otherBody.Owner == thrower) return; + ThrowCollideInteraction(thrower, args.OurFixture.Body, otherBody); + } + + private void PreventCollision(EntityUid uid, ThrownItemComponent component, PreventCollideEvent args) + { + if (args.BodyB.Owner == component.Thrower) + { + args.Cancel(); + } + } + private void HandleSleep(EntityUid uid, ThrownItemComponent thrownItem, PhysicsSleepMessage message) { LandComponent(thrownItem); diff --git a/Content.Shared/Physics/CollisionGroup.cs b/Content.Shared/Physics/CollisionGroup.cs index b609b86c5c..eb63c2abfa 100644 --- a/Content.Shared/Physics/CollisionGroup.cs +++ b/Content.Shared/Physics/CollisionGroup.cs @@ -27,7 +27,7 @@ namespace Content.Shared.Physics MapGrid = MapGridHelpers.CollisionGroup, // Map grids, like shuttles. This is the actual grid itself, not the walls or other entities connected to the grid. MobMask = Impassable | MobImpassable | VaultImpassable | SmallImpassable, - ThrownItem = MobImpassable | Impassable, + ThrownItem = VaultImpassable, // 32 possible groups AllMask = -1, }