ECS thrown items (#4110)
This commit is contained in:
@@ -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<IThrowCollide> _throwCollide = new();
|
||||
|
||||
private const string ThrowingFixture = "throw-fixture";
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ThrownItemComponent, PhysicsSleepMessage>(HandleSleep);
|
||||
SubscribeLocalEvent<ThrownItemComponent, StartCollideEvent>(HandleCollision);
|
||||
SubscribeLocalEvent<ThrownItemComponent, PreventCollideEvent>(PreventCollision);
|
||||
SubscribeLocalEvent<ThrownItemComponent, ThrownEvent>(ThrowItem);
|
||||
SubscribeLocalEvent<ThrownItemComponent, LandEvent>(LandItem);
|
||||
SubscribeLocalEvent<PullStartedMessage>(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);
|
||||
|
||||
Reference in New Issue
Block a user