* Fix throwing bad regencontacts call Throwing needs throwing out the window but this stops it causing problems with anomalies. * Bandaid throwing
206 lines
8.4 KiB
C#
206 lines
8.4 KiB
C#
using System.Linq;
|
|
using Content.Shared.Administration.Logs;
|
|
using Content.Shared.Database;
|
|
using Content.Shared.Gravity;
|
|
using Content.Shared.Physics;
|
|
using Content.Shared.Physics.Pull;
|
|
using Robust.Shared.GameStates;
|
|
using Robust.Shared.Physics;
|
|
using Robust.Shared.Physics.Components;
|
|
using Robust.Shared.Physics.Events;
|
|
using Robust.Shared.Physics.Systems;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Content.Shared.Throwing
|
|
{
|
|
/// <summary>
|
|
/// Handles throwing landing and collisions.
|
|
/// </summary>
|
|
public sealed class ThrownItemSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
|
|
[Dependency] private readonly FixtureSystem _fixtures = default!;
|
|
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
|
|
[Dependency] private readonly SharedGravitySystem _gravity = default!;
|
|
|
|
private const string ThrowingFixture = "throw-fixture";
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<ThrownItemComponent, MapInitEvent>(OnMapInit);
|
|
SubscribeLocalEvent<ThrownItemComponent, PhysicsSleepEvent>(OnSleep);
|
|
SubscribeLocalEvent<ThrownItemComponent, StartCollideEvent>(HandleCollision);
|
|
SubscribeLocalEvent<ThrownItemComponent, PreventCollideEvent>(PreventCollision);
|
|
SubscribeLocalEvent<ThrownItemComponent, ThrownEvent>(ThrowItem);
|
|
SubscribeLocalEvent<ThrownItemComponent, EntityUnpausedEvent>(OnThrownUnpaused);
|
|
SubscribeLocalEvent<ThrownItemComponent, ComponentGetState>(OnThrownGetState);
|
|
SubscribeLocalEvent<ThrownItemComponent, ComponentHandleState>(OnThrownHandleState);
|
|
|
|
SubscribeLocalEvent<PullStartedMessage>(HandlePullStarted);
|
|
}
|
|
|
|
private void OnThrownGetState(EntityUid uid, ThrownItemComponent component, ref ComponentGetState args)
|
|
{
|
|
// TODO: Throwing needs to handle this properly I just want the bad asserts to stop getting in my way.
|
|
TryGetNetEntity(component.Thrower, out var nent);
|
|
|
|
args.State = new ThrownItemComponentState()
|
|
{
|
|
ThrownTime = component.ThrownTime,
|
|
LandTime = component.LandTime,
|
|
Thrower = nent,
|
|
Landed = component.Landed,
|
|
PlayLandSound = component.PlayLandSound,
|
|
};
|
|
}
|
|
|
|
private void OnThrownHandleState(EntityUid uid, ThrownItemComponent component, ref ComponentHandleState args)
|
|
{
|
|
if (args.Current is not ThrownItemComponentState state)
|
|
return;
|
|
|
|
TryGetEntity(state.Thrower, out var thrower);
|
|
|
|
component.ThrownTime = state.ThrownTime;
|
|
component.LandTime = state.LandTime;
|
|
component.Thrower = thrower;
|
|
component.Landed = state.Landed;
|
|
component.PlayLandSound = state.PlayLandSound;
|
|
}
|
|
|
|
private void OnMapInit(EntityUid uid, ThrownItemComponent component, MapInitEvent args)
|
|
{
|
|
component.ThrownTime ??= _gameTiming.CurTime;
|
|
}
|
|
|
|
private void ThrowItem(EntityUid uid, ThrownItemComponent component, ref ThrownEvent @event)
|
|
{
|
|
if (!EntityManager.TryGetComponent(uid, out FixturesComponent? fixturesComponent) ||
|
|
fixturesComponent.Fixtures.Count != 1 ||
|
|
!TryComp<PhysicsComponent>(uid, out var body))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var fixture = fixturesComponent.Fixtures.Values.First();
|
|
var shape = fixture.Shape;
|
|
_fixtures.TryCreateFixture(uid, shape, ThrowingFixture, hard: false, collisionMask: (int) CollisionGroup.ThrownItem, manager: fixturesComponent, body: body);
|
|
}
|
|
|
|
private void OnThrownUnpaused(EntityUid uid, ThrownItemComponent component, ref EntityUnpausedEvent args)
|
|
{
|
|
if (component.LandTime != null)
|
|
{
|
|
component.LandTime = component.LandTime.Value + args.PausedTime;
|
|
}
|
|
}
|
|
|
|
private void HandleCollision(EntityUid uid, ThrownItemComponent component, ref StartCollideEvent args)
|
|
{
|
|
if (!args.OtherFixture.Hard)
|
|
return;
|
|
|
|
if (args.OtherEntity == component.Thrower)
|
|
return;
|
|
|
|
ThrowCollideInteraction(component, args.OurEntity, args.OtherEntity);
|
|
}
|
|
|
|
private void PreventCollision(EntityUid uid, ThrownItemComponent component, ref PreventCollideEvent args)
|
|
{
|
|
if (args.OtherEntity == component.Thrower)
|
|
{
|
|
args.Cancelled = true;
|
|
}
|
|
}
|
|
|
|
private void OnSleep(EntityUid uid, ThrownItemComponent thrownItem, ref PhysicsSleepEvent @event)
|
|
{
|
|
StopThrow(uid, thrownItem);
|
|
}
|
|
|
|
private void HandlePullStarted(PullStartedMessage message)
|
|
{
|
|
// TODO: this isn't directed so things have to be done the bad way
|
|
if (EntityManager.TryGetComponent(message.Pulled.Owner, out ThrownItemComponent? thrownItemComponent))
|
|
StopThrow(message.Pulled.Owner, thrownItemComponent);
|
|
}
|
|
|
|
public void StopThrow(EntityUid uid, ThrownItemComponent thrownItemComponent)
|
|
{
|
|
if (TryComp<PhysicsComponent>(uid, out var physics))
|
|
{
|
|
_physics.SetBodyStatus(physics, BodyStatus.OnGround);
|
|
|
|
if (physics.Awake)
|
|
_broadphase.RegenerateContacts(uid, physics);
|
|
}
|
|
|
|
if (EntityManager.TryGetComponent(uid, out FixturesComponent? manager))
|
|
{
|
|
var fixture = _fixtures.GetFixtureOrNull(uid, ThrowingFixture, manager: manager);
|
|
|
|
if (fixture != null)
|
|
{
|
|
_fixtures.DestroyFixture(uid, ThrowingFixture, fixture, manager: manager);
|
|
}
|
|
}
|
|
|
|
EntityManager.EventBus.RaiseLocalEvent(uid, new StopThrowEvent { User = thrownItemComponent.Thrower }, true);
|
|
EntityManager.RemoveComponent<ThrownItemComponent>(uid);
|
|
}
|
|
|
|
public void LandComponent(EntityUid uid, ThrownItemComponent thrownItem, PhysicsComponent physics, bool playSound)
|
|
{
|
|
if (thrownItem.Landed || thrownItem.Deleted || _gravity.IsWeightless(uid) || Deleted(uid))
|
|
return;
|
|
|
|
thrownItem.Landed = true;
|
|
|
|
// Assume it's uninteresting if it has no thrower. For now anyway.
|
|
if (thrownItem.Thrower is not null)
|
|
_adminLogger.Add(LogType.Landed, LogImpact.Low, $"{ToPrettyString(uid):entity} thrown by {ToPrettyString(thrownItem.Thrower.Value):thrower} landed.");
|
|
|
|
_broadphase.RegenerateContacts(uid, physics);
|
|
var landEvent = new LandEvent(thrownItem.Thrower, playSound);
|
|
RaiseLocalEvent(uid, ref landEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises collision events on the thrown and target entities.
|
|
/// </summary>
|
|
public void ThrowCollideInteraction(ThrownItemComponent component, EntityUid thrown, EntityUid target)
|
|
{
|
|
if (component.Thrower is not null)
|
|
_adminLogger.Add(LogType.ThrowHit, LogImpact.Low,
|
|
$"{ToPrettyString(thrown):thrown} thrown by {ToPrettyString(component.Thrower.Value):thrower} hit {ToPrettyString(target):target}.");
|
|
|
|
RaiseLocalEvent(target, new ThrowHitByEvent(thrown, target, component), true);
|
|
RaiseLocalEvent(thrown, new ThrowDoHitEvent(thrown, target, component), true);
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
|
|
var query = EntityQueryEnumerator<ThrownItemComponent, PhysicsComponent>();
|
|
while (query.MoveNext(out var uid, out var thrown, out var physics))
|
|
{
|
|
if (thrown.LandTime <= _gameTiming.CurTime)
|
|
{
|
|
LandComponent(uid, thrown, physics, thrown.PlayLandSound);
|
|
}
|
|
|
|
var stopThrowTime = (thrown.LandTime ?? thrown.ThrownTime) + TimeSpan.FromSeconds(ThrowingSystem.FlyTime);
|
|
if (stopThrowTime <= _gameTiming.CurTime)
|
|
{
|
|
StopThrow(uid, thrown);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|