Fix item/clothing visual & networking bugs (#10116)

This commit is contained in:
Leon Friedrich
2022-07-29 13:02:09 +12:00
committed by GitHub
parent e22679501c
commit d279f6172a
16 changed files with 278 additions and 239 deletions

View File

@@ -0,0 +1,76 @@
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Sound;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Light
{
[NetworkedComponent]
[RegisterComponent]
[Access(typeof(SharedHandheldLightSystem))]
public sealed class HandheldLightComponent : Robust.Shared.GameObjects.Component
{
public byte? Level;
public bool Activated;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("wattage")]
public float Wattage { get; set; } = .8f;
[DataField("turnOnSound")]
public SoundSpecifier TurnOnSound = new SoundPathSpecifier("/Audio/Items/flashlight_on.ogg");
[DataField("turnOnFailSound")]
public SoundSpecifier TurnOnFailSound = new SoundPathSpecifier("/Audio/Machines/button.ogg");
[DataField("turnOffSound")]
public SoundSpecifier TurnOffSound = new SoundPathSpecifier("/Audio/Items/flashlight_off.ogg");
/// <summary>
/// Whether to automatically set item-prefixes when toggling the flashlight.
/// </summary>
/// <remarks>
/// Flashlights should probably be using explicit unshaded sprite, in-hand and clothing layers, this is
/// mostly here for backwards compatibility.
/// </remarks>
[DataField("addPrefix")]
public bool AddPrefix = false;
[DataField("toggleActionId", customTypeSerializer: typeof(PrototypeIdSerializer<InstantActionPrototype>))]
public string ToggleActionId = "ToggleLight";
[DataField("toggleAction")]
public InstantAction? ToggleAction;
public const int StatusLevels = 6;
[Serializable, NetSerializable]
public sealed class HandheldLightComponentState : ComponentState
{
public byte? Charge { get; }
public bool Activated { get; }
public HandheldLightComponentState(bool activated, byte? charge)
{
Activated = activated;
Charge = charge;
}
}
}
[Serializable, NetSerializable]
public enum HandheldLightVisuals
{
Power
}
[Serializable, NetSerializable]
public enum HandheldLightPowerStates
{
FullPower,
LowPower,
Dying,
}
}

View File

@@ -1,49 +0,0 @@
using Content.Shared.Actions.ActionTypes;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Light.Component
{
[NetworkedComponent]
public abstract class SharedHandheldLightComponent : Robust.Shared.GameObjects.Component
{
[DataField("toggleActionId", customTypeSerializer:typeof(PrototypeIdSerializer<InstantActionPrototype>))]
public string ToggleActionId = "ToggleLight";
[DataField("toggleAction")]
public InstantAction? ToggleAction;
public const int StatusLevels = 6;
[Serializable, NetSerializable]
public sealed class HandheldLightComponentState : ComponentState
{
public byte? Charge { get; }
public bool Activated { get; }
public HandheldLightComponentState(bool activated, byte? charge)
{
Activated = activated;
Charge = charge;
}
}
}
[Serializable, NetSerializable]
public enum HandheldLightVisuals
{
Power
}
[Serializable, NetSerializable]
public enum HandheldLightPowerStates
{
FullPower,
LowPower,
Dying,
}
}

View File

@@ -0,0 +1,79 @@
using Content.Shared.Actions;
using Content.Shared.Clothing.EntitySystems;
using Content.Shared.Item;
using Content.Shared.Toggleable;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Player;
using Robust.Shared.Utility;
namespace Content.Shared.Light;
public abstract class SharedHandheldLightSystem : EntitySystem
{
[Dependency] private readonly SharedItemSystem _itemSys = default!;
[Dependency] private readonly ClothingSystem _clothingSys = default!;
[Dependency] private readonly SharedActionsSystem _actionSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HandheldLightComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<HandheldLightComponent, ComponentHandleState>(OnHandleState);
}
private void OnInit(EntityUid uid, HandheldLightComponent component, ComponentInit args)
{
UpdateVisuals(uid, component);
// Want to make sure client has latest data on level so battery displays properly.
Dirty(component);
}
private void OnHandleState(EntityUid uid, HandheldLightComponent component, ref ComponentHandleState args)
{
if (args.Current is not HandheldLightComponent.HandheldLightComponentState state)
return;
component.Level = state.Charge;
SetActivated(uid, state.Activated, component, false);
}
public void SetActivated(EntityUid uid, bool activated, HandheldLightComponent? component = null, bool makeNoise = true)
{
if (!Resolve(uid, ref component))
return;
if (component.Activated == activated)
return;
component.Activated = activated;
if (makeNoise)
{
var sound = component.Activated ? component.TurnOnSound : component.TurnOffSound;
SoundSystem.Play(sound.GetSound(), Filter.Pvs(component.Owner, entityManager: EntityManager), component.Owner);
}
Dirty(component);
UpdateVisuals(uid, component);
}
public void UpdateVisuals(EntityUid uid, HandheldLightComponent? component = null, AppearanceComponent? appearance = null)
{
if (!Resolve(uid, ref component, ref appearance, false))
return;
if (component.AddPrefix)
{
var prefix = component.Activated ? "on" : "off";
_itemSys.SetHeldPrefix(uid, prefix);
_clothingSys.SetEquippedPrefix(uid, prefix);
}
if (component.ToggleAction != null)
_actionSystem.SetToggled(component.ToggleAction, component.Activated);
appearance.SetData(ToggleableLightVisuals.Enabled, component.Activated);
}
}