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

@@ -8,6 +8,8 @@ namespace Content.Client.Lathe;
public sealed class LatheSystem : SharedLatheSystem
{
[Dependency] private readonly AppearanceSystem _appearance = default!;
public override void Initialize()
{
base.Initialize();
@@ -20,32 +22,19 @@ public sealed class LatheSystem : SharedLatheSystem
if (args.Sprite == null)
return;
if (args.Component.TryGetData(PowerDeviceVisuals.Powered, out bool powered) &&
if (_appearance.TryGetData(uid, PowerDeviceVisuals.Powered, out bool powered, args.Component) &&
args.Sprite.LayerMapTryGet(PowerDeviceVisualLayers.Powered, out _))
{
args.Sprite.LayerSetVisible(PowerDeviceVisualLayers.Powered, powered);
}
// Lathe specific stuff
if (args.Component.TryGetData(LatheVisuals.IsRunning, out bool isRunning))
if (_appearance.TryGetData(uid, LatheVisuals.IsRunning, out bool isRunning, args.Component))
{
var state = isRunning ? component.RunningState : component.IdleState;
args.Sprite.LayerSetAnimationTime(LatheVisualLayers.IsRunning, 0f);
args.Sprite.LayerSetState(LatheVisualLayers.IsRunning, state);
}
if (args.Component.TryGetData(LatheVisuals.IsInserting, out bool isInserting)
&& args.Sprite.LayerMapTryGet(LatheVisualLayers.IsInserting, out var isInsertingLayer))
{
if (args.Component.TryGetData(LatheVisuals.InsertingColor, out Color color)
&& !component.IgnoreColor)
{
args.Sprite.LayerSetColor(isInsertingLayer, color);
}
args.Sprite.LayerSetAnimationTime(isInsertingLayer, 0f);
args.Sprite.LayerSetVisible(isInsertingLayer, isInserting);
}
}
///<remarks>
@@ -60,6 +49,5 @@ public sealed class LatheSystem : SharedLatheSystem
public enum LatheVisualLayers : byte
{
IsRunning,
IsInserting
IsRunning
}

View File

@@ -3,13 +3,43 @@ using Robust.Client.GameObjects;
namespace Content.Client.Materials;
/// <summary>
/// This handles...
/// </summary>
public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
{
[Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly TransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MaterialStorageComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
private void OnAppearanceChange(EntityUid uid, MaterialStorageComponent component, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
return;
if (!args.Sprite.LayerMapTryGet(MaterialStorageVisualLayers.Inserting, out var layer))
return;
if (!_appearance.TryGetData(uid, MaterialStorageVisuals.Inserting, out bool inserting, args.Component))
return;
if (inserting && TryComp<InsertingMaterialStorageComponent>(uid, out var insertingComp))
{
args.Sprite.LayerSetAnimationTime(layer, 0f);
args.Sprite.LayerSetVisible(layer, true);
if (insertingComp.MaterialColor != null)
args.Sprite.LayerSetColor(layer, insertingComp.MaterialColor.Value);
}
else
{
args.Sprite.LayerSetVisible(layer, false);
}
}
public override bool TryInsertMaterialEntity(EntityUid user, EntityUid toInsert, EntityUid receiver, MaterialStorageComponent? component = null)
{
if (!base.TryInsertMaterialEntity(user, toInsert, receiver, component))
@@ -18,3 +48,8 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
return true;
}
}
public enum MaterialStorageVisualLayers : byte
{
Inserting
}

View File

@@ -1,15 +0,0 @@
namespace Content.Server.Lathe.Components
{
/// <summary>
/// For EntityQuery to keep track of which lathes are inserting
/// </summary>
[RegisterComponent]
public sealed class LatheInsertingComponent : Component
{
/// <summary>
/// Remaining insertion time, in seconds.
/// </summary>
[DataField("timeRemaining", required: true)]
public float TimeRemaining;
}
}

View File

@@ -33,7 +33,6 @@ namespace Content.Server.Lathe
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LatheComponent, MaterialEntityInsertedEvent>(OnMaterialEntityInserted);
SubscribeLocalEvent<LatheComponent, GetMaterialWhitelistEvent>(OnGetWhitelist);
SubscribeLocalEvent<LatheComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<LatheComponent, PowerChangedEvent>(OnPowerChanged);
@@ -45,24 +44,13 @@ namespace Content.Server.Lathe
SubscribeLocalEvent<LatheComponent, LatheSyncRequestMessage>(OnLatheSyncRequestMessage);
SubscribeLocalEvent<LatheComponent, BeforeActivatableUIOpenEvent>((u,c,_) => UpdateUserInterfaceState(u,c));
SubscribeLocalEvent<LatheComponent, MaterialAmountChangedEvent>((u,c,_) => UpdateUserInterfaceState(u,c));
SubscribeLocalEvent<LatheComponent, MaterialAmountChangedEvent>(OnMaterialAmountChanged);
SubscribeLocalEvent<TechnologyDatabaseComponent, LatheGetRecipesEvent>(OnGetRecipes);
}
public override void Update(float frameTime)
{
foreach (var comp in EntityQuery<LatheInsertingComponent>())
{
comp.TimeRemaining -= frameTime;
if (comp.TimeRemaining > 0)
continue;
UpdateInsertingAppearance(comp.Owner, false);
RemCompDeferred(comp.Owner, comp);
}
foreach (var (comp, lathe) in EntityQuery<LatheProducingComponent, LatheComponent>())
{
if (lathe.CurrentRecipe == null)
@@ -73,7 +61,7 @@ namespace Content.Server.Lathe
}
}
private void OnGetWhitelist(EntityUid uid, LatheComponent component, GetMaterialWhitelistEvent args)
private void OnGetWhitelist(EntityUid uid, LatheComponent component, ref GetMaterialWhitelistEvent args)
{
if (args.Storage != uid)
return;
@@ -203,6 +191,11 @@ namespace Content.Server.Lathe
args.Recipes = args.Recipes.Union(component.RecipeIds.Where(r => latheComponent.DynamicRecipes.Contains(r))).ToList();
}
private void OnMaterialAmountChanged(EntityUid uid, LatheComponent component, ref MaterialAmountChangedEvent args)
{
UpdateUserInterfaceState(uid, component);
}
/// <summary>
/// Initialize the UI and appearance.
/// Appearance requires initialization or the layers break
@@ -215,15 +208,6 @@ namespace Content.Server.Lathe
_materialStorage.UpdateMaterialWhitelist(uid);
}
private void OnMaterialEntityInserted(EntityUid uid, LatheComponent component, MaterialEntityInsertedEvent args)
{
var lastMat = args.MaterialComp.Materials.Keys.Last();
// We need the prototype to get the color
_proto.TryIndex(lastMat, out MaterialPrototype? matProto);
EnsureComp<LatheInsertingComponent>(uid).TimeRemaining = component.InsertionTime;
UpdateInsertingAppearance(uid, true, matProto?.Color);
}
/// <summary>
/// Sets the machine sprite to either play the running animation
/// or stop.
@@ -233,17 +217,6 @@ namespace Content.Server.Lathe
_appearance.SetData(uid, LatheVisuals.IsRunning, isRunning);
}
/// <summary>
/// Sets the machine sprite to play the inserting animation
/// and sets the color of the inserted mat if applicable
/// </summary>
private void UpdateInsertingAppearance(EntityUid uid, bool isInserting, Color? color = null)
{
_appearance.SetData(uid, LatheVisuals.IsInserting, isInserting);
if (color != null)
_appearance.SetData(uid, LatheVisuals.InsertingColor, color);
}
private void OnPowerChanged(EntityUid uid, LatheComponent component, ref PowerChangedEvent args)
{
if (!args.Powered)
@@ -297,8 +270,10 @@ namespace Content.Server.Lathe
count++;
}
if (count > 0 && args.Session.AttachedEntity != null)
{
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(args.Session.AttachedEntity.Value):player} queued {count} {recipe.Name} at {ToPrettyString(uid):lathe}");
}
}
TryStartProducing(uid, component);
UpdateUserInterfaceState(uid, component);

