ECS thrown items (#4110)

This commit is contained in:
metalgearsloth
2021-05-31 17:13:40 +10:00
committed by GitHub
parent 8a6ab624ab
commit 10615c233c
3 changed files with 56 additions and 38 deletions

View File

@@ -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<ThrownItemSystem>().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);
}
}
}