Lavaland chasms (#19154)
This commit is contained in:
78
Content.Shared/Chasm/ChasmSystem.cs
Normal file
78
Content.Shared/Chasm/ChasmSystem.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Movement.Events;
|
||||
using Content.Shared.StepTrigger.Systems;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.Chasm;
|
||||
|
||||
/// <summary>
|
||||
/// Handles making entities fall into chasms when stepped on.
|
||||
/// </summary>
|
||||
public sealed class ChasmSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
|
||||
[Dependency] private readonly INetManager _net = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<ChasmComponent, StepTriggeredEvent>(OnStepTriggered);
|
||||
SubscribeLocalEvent<ChasmComponent, StepTriggerAttemptEvent>(OnStepTriggerAttempt);
|
||||
SubscribeLocalEvent<ChasmFallingComponent, EntityUnpausedEvent>(OnUnpaused);
|
||||
SubscribeLocalEvent<ChasmFallingComponent, UpdateCanMoveEvent>(OnUpdateCanMove);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
// don't predict queuedels on client
|
||||
if (_net.IsClient)
|
||||
return;
|
||||
|
||||
var query = EntityQueryEnumerator<ChasmFallingComponent>();
|
||||
while (query.MoveNext(out var uid, out var chasm))
|
||||
{
|
||||
if (_timing.CurTime < chasm.NextDeletionTime)
|
||||
continue;
|
||||
|
||||
QueueDel(uid);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnStepTriggered(EntityUid uid, ChasmComponent component, ref StepTriggeredEvent args)
|
||||
{
|
||||
// already doomed
|
||||
if (HasComp<ChasmFallingComponent>(args.Tripper))
|
||||
return;
|
||||
|
||||
var falling = AddComp<ChasmFallingComponent>(args.Tripper);
|
||||
|
||||
falling.NextDeletionTime = _timing.CurTime + falling.DeletionTime;
|
||||
_blocker.UpdateCanMove(args.Tripper);
|
||||
_audio.PlayPredicted(component.FallingSound, uid, args.Tripper);
|
||||
}
|
||||
|
||||
private void OnStepTriggerAttempt(EntityUid uid, ChasmComponent component, ref StepTriggerAttemptEvent args)
|
||||
{
|
||||
if (TryComp<PhysicsComponent>(args.Tripper, out var physics) && physics.BodyStatus == BodyStatus.InAir)
|
||||
return;
|
||||
|
||||
args.Continue = true;
|
||||
}
|
||||
|
||||
private void OnUnpaused(EntityUid uid, ChasmFallingComponent component, ref EntityUnpausedEvent args)
|
||||
{
|
||||
component.NextDeletionTime += args.PausedTime;
|
||||
}
|
||||
|
||||
private void OnUpdateCanMove(EntityUid uid, ChasmFallingComponent component, UpdateCanMoveEvent args)
|
||||
{
|
||||
args.Cancel();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user