Change ThrownItemComponent to be removed after flytime (#20700)

This commit is contained in:
DrSmugleaf
2023-10-06 17:43:54 -07:00
committed by GitHub
parent 6af7c90b73
commit 783ed6f917
4 changed files with 77 additions and 35 deletions

View File

@@ -3,12 +3,11 @@ using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Physics;
using Content.Shared.Physics.Pull;
using Robust.Shared.Containers;
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
{
@@ -18,6 +17,7 @@ namespace Content.Shared.Throwing
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!;
@@ -27,29 +27,18 @@ namespace Content.Shared.Throwing
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, ComponentGetState>(OnGetState);
SubscribeLocalEvent<ThrownItemComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<ThrownItemComponent, EntityUnpausedEvent>(OnThrownUnpaused);
SubscribeLocalEvent<PullStartedMessage>(HandlePullStarted);
}
private void OnGetState(EntityUid uid, ThrownItemComponent component, ref ComponentGetState args)
private void OnMapInit(EntityUid uid, ThrownItemComponent component, MapInitEvent args)
{
args.State = new ThrownItemComponentState(GetNetEntity(component.Thrower));
}
private void OnHandleState(EntityUid uid, ThrownItemComponent component, ref ComponentHandleState args)
{
if (args.Current is not ThrownItemComponentState { Thrower: not null } state ||
!state.Thrower.Value.IsValid())
{
return;
}
component.Thrower = EnsureEntity<ThrownItemComponent>(state.Thrower.Value, uid);
component.ThrownTime ??= _gameTiming.CurTime;
}
private void ThrowItem(EntityUid uid, ThrownItemComponent component, ThrownEvent args)
@@ -66,6 +55,14 @@ namespace Content.Shared.Throwing
_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)
@@ -120,9 +117,11 @@ namespace Content.Shared.Throwing
public void LandComponent(EntityUid uid, ThrownItemComponent thrownItem, PhysicsComponent physics, bool playSound)
{
if (thrownItem.Deleted || Deleted(uid))
if (thrownItem.Landed || thrownItem.Deleted || 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.");
@@ -130,8 +129,6 @@ namespace Content.Shared.Throwing
_broadphase.RegenerateContacts(uid, physics);
var landEvent = new LandEvent(thrownItem.Thrower, playSound);
RaiseLocalEvent(uid, ref landEvent);
StopThrow(uid, thrownItem);
}
/// <summary>
@@ -146,5 +143,25 @@ namespace Content.Shared.Throwing
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);
}
}
}
}
}