decouple material insertion visualization from lathes (#13242)

This commit is contained in:
Nemanja
2023-01-07 21:36:50 -05:00
committed by GitHub
parent 1f5bae751f
commit 26786b5839
10 changed files with 184 additions and 108 deletions

View File

@@ -28,12 +28,6 @@ namespace Content.Shared.Lathe
[DataField("queue")]
public List<LatheRecipePrototype> Queue = new();
/// <summary>
/// How long the inserting animation will play
/// </summary>
[DataField("insertionTime")]
public float InsertionTime = 0.79f; // 0.01 off for animation timing
/// <summary>
/// The sound that plays when the lathe is producing an item, if any
/// </summary>
@@ -46,9 +40,6 @@ namespace Content.Shared.Lathe
[DataField("runningState", required: true)]
public string RunningState = default!;
[DataField("ignoreColor")]
public bool IgnoreColor;
#endregion
/// <summary>

View File

@@ -0,0 +1,31 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.Materials;
[RegisterComponent, NetworkedComponent]
public sealed class InsertingMaterialStorageComponent : Component
{
/// <summary>
/// The time when insertion ends.
/// </summary>
[DataField("endTime", customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite)]
public TimeSpan EndTime;
[ViewVariables]
public Color? MaterialColor;
}
[Serializable, NetSerializable]
public sealed class InsertingMaterialStorageComponentState : ComponentState
{
public TimeSpan EndTime;
public Color? MaterialColor;
public InsertingMaterialStorageComponentState(TimeSpan endTime, Color? materialColor)
{
EndTime = endTime;
MaterialColor = materialColor;
}
}

View File

@@ -2,6 +2,7 @@ using Content.Shared.Whitelist;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
@@ -38,44 +39,56 @@ public sealed class MaterialStorageComponent : Component
[DataField("materialWhiteList", customTypeSerializer: typeof(PrototypeIdListSerializer<MaterialPrototype>))]
public List<string>? MaterialWhiteList;
/// <summary>
/// Whether or not the visualization for the insertion animation
/// should ignore the color of the material being inserted.
/// </summary>
[DataField("ignoreColor")]
public bool IgnoreColor;
/// <summary>
/// The sound that plays when inserting an item into the storage
/// </summary>
[DataField("insertingSound")]
public SoundSpecifier? InsertingSound;
/// <summary>
/// How long the inserting animation will play
/// </summary>
[DataField("insertionTime", customTypeSerializer: typeof(TimeOffsetSerializer))]
public TimeSpan InsertionTime = TimeSpan.FromSeconds(0.79f); // 0.01 off for animation timing
}
[Serializable, NetSerializable]
public enum MaterialStorageVisuals : byte
{
Inserting
}
/// <summary>
/// event raised on the materialStorage when a material entity is inserted into it.
/// </summary>
public readonly struct MaterialEntityInsertedEvent
[ByRefEvent]
public readonly record struct MaterialEntityInsertedEvent(MaterialComponent MaterialComp)
{
public readonly MaterialComponent MaterialComp;
public MaterialEntityInsertedEvent(MaterialComponent materials)
{
MaterialComp = materials;
}
public readonly MaterialComponent MaterialComp = MaterialComp;
}
/// <summary>
/// Event raised when a material amount is changed
/// </summary>
public readonly struct MaterialAmountChangedEvent
{
[ByRefEvent]
public readonly record struct MaterialAmountChangedEvent;
}
public sealed class GetMaterialWhitelistEvent : EntityEventArgs
/// <summary>
/// Event raised to get all the materials that the
/// </summary>
[ByRefEvent]
public record struct GetMaterialWhitelistEvent(EntityUid Storage)
{
public readonly EntityUid Storage;
public readonly EntityUid Storage = Storage;
public List<string> Whitelist = new();
public GetMaterialWhitelistEvent(EntityUid storage)
{
Storage = storage;
}
}
[Serializable, NetSerializable]

View File

@@ -1,8 +1,10 @@
using System.Linq;
using System.Linq;
using Content.Shared.Interaction;
using Content.Shared.Stacks;
using JetBrains.Annotations;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
namespace Content.Shared.Materials;
@@ -12,14 +14,40 @@ namespace Content.Shared.Materials;
/// </summary>
public abstract class SharedMaterialStorageSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MaterialStorageComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<MaterialStorageComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<MaterialStorageComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<MaterialStorageComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<InsertingMaterialStorageComponent, ComponentGetState>(OnGetInsertingState);
SubscribeLocalEvent<InsertingMaterialStorageComponent, ComponentHandleState>(OnHandleInsertingState);
SubscribeLocalEvent<InsertingMaterialStorageComponent, EntityUnpausedEvent>(OnUnpaused);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var inserting in EntityQuery<InsertingMaterialStorageComponent>())
{
if (_timing.CurTime < inserting.EndTime)
continue;
_appearance.SetData(inserting.Owner, MaterialStorageVisuals.Inserting, false);
RemComp(inserting.Owner, inserting);
}
}
private void OnMapInit(EntityUid uid, MaterialStorageComponent component, MapInitEvent args)
{
_appearance.SetData(uid, MaterialStorageVisuals.Inserting, false);
}
private void OnGetState(EntityUid uid, MaterialStorageComponent component, ref ComponentGetState args)
@@ -38,6 +66,25 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
component.MaterialWhiteList = new List<string>(state.MaterialWhitelist);
}
private void OnGetInsertingState(EntityUid uid, InsertingMaterialStorageComponent component, ref ComponentGetState args)
{
args.State = new InsertingMaterialStorageComponentState(component.EndTime, component.MaterialColor);
}
private void OnHandleInsertingState(EntityUid uid, InsertingMaterialStorageComponent component, ref ComponentHandleState args)
{
if (args.Current is not InsertingMaterialStorageComponentState state)
return;
component.EndTime = state.EndTime;
component.MaterialColor = state.MaterialColor;
}
private void OnUnpaused(EntityUid uid, InsertingMaterialStorageComponent component, ref EntityUnpausedEvent args)
{
component.EndTime += args.PausedTime;
}
/// <summary>
/// Gets the volume of a specified material contained in this storage.
/// </summary>
@@ -128,7 +175,8 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
component.Storage.Add(materialId, 0);
component.Storage[materialId] += volume;
RaiseLocalEvent(uid, new MaterialAmountChangedEvent());
var ev = new MaterialAmountChangedEvent();
RaiseLocalEvent(uid, ref ev);
Dirty(component);
return true;
}
@@ -171,7 +219,18 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
TryChangeMaterialAmount(receiver, mat, vol * multiplier, component);
}
RaiseLocalEvent(component.Owner, new MaterialEntityInsertedEvent(material));
var insertingComp = EnsureComp<InsertingMaterialStorageComponent>(receiver);
insertingComp.EndTime = _timing.CurTime + component.InsertionTime;
if (!component.IgnoreColor)
{
_prototype.TryIndex<MaterialPrototype>(material.Materials.Keys.Last(), out var lastMat);
insertingComp.MaterialColor = lastMat?.Color;
}
_appearance.SetData(receiver, MaterialStorageVisuals.Inserting, true);
Dirty(insertingComp);
var ev = new MaterialEntityInsertedEvent(material);
RaiseLocalEvent(component.Owner, ref ev);
return true;
}
@@ -186,7 +245,7 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
if (!Resolve(uid, ref component, false))
return;
var ev = new GetMaterialWhitelistEvent(uid);
RaiseLocalEvent(uid, ev);
RaiseLocalEvent(uid, ref ev);
component.MaterialWhiteList = ev.Whitelist;
Dirty(component);
}