Chameleon clothing (#8444)
Co-authored-by: Moony <moonheart08@users.noreply.github.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
98
Content.Server/Clothing/Systems/ChameleonClothingSystem.cs
Normal file
98
Content.Server/Clothing/Systems/ChameleonClothingSystem.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using Content.Shared.Clothing.Components;
|
||||
using Content.Shared.Clothing.EntitySystems;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Clothing.Systems;
|
||||
|
||||
public sealed class ChameleonClothingSystem : SharedChameleonClothingSystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ChameleonClothingComponent, ComponentInit>(OnInit);
|
||||
SubscribeLocalEvent<ChameleonClothingComponent, ComponentGetState>(GetState);
|
||||
SubscribeLocalEvent<ChameleonClothingComponent, GetVerbsEvent<InteractionVerb>>(OnVerb);
|
||||
SubscribeLocalEvent<ChameleonClothingComponent, ChameleonPrototypeSelectedMessage>(OnSelected);
|
||||
}
|
||||
|
||||
private void OnInit(EntityUid uid, ChameleonClothingComponent component, ComponentInit args)
|
||||
{
|
||||
SetSelectedPrototype(uid, component.SelectedId, true, component);
|
||||
}
|
||||
|
||||
private void GetState(EntityUid uid, ChameleonClothingComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new ChameleonClothingComponentState
|
||||
{
|
||||
SelectedId = component.SelectedId
|
||||
};
|
||||
}
|
||||
|
||||
private void OnVerb(EntityUid uid, ChameleonClothingComponent component, GetVerbsEvent<InteractionVerb> args)
|
||||
{
|
||||
if (!args.CanAccess || !args.CanInteract)
|
||||
return;
|
||||
|
||||
args.Verbs.Add(new InteractionVerb()
|
||||
{
|
||||
Text = Loc.GetString("chameleon-component-verb-text"),
|
||||
IconTexture = "/Textures/Interface/VerbIcons/settings.svg.192dpi.png",
|
||||
Act = () => TryOpenUi(uid, args.User, component)
|
||||
});
|
||||
}
|
||||
|
||||
private void OnSelected(EntityUid uid, ChameleonClothingComponent component, ChameleonPrototypeSelectedMessage args)
|
||||
{
|
||||
SetSelectedPrototype(uid, args.SelectedId, component: component);
|
||||
}
|
||||
|
||||
private void TryOpenUi(EntityUid uid, EntityUid user, ChameleonClothingComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
if (!TryComp(user, out ActorComponent? actor))
|
||||
return;
|
||||
_uiSystem.TryToggleUi(uid, ChameleonUiKey.Key, actor.PlayerSession);
|
||||
}
|
||||
|
||||
private void UpdateUi(EntityUid uid, ChameleonClothingComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
var state = new ChameleonBoundUserInterfaceState(component.Slot, component.SelectedId);
|
||||
_uiSystem.TrySetUiState(uid, ChameleonUiKey.Key, state);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change chameleon items name, description and sprite to mimic other entity prototype.
|
||||
/// </summary>
|
||||
public void SetSelectedPrototype(EntityUid uid, string? protoId, bool forceUpdate = false,
|
||||
ChameleonClothingComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component, false))
|
||||
return;
|
||||
|
||||
// check that wasn't already selected
|
||||
// forceUpdate on component init ignores this check
|
||||
if (component.SelectedId == protoId && !forceUpdate)
|
||||
return;
|
||||
|
||||
// make sure that it is valid change
|
||||
if (string.IsNullOrEmpty(protoId) || !_proto.TryIndex(protoId, out EntityPrototype? proto))
|
||||
return;
|
||||
if (!IsValidTarget(proto, component.Slot))
|
||||
return;
|
||||
component.SelectedId = protoId;
|
||||
|
||||
UpdateVisuals(uid, component);
|
||||
UpdateUi(uid, component);
|
||||
Dirty(component);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user