From 0f7da24993a943143879b8669266d214b4385d29 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 8 Mar 2021 12:10:04 +1100 Subject: [PATCH] Better throwing collisions (#3568) Co-authored-by: Metal Gear Sloth --- .../Components/Items/Storage/ItemComponent.cs | 26 +------------------ .../Components/Items/ThrownItemComponent.cs | 24 ++++++++++++++++- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs b/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs index 054e3331f1..6d1c8e76dc 100644 --- a/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs +++ b/Content.Server/GameObjects/Components/Items/Storage/ItemComponent.cs @@ -7,16 +7,13 @@ using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems.ActionBlocker; using Content.Shared.GameObjects.Verbs; using Content.Shared.Interfaces.GameObjects.Components; -using Content.Shared.Physics; using Content.Shared.Utility; using Robust.Server.GameObjects; using Robust.Shared.Containers; using Robust.Shared.GameObjects; using Robust.Shared.Localization; using Robust.Shared.Players; -using Robust.Shared.Prototypes; using Robust.Shared.Physics; -using Robust.Shared.Serialization; using Robust.Shared.Serialization.Manager.Attributes; namespace Content.Server.GameObjects.Components.Items.Storage @@ -25,7 +22,7 @@ namespace Content.Server.GameObjects.Components.Items.Storage [ComponentReference(typeof(StorableComponent))] [ComponentReference(typeof(SharedStorableComponent))] [ComponentReference(typeof(IItemComponent))] - public class ItemComponent : StorableComponent, IInteractHand, IExAct, IEquipped, IUnequipped, IItemComponent, IThrown, ILand + public class ItemComponent : StorableComponent, IInteractHand, IExAct, IEquipped, IUnequipped, IItemComponent { public override string Name => "Item"; public override uint? NetID => ContentNetIDs.ITEM; @@ -155,26 +152,5 @@ namespace Content.Server.GameObjects.Components.Items.Storage Owner.TryThrow(dirVec * throwForce); } - - // TODO: Predicted - void IThrown.Thrown(ThrownEventArgs eventArgs) - { - if (!Owner.TryGetComponent(out PhysicsComponent physicsComponent)) return; - - foreach (var fixture in physicsComponent.Fixtures) - { - fixture.CollisionLayer |= (int) CollisionGroup.MobImpassable; - } - } - - void ILand.Land(LandEventArgs eventArgs) - { - if (!Owner.TryGetComponent(out PhysicsComponent physicsComponent)) return; - - foreach (var fixture in physicsComponent.Fixtures) - { - fixture.CollisionLayer &= ~(int) CollisionGroup.MobImpassable; - } - } } } diff --git a/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs b/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs index fc53c555fb..727fe326dc 100644 --- a/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs +++ b/Content.Shared/GameObjects/Components/Items/ThrownItemComponent.cs @@ -1,18 +1,23 @@ #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 + public class ThrownItemComponent : Component, IStartCollide, ICollideSpecial, IThrown, ILand { public override string Name => "ThrownItem"; public IEntity? Thrower { get; set; } + private Fixture? _fixture; + void IStartCollide.CollideWith(IPhysBody ourBody, IPhysBody otherBody, in Manifold manifold) { if (otherBody.Entity == Thrower) return; @@ -23,5 +28,22 @@ namespace Content.Shared.GameObjects.Components.Items { return collidedwith.Entity == 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); + } } }