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

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