AutoCompState + ItemToggle fixes (#23422)

* AutoCompState + ItemToggle fixes

Fix a lot of the comp states that are never actually networked and also cleaned up ItemToggle events a bunch. ItemToggle will still need some future work for lights and sounds.

* Also catch these
This commit is contained in:
metalgearsloth
2024-01-03 17:24:02 +11:00
committed by GitHub
parent 2548156eae
commit 2166958bd0
29 changed files with 160 additions and 294 deletions

View File

@@ -12,10 +12,10 @@ namespace Content.Shared.Item;
/// </summary>
[RegisterComponent]
[NetworkedComponent]
[Access(typeof(SharedItemSystem))]
[Access(typeof(SharedItemSystem)), AutoGenerateComponentState(true)]
public sealed partial class ItemComponent : Component
{
[DataField, ViewVariables(VVAccess.ReadWrite)]
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
[Access(typeof(SharedItemSystem))]
public ProtoId<ItemSizePrototype> Size = "Small";
@@ -25,7 +25,7 @@ public sealed partial class ItemComponent : Component
[Access(typeof(SharedItemSystem))]
[ViewVariables(VVAccess.ReadWrite)]
[DataField]
[DataField, AutoNetworkedField]
public string? HeldPrefix;
/// <summary>
@@ -56,19 +56,6 @@ public sealed partial class ItemComponent : Component
public float StoredRotation = 0;
}
[Serializable, NetSerializable]
public sealed class ItemComponentState : ComponentState
{
public ProtoId<ItemSizePrototype> Size { get; }
public string? HeldPrefix { get; }
public ItemComponentState(ProtoId<ItemSizePrototype> size, string? heldPrefix)
{
Size = size;
HeldPrefix = heldPrefix;
}
}
/// <summary>
/// Raised when an item's visual state is changed. The event is directed at the entity that contains this item, so
/// that it can properly update its hands or inventory sprites and GUI.

View File

@@ -1,16 +1,16 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Item;
namespace Content.Shared.Item.ItemToggle.Components;
/// <summary>
/// Handles the active sound being played continuously with some items that are activated (ie e-sword hum).
/// </summary>
[RegisterComponent, NetworkedComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ItemToggleActiveSoundComponent : Component
{
/// <summary>
/// The continuous noise this item makes when it's activated (like an e-sword's hum). This loops.
/// The continuous noise this item makes when it's activated (like an e-sword's hum).
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField]
public SoundSpecifier? ActiveSound;
@@ -18,17 +18,6 @@ public sealed partial class ItemToggleActiveSoundComponent : Component
/// <summary>
/// Used when the item emits sound while active.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField]
[ViewVariables(VVAccess.ReadWrite), DataField]
public EntityUid? PlayingStream;
}
/// <summary>
/// Raised in order to effect changes upon the ActiveSound of the entity.
/// </summary>
[ByRefEvent]
public record struct ItemToggleActiveSoundUpdateEvent(bool Activated, bool Predicted, EntityUid? User)
{
public bool Activated = Activated;
public bool Predicted = Predicted;
public EntityUid? User = User;
}

View File

@@ -1,7 +1,7 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Item;
namespace Content.Shared.Item.ItemToggle.Components;
/// <summary>
/// Handles generic item toggles, like a welder turning on and off, or an e-sword.
@@ -54,7 +54,7 @@ public sealed partial class ItemToggleComponent : Component
public record struct ItemToggleActivateAttemptEvent(EntityUid? User)
{
public bool Cancelled = false;
public EntityUid? User = User;
public readonly EntityUid? User = User;
}
/// <summary>
@@ -64,63 +64,34 @@ public record struct ItemToggleActivateAttemptEvent(EntityUid? User)
public record struct ItemToggleDeactivateAttemptEvent(EntityUid? User)
{
public bool Cancelled = false;
public EntityUid? User = User;
public readonly EntityUid? User = User;
}
/// <summary>
/// Raised directed on an entity any sort of toggle is complete.
/// </summary>
[ByRefEvent]
public record struct ItemToggleDoneEvent(bool Activated, EntityUid? User)
public readonly record struct ItemToggledEvent(bool Predicted, bool Activated, EntityUid? User)
{
public bool Activated = Activated;
public EntityUid? User = User;
public readonly bool Predicted = Predicted;
public readonly bool Activated = Activated;
public readonly EntityUid? User = User;
}
/// <summary>
/// Raised in order to play a toggle sound effect.
/// Raised directed on an entity when an itemtoggle is activated.
/// </summary>
[ByRefEvent]
public record struct ItemTogglePlayToggleSoundEvent(bool Activated, bool Predicted, EntityUid? User)
public readonly record struct ItemToggleActivatedEvent(EntityUid? User)
{
public bool Activated = Activated;
public bool Predicted = Predicted;
public EntityUid? User = User;
public readonly EntityUid? User = User;
}
/// <summary>
/// Raised in order to play a failure to toggle sound effect.
/// Raised directed on an entity when an itemtoggle is deactivated.
/// </summary>
[ByRefEvent]
public record struct ItemTogglePlayFailSoundEvent(bool Predicted, EntityUid? User)
public readonly record struct ItemToggleDeactivatedEvent(EntityUid? User)
{
public bool Predicted = Predicted;
public EntityUid? User = User;
}
/// <summary>
/// Raised in order to effect changes upon the Light component of the entity.
/// </summary>
[ByRefEvent]
public record struct ItemToggleLightUpdateEvent(bool Activated)
{
public bool Activated = Activated;
}
/// <summary>
/// Raised in order to effect changes upon the Appearance component of the entity.
/// </summary>
[ByRefEvent]
public record struct ItemToggleAppearanceUpdateEvent(bool Activated)
{
public bool Activated = Activated;
}
/// <summary>
/// Raised in order to effect changes upon the Reflect component of the entity.
/// </summary>
[ByRefEvent]
public record struct ItemToggleReflectUpdateEvent(bool Activated)
{
public bool Activated = Activated;
public readonly EntityUid? User = User;
}

View File

@@ -1,11 +1,11 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Item;
namespace Content.Shared.Item.ItemToggle.Components;
/// <summary>
/// Handles whether the item is hot when toggled on.
/// Handles whether the item is hot when toggled on.
/// </summary>
[RegisterComponent, NetworkedComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ItemToggleHotComponent : Component
{
/// <summary>

View File

@@ -1,16 +1,16 @@
using Robust.Shared.GameStates;
using Robust.Shared.Audio;
using Content.Shared.Damage;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Item;
namespace Content.Shared.Item.ItemToggle.Components;
/// <summary>
/// Handles the changes to the melee weapon component when the item is toggled.
/// Handles the changes to the melee weapon component when the item is toggled.
/// </summary>
/// <remarks>
/// You can change the damage, sound on hit, on swing, as well as hidden status while activated.
/// </remarks>
[RegisterComponent, NetworkedComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ItemToggleMeleeWeaponComponent : Component
{
/// <summary>
@@ -67,13 +67,3 @@ public sealed partial class ItemToggleMeleeWeaponComponent : Component
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField]
public bool DeactivatedSecret = false;
}
/// <summary>
/// Raised in order to effect changes upon the MeleeWeaponComponent of the entity.
/// </summary>
[ByRefEvent]
public record struct ItemToggleMeleeWeaponUpdateEvent(bool Activated)
{
public bool Activated = Activated;
}

View File

@@ -1,15 +1,15 @@
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.Item;
namespace Content.Shared.Item.ItemToggle.Components;
/// <summary>
/// Handles the changes to the item size when toggled.
/// Handles the changes to the item size when toggled.
/// </summary>
/// <remarks>
/// You can change the size when activated or not. By default the sizes are copied from the item.
/// </remarks>
[RegisterComponent, NetworkedComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ItemToggleSizeComponent : Component
{
/// <summary>
@@ -24,12 +24,3 @@ public sealed partial class ItemToggleSizeComponent : Component
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField]
public ProtoId<ItemSizePrototype>? DeactivatedSize = null;
}
/// <summary>
/// Raised in order to effect changes upon the MeleeWeaponComponent of the entity.
/// </summary>
[ByRefEvent]
public record struct ItemToggleSizeUpdateEvent(bool Activated)
{
public bool Activated = Activated;
}

View File

@@ -1,11 +1,10 @@
using Content.Shared.Interaction.Events;
using Content.Shared.Toggleable;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.Temperature;
using Content.Shared.Toggleable;
using Content.Shared.Wieldable;
using Content.Shared.Wieldable.Components;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Prototypes;
using Robust.Shared.Network;
namespace Content.Shared.Item.ItemToggle;
@@ -29,12 +28,10 @@ public abstract class SharedItemToggleSystem : EntitySystem
SubscribeLocalEvent<ItemToggleComponent, ItemUnwieldedEvent>(TurnOffonUnwielded);
SubscribeLocalEvent<ItemToggleComponent, ItemWieldedEvent>(TurnOnonWielded);
SubscribeLocalEvent<ItemToggleComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<ItemToggleHotComponent, IsHotEvent>(OnIsHotEvent);
SubscribeLocalEvent<ItemToggleActiveSoundComponent, ItemToggleActiveSoundUpdateEvent>(UpdateActiveSound);
SubscribeLocalEvent<AppearanceComponent, ItemToggleAppearanceUpdateEvent>(UpdateAppearance);
SubscribeLocalEvent<ItemToggleComponent, ItemToggleLightUpdateEvent>(UpdateLight);
SubscribeLocalEvent<ItemToggleComponent, ItemTogglePlayToggleSoundEvent>(PlayToggleSound);
SubscribeLocalEvent<ItemToggleComponent, ItemTogglePlayFailSoundEvent>(PlayFailToggleSound);
SubscribeLocalEvent<ItemToggleActiveSoundComponent, ItemToggledEvent>(UpdateActiveSound);
}
private void OnUseInHand(EntityUid uid, ItemToggleComponent itemToggle, UseInHandEvent args)
@@ -48,7 +45,7 @@ public abstract class SharedItemToggleSystem : EntitySystem
}
/// <summary>
/// Used when an item is attempted to be toggled.
/// Used when an item is attempted to be toggled.
/// </summary>
public void Toggle(EntityUid uid, EntityUid? user = null, bool predicted = true, ItemToggleComponent? itemToggle = null)
{
@@ -81,9 +78,10 @@ public abstract class SharedItemToggleSystem : EntitySystem
if (attempt.Cancelled)
{
//Raises the event to play the failure to activate noise.
var evPlayFailToggleSound = new ItemTogglePlayFailSoundEvent(Predicted: predicted, user);
RaiseLocalEvent(uid, ref evPlayFailToggleSound);
if (predicted)
_audio.PlayPredicted(itemToggle.SoundFailToActivate, uid, user);
else
_audio.PlayPvs(itemToggle.SoundFailToActivate, uid);
return false;
}
@@ -92,16 +90,7 @@ public abstract class SharedItemToggleSystem : EntitySystem
if (predicted == false && _netManager.IsClient)
return true;
Activate(uid, itemToggle);
var evPlayToggleSound = new ItemTogglePlayToggleSoundEvent(Activated: true, Predicted: predicted, user);
RaiseLocalEvent(uid, ref evPlayToggleSound);
var evActiveSound = new ItemToggleActiveSoundUpdateEvent(Activated: true, Predicted: predicted, user);
RaiseLocalEvent(uid, ref evActiveSound);
var toggleUsed = new ItemToggleDoneEvent(Activated: true, user);
RaiseLocalEvent(uid, ref toggleUsed);
Activate(uid, itemToggle, predicted, user);
return true;
}
@@ -129,61 +118,78 @@ public abstract class SharedItemToggleSystem : EntitySystem
if (predicted == false && _netManager.IsClient)
return true;
Deactivate(uid, itemToggle);
var evPlayToggleSound = new ItemTogglePlayToggleSoundEvent(Activated: false, Predicted: predicted, user);
RaiseLocalEvent(uid, ref evPlayToggleSound);
var evActiveSound = new ItemToggleActiveSoundUpdateEvent(Activated: false, Predicted: predicted, user);
RaiseLocalEvent(uid, ref evActiveSound);
var toggleUsed = new ItemToggleDoneEvent(Activated: false, user);
RaiseLocalEvent(uid, ref toggleUsed);
Deactivate(uid, itemToggle, predicted, user);
return true;
}
/// <summary>
/// Used to make the actual changes to the item's components on activation.
/// </summary>
private void Activate(EntityUid uid, ItemToggleComponent itemToggle)
private void Activate(EntityUid uid, ItemToggleComponent itemToggle, bool predicted, EntityUid? user = null)
{
UpdateComponents(uid, itemToggle.Activated = true);
// TODO: Fix this hardcoding
TryComp(uid, out AppearanceComponent? appearance);
_appearance.SetData(uid, ToggleableLightVisuals.Enabled, true, appearance);
_appearance.SetData(uid, ToggleVisuals.Toggled, true, appearance);
if (_light.TryGetLight(uid, out var light))
{
_light.SetEnabled(uid, true, light);
}
SoundSpecifier? soundToPlay = itemToggle.SoundActivate;
if (soundToPlay == null)
return;
if (predicted)
_audio.PlayPredicted(soundToPlay, uid, user);
else
_audio.PlayPvs(soundToPlay, uid);
// END FIX HARDCODING
var toggleUsed = new ItemToggledEvent(predicted, Activated: true, user);
RaiseLocalEvent(uid, ref toggleUsed);
var activev = new ItemToggleActivatedEvent(user);
RaiseLocalEvent(uid, ref activev);
itemToggle.Activated = true;
Dirty(uid, itemToggle);
}
/// <summary>
/// Used to make the actual changes to the item's components on deactivation.
/// </summary>
private void Deactivate(EntityUid uid, ItemToggleComponent itemToggle)
private void Deactivate(EntityUid uid, ItemToggleComponent itemToggle, bool predicted, EntityUid? user = null)
{
UpdateComponents(uid, itemToggle.Activated = false);
// TODO: Fix this hardcoding
TryComp(uid, out AppearanceComponent? appearance);
_appearance.SetData(uid, ToggleableLightVisuals.Enabled, false, appearance);
_appearance.SetData(uid, ToggleVisuals.Toggled, false, appearance);
if (_light.TryGetLight(uid, out var light))
{
_light.SetEnabled(uid, false, light);
}
var soundToPlay = itemToggle.SoundDeactivate;
if (predicted)
_audio.PlayPredicted(soundToPlay, uid, user);
else
_audio.PlayPvs(soundToPlay, uid);
// END FIX HARDCODING
var toggleUsed = new ItemToggledEvent(predicted, Activated: false, user);
RaiseLocalEvent(uid, ref toggleUsed);
var activev = new ItemToggleDeactivatedEvent(user);
RaiseLocalEvent(uid, ref activev);
itemToggle.Activated = false;
Dirty(uid, itemToggle);
}
/// <summary>
/// Used to raise events to update components on toggle.
/// </summary>
private void UpdateComponents(EntityUid uid, bool activated)
{
var evSize = new ItemToggleSizeUpdateEvent(activated);
RaiseLocalEvent(uid, ref evSize);
var evMelee = new ItemToggleMeleeWeaponUpdateEvent(activated);
RaiseLocalEvent(uid, ref evMelee);
var evAppearance = new ItemToggleAppearanceUpdateEvent(activated);
RaiseLocalEvent(uid, ref evAppearance);
var evLight = new ItemToggleLightUpdateEvent(activated);
RaiseLocalEvent(uid, ref evLight);
var evReflect = new ItemToggleReflectUpdateEvent(activated);
RaiseLocalEvent(uid, ref evReflect);
}
/// <summary>
/// Used for items that require to be wielded in both hands to activate. For instance the dual energy sword will turn off if not wielded.
/// </summary>
@@ -219,30 +225,10 @@ public abstract class SharedItemToggleSystem : EntitySystem
args.IsHot = IsActivated(uid);
}
/// <summary>
/// Used to update item appearance.
/// </summary>
private void UpdateAppearance(EntityUid uid, AppearanceComponent appearance, ref ItemToggleAppearanceUpdateEvent args)
{
_appearance.SetData(uid, ToggleableLightVisuals.Enabled, args.Activated, appearance);
_appearance.SetData(uid, ToggleVisuals.Toggled, args.Activated, appearance);
}
/// <summary>
/// Used to update light settings.
/// </summary>
private void UpdateLight(EntityUid uid, ItemToggleComponent comp, ref ItemToggleLightUpdateEvent args)
{
if (!_light.TryGetLight(uid, out var light))
return;
_light.SetEnabled(uid, args.Activated, light);
}
/// <summary>
/// Used to update the looping active sound linked to the entity.
/// </summary>
private void UpdateActiveSound(EntityUid uid, ItemToggleActiveSoundComponent activeSound, ref ItemToggleActiveSoundUpdateEvent args)
private void UpdateActiveSound(EntityUid uid, ItemToggleActiveSoundComponent activeSound, ref ItemToggledEvent args)
{
if (args.Activated)
{
@@ -259,35 +245,4 @@ public abstract class SharedItemToggleSystem : EntitySystem
activeSound.PlayingStream = _audio.Stop(activeSound.PlayingStream);
}
}
/// <summary>
/// Used to play a toggle sound.
/// </summary>
private void PlayToggleSound(EntityUid uid, ItemToggleComponent itemToggle, ref ItemTogglePlayToggleSoundEvent args)
{
SoundSpecifier? soundToPlay;
if (args.Activated)
soundToPlay = itemToggle.SoundActivate;
else
soundToPlay = itemToggle.SoundDeactivate;
if (soundToPlay == null)
return;
if (args.Predicted)
_audio.PlayPredicted(soundToPlay, uid, args.User);
else
_audio.PlayPvs(soundToPlay, uid);
}
/// <summary>
/// Used to play a failure to toggle sound.
/// </summary>
private void PlayFailToggleSound(EntityUid uid, ItemToggleComponent itemToggle, ref ItemTogglePlayFailSoundEvent args)
{
if (args.Predicted)
_audio.PlayPredicted(itemToggle.SoundFailToActivate, uid, args.User);
else
_audio.PlayPvs(itemToggle.SoundFailToActivate, uid);
}
}

