Skip to content

Commit

Permalink
Predicted movement opening lockers (space-wizards#24937)
Browse files Browse the repository at this point in the history
Relay wasn't really networked properly and this annoys me.

EntityStorage is still pretty skrunkly but this fixes the main issue I think.
  • Loading branch information
metalgearsloth authored Feb 4, 2024
1 parent 7d15672 commit 042feae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions Content.Client/Storage/Systems/EntityStorageSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class EntityStorageSystem : SharedEntityStorageSystem
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EntityStorageComponent, EntityUnpausedEvent>(OnEntityUnpausedEvent);
SubscribeLocalEvent<EntityStorageComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<EntityStorageComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<EntityStorageComponent, ActivateInWorldEvent>(OnInteract, after: new[] { typeof(LockSystem) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public override void Initialize()
base.Initialize();

/* CompRef things */
SubscribeLocalEvent<EntityStorageComponent, EntityUnpausedEvent>(OnEntityUnpausedEvent);
SubscribeLocalEvent<EntityStorageComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<EntityStorageComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<EntityStorageComponent, ActivateInWorldEvent>(OnInteract, after: new[] { typeof(LockSystem) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract partial class SharedEntityStorageComponent : Component
public readonly float MaxSize = 1.0f; // maximum width or height of an entity allowed inside the storage.

public static readonly TimeSpan InternalOpenAttemptDelay = TimeSpan.FromSeconds(0.5);
public TimeSpan LastInternalOpenAttempt;
public TimeSpan NextInternalOpenAttempt;

/// <summary>
/// Collision masks that get removed when the storage gets opened.
Expand Down Expand Up @@ -139,13 +139,16 @@ public sealed class EntityStorageComponentState : ComponentState

public float EnteringRange;

public EntityStorageComponentState(bool open, int capacity, bool isCollidableWhenOpen, bool openOnMove, float enteringRange)
public TimeSpan NextInternalOpenAttempt;

public EntityStorageComponentState(bool open, int capacity, bool isCollidableWhenOpen, bool openOnMove, float enteringRange, TimeSpan nextInternalOpenAttempt)
{
Open = open;
Capacity = capacity;
IsCollidableWhenOpen = isCollidableWhenOpen;
OpenOnMove = openOnMove;
EnteringRange = enteringRange;
NextInternalOpenAttempt = nextInternalOpenAttempt;
}
}

Expand Down
17 changes: 13 additions & 4 deletions Content.Shared/Storage/EntitySystems/SharedEntityStorageSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ public abstract class SharedEntityStorageSystem : EntitySystem

public const string ContainerName = "entity_storage";

protected void OnEntityUnpausedEvent(EntityUid uid, SharedEntityStorageComponent component, EntityUnpausedEvent args)
{
component.NextInternalOpenAttempt += args.PausedTime;
}

protected void OnGetState(EntityUid uid, SharedEntityStorageComponent component, ref ComponentGetState args)
{
args.State = new EntityStorageComponentState(component.Open,
component.Capacity,
component.IsCollidableWhenOpen,
component.OpenOnMove,
component.EnteringRange);
component.EnteringRange,
component.NextInternalOpenAttempt);
}

protected void OnHandleState(EntityUid uid, SharedEntityStorageComponent component, ref ComponentHandleState args)
Expand All @@ -66,6 +72,7 @@ protected void OnHandleState(EntityUid uid, SharedEntityStorageComponent compone
component.IsCollidableWhenOpen = state.IsCollidableWhenOpen;
component.OpenOnMove = state.OpenOnMove;
component.EnteringRange = state.EnteringRange;
component.NextInternalOpenAttempt = state.NextInternalOpenAttempt;
}

protected virtual void OnComponentInit(EntityUid uid, SharedEntityStorageComponent component, ComponentInit args)
Expand Down Expand Up @@ -123,10 +130,12 @@ protected void OnRelayMovement(EntityUid uid, SharedEntityStorageComponent compo
if (!HasComp<HandsComponent>(args.Entity))
return;

if (_timing.CurTime < component.LastInternalOpenAttempt + SharedEntityStorageComponent.InternalOpenAttemptDelay)
if (_timing.CurTime < component.NextInternalOpenAttempt)
return;

component.LastInternalOpenAttempt = _timing.CurTime;
component.NextInternalOpenAttempt = _timing.CurTime + SharedEntityStorageComponent.InternalOpenAttemptDelay;
Dirty(uid, component);

if (component.OpenOnMove)
{
TryOpenStorage(args.Entity, uid);
Expand Down Expand Up @@ -259,7 +268,7 @@ public void CloseStorage(EntityUid uid, SharedEntityStorageComponent? component
ModifyComponents(uid, component);
if (_net.IsClient && _timing.IsFirstTimePredicted)
_audio.PlayPvs(component.CloseSound, uid);
component.LastInternalOpenAttempt = default;

var afterev = new StorageAfterCloseEvent();
RaiseLocalEvent(uid, ref afterev);
}
Expand Down

0 comments on commit 042feae

Please sign in to comment.