Make UseDelay SetLength do EnsureComp (#27601)

Make UseDelay.SetLength do EnsureComp
This commit is contained in:
Tayrtahn
2024-05-02 08:16:16 -04:00
committed by GitHub
parent 05f0fddf5d
commit 236da1cd27
3 changed files with 12 additions and 10 deletions

View File

@@ -144,7 +144,7 @@ public sealed class SpraySystem : EntitySystem
_audio.PlayPvs(entity.Comp.SpraySound, entity, entity.Comp.SpraySound.Params.WithVariation(0.125f)); _audio.PlayPvs(entity.Comp.SpraySound, entity, entity.Comp.SpraySound.Params.WithVariation(0.125f));
_useDelay.SetLength((entity, useDelay), TimeSpan.FromSeconds(cooldownTime)); _useDelay.SetLength(entity.Owner, TimeSpan.FromSeconds(cooldownTime));
_useDelay.TryResetDelay((entity, useDelay)); _useDelay.TryResetDelay((entity, useDelay));
} }
} }

View File

@@ -135,11 +135,8 @@ public abstract class SharedStorageSystem : EntitySystem
private void OnMapInit(Entity<StorageComponent> entity, ref MapInitEvent args) private void OnMapInit(Entity<StorageComponent> entity, ref MapInitEvent args)
{ {
if (TryComp<UseDelayComponent>(entity, out var useDelayComp)) UseDelay.SetLength(entity.Owner, entity.Comp.QuickInsertCooldown, QuickInsertUseDelayID);
{ UseDelay.SetLength(entity.Owner, entity.Comp.OpenUiCooldown, OpenUiUseDelayID);
UseDelay.SetLength((entity, useDelayComp), entity.Comp.QuickInsertCooldown, QuickInsertUseDelayID);
UseDelay.SetLength((entity, useDelayComp), entity.Comp.OpenUiCooldown, OpenUiUseDelayID);
}
} }
private void OnStorageGetState(EntityUid uid, StorageComponent component, ref ComponentGetState args) private void OnStorageGetState(EntityUid uid, StorageComponent component, ref ComponentGetState args)

View File

@@ -47,7 +47,7 @@ public sealed class UseDelaySystem : EntitySystem
{ {
// Set default delay length from the prototype // Set default delay length from the prototype
// This makes it easier for simple use cases that only need a single delay // This makes it easier for simple use cases that only need a single delay
SetLength(ent, ent.Comp.Delay, DefaultId); SetLength((ent, ent.Comp), ent.Comp.Delay, DefaultId);
} }
private void OnUnpaused(Entity<UseDelayComponent> ent, ref EntityUnpausedEvent args) private void OnUnpaused(Entity<UseDelayComponent> ent, ref EntityUnpausedEvent args)
@@ -62,9 +62,14 @@ public sealed class UseDelaySystem : EntitySystem
/// <summary> /// <summary>
/// Sets the length of the delay with the specified ID. /// Sets the length of the delay with the specified ID.
/// </summary> /// </summary>
public bool SetLength(Entity<UseDelayComponent> ent, TimeSpan length, string id = DefaultId) /// <remarks>
/// This will add a UseDelay component to the entity if it doesn't have one.
/// </remarks>
public bool SetLength(Entity<UseDelayComponent?> ent, TimeSpan length, string id = DefaultId)
{ {
if (ent.Comp.Delays.TryGetValue(id, out var entry)) EnsureComp<UseDelayComponent>(ent.Owner, out var comp);
if (comp.Delays.TryGetValue(id, out var entry))
{ {
if (entry.Length == length) if (entry.Length == length)
return true; return true;
@@ -73,7 +78,7 @@ public sealed class UseDelaySystem : EntitySystem
} }
else else
{ {
ent.Comp.Delays.Add(id, new UseDelayInfo(length)); comp.Delays.Add(id, new UseDelayInfo(length));
} }
Dirty(ent); Dirty(ent);