View File

@@ -2,6 +2,7 @@ using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Verbs;
using Content.Shared.Examine;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.Storage;
using JetBrains.Annotations;
using Robust.Shared.Containers;
@@ -21,14 +22,17 @@ public abstract class SharedItemSystem : EntitySystem
{
base.Initialize();
SubscribeLocalEvent<ItemComponent, GetVerbsEvent<InteractionVerb>>(AddPickupVerb);
SubscribeLocalEvent<ItemComponent, InteractHandEvent>(OnHandInteract, before: new []{typeof(SharedItemSystem)});
SubscribeLocalEvent<ItemComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<ItemComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<ItemComponent, InteractHandEvent>(OnHandInteract);
SubscribeLocalEvent<ItemComponent, AfterAutoHandleStateEvent>(OnItemAutoState);
SubscribeLocalEvent<ItemComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<ItemToggleSizeComponent, ItemToggleSizeUpdateEvent>(OnItemToggle);
SubscribeLocalEvent<ItemToggleSizeComponent, ItemToggledEvent>(OnItemToggle);
}
private void OnItemAutoState(EntityUid uid, ItemComponent component, ref AfterAutoHandleStateEvent args)
{
SetHeldPrefix(uid, component.HeldPrefix, force: true, component);
}
#region Public API
@@ -42,12 +46,12 @@ public abstract class SharedItemSystem : EntitySystem
Dirty(uid, component);
}
public void SetHeldPrefix(EntityUid uid, string? heldPrefix, ItemComponent? component = null)
public void SetHeldPrefix(EntityUid uid, string? heldPrefix, bool force = false, ItemComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return;
if (component.HeldPrefix == heldPrefix)
if (!force && component.HeldPrefix == heldPrefix)
return;
component.HeldPrefix = heldPrefix;
@@ -81,20 +85,6 @@ public abstract class SharedItemSystem : EntitySystem
args.Handled = _handsSystem.TryPickup(args.User, uid, animateUser: false);
}
private void OnHandleState(EntityUid uid, ItemComponent component, ref ComponentHandleState args)
{
if (args.Current is not ItemComponentState state)
return;
component.Size = state.Size;
SetHeldPrefix(uid, state.HeldPrefix, component);
}
private void OnGetState(EntityUid uid, ItemComponent component, ref ComponentGetState args)
{
args.State = new ItemComponentState(component.Size, component.HeldPrefix);
}
private void AddPickupVerb(EntityUid uid, ItemComponent component, GetVerbsEvent<InteractionVerb> args)
{
if (args.Hands == null ||
@@ -211,7 +201,7 @@ public abstract class SharedItemSystem : EntitySystem
/// <summary>
/// Used to update the Item component on item toggle (specifically size).
/// </summary>
private void OnItemToggle(EntityUid uid, ItemToggleSizeComponent itemToggleSize, ItemToggleSizeUpdateEvent args)
private void OnItemToggle(EntityUid uid, ItemToggleSizeComponent itemToggleSize, ItemToggledEvent args)
{
if (!TryComp(uid, out ItemComponent? item))
return;