Machine upgrade examine verb (#12119)
This commit is contained in:
@@ -1,16 +1,22 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Construction.Components;
|
||||
using Content.Server.Examine;
|
||||
using Content.Shared.Construction.Prototypes;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Construction;
|
||||
|
||||
public sealed partial class ConstructionSystem
|
||||
{
|
||||
[Dependency] private readonly ExamineSystem _examineSystem = default!;
|
||||
|
||||
private void InitializeMachines()
|
||||
{
|
||||
SubscribeLocalEvent<MachineComponent, ComponentInit>(OnMachineInit);
|
||||
SubscribeLocalEvent<MachineComponent, MapInitEvent>(OnMachineMapInit);
|
||||
SubscribeLocalEvent<MachineComponent, GetVerbsEvent<ExamineVerb>>(OnMachineExaminableVerb);
|
||||
}
|
||||
|
||||
private void OnMachineInit(EntityUid uid, MachineComponent component, ComponentInit args)
|
||||
@@ -25,6 +31,33 @@ public sealed partial class ConstructionSystem
|
||||
RefreshParts(component);
|
||||
}
|
||||
|
||||
private void OnMachineExaminableVerb(EntityUid uid, MachineComponent component, GetVerbsEvent<ExamineVerb> args)
|
||||
{
|
||||
if (!args.CanInteract || !args.CanAccess)
|
||||
return;
|
||||
|
||||
var markup = new FormattedMessage();
|
||||
RaiseLocalEvent(uid, new UpgradeExamineEvent(ref markup));
|
||||
if (markup.IsEmpty)
|
||||
return; // Not upgradable.
|
||||
|
||||
markup = FormattedMessage.FromMarkup(markup.ToMarkup().TrimEnd('\n')); // Cursed workaround to https://github.com/space-wizards/RobustToolbox/issues/3371
|
||||
|
||||
var verb = new ExamineVerb()
|
||||
{
|
||||
Act = () =>
|
||||
{
|
||||
_examineSystem.SendExamineTooltip(args.User, uid, markup, getVerbs: false, centerAtCursor: false);
|
||||
},
|
||||
Text = Loc.GetString("machine-examinable-verb-text"),
|
||||
Message = Loc.GetString("machine-examinable-verb-message"),
|
||||
Category = VerbCategory.Examine,
|
||||
IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png"
|
||||
};
|
||||
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
public List<MachinePartComponent> GetAllParts(MachineComponent component)
|
||||
{
|
||||
var parts = new List<MachinePartComponent>();
|
||||
@@ -143,3 +176,43 @@ public sealed class RefreshPartsEvent : EntityEventArgs
|
||||
|
||||
public Dictionary<string, float> PartRatings = new Dictionary<string, float>();
|
||||
}
|
||||
|
||||
public sealed class UpgradeExamineEvent : EntityEventArgs
|
||||
{
|
||||
private FormattedMessage Message;
|
||||
|
||||
public UpgradeExamineEvent(ref FormattedMessage message)
|
||||
{
|
||||
Message = message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a line to the upgrade examine tooltip with a percentage-based increase or decrease.
|
||||
/// </summary>
|
||||
public void AddPercentageUpgrade(string upgradedLocId, float multiplier)
|
||||
{
|
||||
var percent = Math.Round(100 * MathF.Abs(multiplier - 1), 2);
|
||||
var locId = multiplier switch {
|
||||
< 1 => "machine-upgrade-decreased-by-percentage",
|
||||
1 or float.NaN => "machine-upgrade-not-upgraded",
|
||||
> 1 => "machine-upgrade-increased-by-percentage",
|
||||
};
|
||||
var upgraded = Loc.GetString(upgradedLocId);
|
||||
this.Message.AddMarkup(Loc.GetString(locId, ("upgraded", upgraded), ("percent", percent)) + '\n');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a line to the upgrade examine tooltip with a numeric increase or decrease.
|
||||
/// </summary>
|
||||
public void AddNumberUpgrade(string upgradedLocId, int number)
|
||||
{
|
||||
var difference = Math.Abs(number);
|
||||
var locId = number switch {
|
||||
< 0 => "machine-upgrade-decreased-by-amount",
|
||||
0 => "machine-upgrade-not-upgraded",
|
||||
> 0 => "machine-upgrade-increased-by-amount",
|
||||
};
|
||||
var upgraded = Loc.GetString(upgradedLocId);
|
||||
this.Message.AddMarkup(Loc.GetString(locId, ("upgraded", upgraded), ("difference", difference)) + '\n');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user