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:
@@ -0,0 +1,66 @@
|
||||
using Content.Shared.Clothing.EntitySystems;
|
||||
using Content.Shared.Inventory;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Shared.Clothing.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Allow players to change clothing sprite to any other clothing prototype.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
[Access(typeof(SharedChameleonClothingSystem))]
|
||||
public sealed class ChameleonClothingComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Filter possible chameleon options by their slot flag.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadOnly)]
|
||||
[DataField("slot", required: true)]
|
||||
public SlotFlags Slot;
|
||||
|
||||
/// <summary>
|
||||
/// EntityPrototype id that chameleon item is trying to mimic.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadOnly)]
|
||||
[DataField("default", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
public string? SelectedId;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class ChameleonClothingComponentState : ComponentState
|
||||
{
|
||||
public string? SelectedId;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class ChameleonBoundUserInterfaceState : BoundUserInterfaceState
|
||||
{
|
||||
public readonly SlotFlags Slot;
|
||||
public readonly string? SelectedId;
|
||||
|
||||
public ChameleonBoundUserInterfaceState(SlotFlags slot, string? selectedId)
|
||||
{
|
||||
Slot = slot;
|
||||
SelectedId = selectedId;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class ChameleonPrototypeSelectedMessage: BoundUserInterfaceMessage
|
||||
{
|
||||
public readonly string SelectedId;
|
||||
|
||||
public ChameleonPrototypeSelectedMessage(string selectedId)
|
||||
{
|
||||
SelectedId = selectedId;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum ChameleonUiKey : byte
|
||||
{
|
||||
Key
|
||||
}
|
||||
@@ -42,6 +42,10 @@ public abstract class SharedClothingComponent : Component
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("sprite")]
|
||||
public string? RsiPath;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("femaleMask")]
|
||||
public FemaleClothingMask FemaleMask = FemaleClothingMask.UniformFull;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
@@ -54,3 +58,10 @@ public sealed class ClothingComponentState : ComponentState
|
||||
EquippedPrefix = equippedPrefix;
|
||||
}
|
||||
}
|
||||
|
||||
public enum FemaleClothingMask : byte
|
||||
{
|
||||
NoMask = 0,
|
||||
UniformFull,
|
||||
UniformTop
|
||||
}
|
||||
|
||||
@@ -52,5 +52,22 @@ public sealed class ClothingSystem : EntitySystem
|
||||
Dirty(clothing);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy all clothing specific visuals from another item.
|
||||
/// </summary>
|
||||
public void CopyVisuals(EntityUid uid, SharedClothingComponent otherClothing, SharedClothingComponent? clothing = null)
|
||||
{
|
||||
if (!Resolve(uid, ref clothing))
|
||||
return;
|
||||
|
||||
clothing.ClothingVisuals = otherClothing.ClothingVisuals;
|
||||
clothing.EquippedPrefix = otherClothing.EquippedPrefix;
|
||||
clothing.RsiPath = otherClothing.RsiPath;
|
||||
clothing.FemaleMask = otherClothing.FemaleMask;
|
||||
|
||||
_itemSys.VisualsChanged(uid);
|
||||
Dirty(clothing);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
using Content.Shared.Clothing.Components;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Clothing.EntitySystems;
|
||||
|
||||
public abstract class SharedChameleonClothingSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IComponentFactory _factory = default!;
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
[Dependency] private readonly SharedItemSystem _itemSystem = default!;
|
||||
[Dependency] private readonly ClothingSystem _clothingSystem = default!;
|
||||
|
||||
// Updates chameleon visuals and meta information.
|
||||
// This function is called on a server after user selected new outfit.
|
||||
// And after that on a client after state was updated.
|
||||
// This 100% makes sure that server and client have exactly same data.
|
||||
protected void UpdateVisuals(EntityUid uid, ChameleonClothingComponent component)
|
||||
{
|
||||
if (string.IsNullOrEmpty(component.SelectedId) ||
|
||||
!_proto.TryIndex(component.SelectedId, out EntityPrototype? proto))
|
||||
return;
|
||||
|
||||
// world sprite icon
|
||||
UpdateSprite(uid, proto);
|
||||
|
||||
// copy name and description
|
||||
var meta = MetaData(uid);
|
||||
meta.EntityName = proto.Name;
|
||||
meta.EntityDescription = proto.Description;
|
||||
|
||||
// item sprite logic
|
||||
if (TryComp(uid, out ItemComponent? item) &&
|
||||
proto.TryGetComponent(out ItemComponent? otherItem, _factory))
|
||||
{
|
||||
_itemSystem.CopyVisuals(uid, otherItem, item);
|
||||
}
|
||||
|
||||
// clothing sprite logic
|
||||
if (TryComp(uid, out SharedClothingComponent? clothing) &&
|
||||
proto.TryGetComponent("Clothing", out SharedClothingComponent? otherClothing))
|
||||
{
|
||||
_clothingSystem.CopyVisuals(uid, otherClothing, clothing);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void UpdateSprite(EntityUid uid, EntityPrototype proto) { }
|
||||
|
||||
/// <summary>
|
||||
/// Check if this entity prototype is valid target for chameleon item.
|
||||
/// </summary>
|
||||
public bool IsValidTarget(EntityPrototype proto, SlotFlags chameleonSlot = SlotFlags.NONE)
|
||||
{
|
||||
// check if entity is valid
|
||||
if (proto.Abstract || proto.NoSpawn)
|
||||
return false;
|
||||
|
||||
// check if it is marked as valid chameleon target
|
||||
if (!proto.TryGetComponent(out TagComponent? tags, _factory) || !tags.Tags.Contains("WhitelistChameleon"))
|
||||
return false;
|
||||
|
||||
// check if it's valid clothing
|
||||
if (!proto.TryGetComponent("Clothing", out SharedClothingComponent? clothing))
|
||||
return false;
|
||||
if (!clothing.Slots.HasFlag(chameleonSlot))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user