View File

@@ -54,7 +54,6 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
var count = stack?.Count ?? 1;
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(user):player} inserted {count} {ToPrettyString(toInsert):inserted} into {ToPrettyString(receiver):receiver}");
return true;
}
}

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);
}

View File

@@ -15,7 +15,7 @@
shader: unshaded
map: ["enum.PowerDeviceVisualLayers.Powered"]
- state: inserting
map: ["enum.LatheVisualLayers.IsInserting"]
map: ["enum.MaterialStorageVisualLayers.Inserting"]
- state: panel
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- type: Appearance
@@ -102,7 +102,7 @@
shader: unshaded
map: ["enum.PowerDeviceVisualLayers.Powered"]
- state: inserting
map: ["enum.LatheVisualLayers.IsInserting"]
map: ["enum.MaterialStorageVisualLayers.Inserting"]
- state: panel
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- type: Appearance
@@ -335,7 +335,7 @@
- state: fab-idle
map: ["enum.LatheVisualLayers.IsRunning"]
- state: fab-load
map: ["enum.LatheVisualLayers.IsInserting"]
map: ["enum.MaterialStorageVisualLayers.Inserting"]
- state: fab-o
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- type: Machine
@@ -376,7 +376,7 @@
shader: unshaded
map: ["enum.PowerDeviceVisualLayers.Powered"]
- state: inserting
map: ["enum.LatheVisualLayers.IsInserting"]
map: ["enum.MaterialStorageVisualLayers.Inserting"]
- state: panel
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- type: Machine
@@ -432,7 +432,7 @@
shader: unshaded
map: ["enum.PowerDeviceVisualLayers.Powered"]
- state: inserting
map: ["enum.LatheVisualLayers.IsInserting"]
map: ["enum.MaterialStorageVisualLayers.Inserting"]
- state: panel
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- type: Lathe
@@ -579,18 +579,18 @@
shader: unshaded
map: ["enum.PowerDeviceVisualLayers.Powered"]
- state: inserting
map: ["enum.LatheVisualLayers.IsInserting"]
map: ["enum.MaterialStorageVisualLayers.Inserting"]
- state: panel
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- type: Machine
board: OreProcessorMachineCircuitboard
- type: MaterialStorage
dropOnDeconstruct: false #should drop ores instead of ingots/sheets
ignoreColor: true
whitelist:
tags:
- Ore
- type: Lathe
ignoreColor: true
staticRecipes:
- SheetSteel30
- SheetGlass30