Refactor Crayons to use shared charges system and autonetworking. Adds auto recharging crayon. (#40575)

* Added special crayon with infinite charges for borg usage.

* Use battery system to manage charges.

* Reverted extra changes

* Set charge on init

* removed init assignment

* Added comments to crayoncomponent

* tweaked comments

* Working with the new charges component, but at what cost?

* Remvoed extra field

* Apply suggestion from @slarticodefast

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

* Apply suggestion from @slarticodefast

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

* Apply suggestion from @slarticodefast

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

* Apply suggestion from @slarticodefast

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

* Fix renamed variables and descriptions in comments

* Variable naming, comment cleanup and autonetworking.

* Fix for test case, modified on init

* Cleaned up/merged charges logic

* review

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
David
2025-10-10 13:45:48 -06:00
committed by GitHub
parent 766c2b8759
commit 3503cb52d2
7 changed files with 169 additions and 219 deletions

View File

@@ -1,28 +0,0 @@
using Content.Server.UserInterface;
using Content.Shared.Crayon;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
namespace Content.Server.Crayon
{
[RegisterComponent]
public sealed partial class CrayonComponent : SharedCrayonComponent
{
[DataField("useSound")] public SoundSpecifier? UseSound;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("selectableColor")]
public bool SelectableColor { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public int Charges { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
[DataField("capacity")]
public int Capacity { get; set; } = 30;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("deleteEmpty")]
public bool DeleteEmpty = true;
}
}

View File

@@ -3,6 +3,7 @@ using System.Numerics;
using Content.Server.Administration.Logs;
using Content.Server.Decals;
using Content.Server.Popups;
using Content.Shared.Charges.Systems;
using Content.Shared.Crayon;
using Content.Shared.Database;
using Content.Shared.Decals;
@@ -12,7 +13,6 @@ using Content.Shared.Nutrition.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Server.Crayon;
@@ -24,23 +24,27 @@ public sealed class CrayonSystem : SharedCrayonSystem
[Dependency] private readonly DecalSystem _decals = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedChargesSystem _charges = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CrayonComponent, ComponentInit>(OnCrayonInit);
SubscribeLocalEvent<CrayonComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<CrayonComponent, CrayonSelectMessage>(OnCrayonBoundUI);
SubscribeLocalEvent<CrayonComponent, CrayonColorMessage>(OnCrayonBoundUIColor);
SubscribeLocalEvent<CrayonComponent, UseInHandEvent>(OnCrayonUse, before: new[] { typeof(FoodSystem) });
SubscribeLocalEvent<CrayonComponent, AfterInteractEvent>(OnCrayonAfterInteract, after: new[] { typeof(FoodSystem) });
SubscribeLocalEvent<CrayonComponent, DroppedEvent>(OnCrayonDropped);
SubscribeLocalEvent<CrayonComponent, ComponentGetState>(OnCrayonGetState);
}
private static void OnCrayonGetState(EntityUid uid, CrayonComponent component, ref ComponentGetState args)
private void OnMapInit(Entity<CrayonComponent> ent, ref MapInitEvent args)
{
args.State = new CrayonComponentState(component.Color, component.SelectedState, component.Charges, component.Capacity);
// Get the first one from the catalog and set it as default
var decal = _prototypeManager.EnumeratePrototypes<DecalPrototype>().FirstOrDefault(x => x.Tags.Contains("crayon"));
ent.Comp.SelectedState = decal?.ID ?? string.Empty;
Dirty(ent);
}
private void OnCrayonAfterInteract(EntityUid uid, CrayonComponent component, AfterInteractEvent args)
@@ -48,7 +52,7 @@ public sealed class CrayonSystem : SharedCrayonSystem
if (args.Handled || !args.CanReach)
return;
if (component.Charges <= 0)
if (_charges.IsEmpty(uid))
{
if (component.DeleteEmpty)
UseUpCrayon(uid, args.User);
@@ -72,17 +76,15 @@ public sealed class CrayonSystem : SharedCrayonSystem
if (component.UseSound != null)
_audio.PlayPvs(component.UseSound, uid, AudioParams.Default.WithVariation(0.125f));
// Decrease "Ammo"
component.Charges--;
Dirty(uid, component);
_charges.TryUseCharge(uid);
_adminLogger.Add(LogType.CrayonDraw, LogImpact.Low, $"{ToPrettyString(args.User):user} drew a {component.Color:color} {component.SelectedState}");
args.Handled = true;
if (component.DeleteEmpty && component.Charges <= 0)
if (component.DeleteEmpty && _charges.IsEmpty(uid))
UseUpCrayon(uid, args.User);
else
_uiSystem.ServerSendUiMessage(uid, SharedCrayonComponent.CrayonUiKey.Key, new CrayonUsedMessage(component.SelectedState));
_uiSystem.ServerSendUiMessage(uid, CrayonUiKey.Key, new CrayonUsedMessage(component.SelectedState));
}
private void OnCrayonUse(EntityUid uid, CrayonComponent component, UseInHandEvent args)
@@ -91,14 +93,12 @@ public sealed class CrayonSystem : SharedCrayonSystem
if (args.Handled)
return;
if (!_uiSystem.HasUi(uid, SharedCrayonComponent.CrayonUiKey.Key))
{
if (!_uiSystem.HasUi(uid, CrayonUiKey.Key))
return;
}
_uiSystem.TryToggleUi(uid, SharedCrayonComponent.CrayonUiKey.Key, args.User);
_uiSystem.TryToggleUi(uid, CrayonUiKey.Key, args.User);
_uiSystem.SetUiState(uid, SharedCrayonComponent.CrayonUiKey.Key, new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color));
_uiSystem.SetUiState(uid, CrayonUiKey.Key, new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color));
args.Handled = true;
}
@@ -109,35 +109,23 @@ public sealed class CrayonSystem : SharedCrayonSystem
return;
component.SelectedState = args.State;
Dirty(uid, component);
}
private void OnCrayonBoundUIColor(EntityUid uid, CrayonComponent component, CrayonColorMessage args)
{
// you still need to ensure that the given color is a valid color
// Ensure that the given color can be changed or already matches
if (!component.SelectableColor || args.Color == component.Color)
return;
component.Color = args.Color;
Dirty(uid, component);
}
private void OnCrayonInit(EntityUid uid, CrayonComponent component, ComponentInit args)
{
component.Charges = component.Capacity;
// Get the first one from the catalog and set it as default
var decal = _prototypeManager.EnumeratePrototypes<DecalPrototype>().FirstOrDefault(x => x.Tags.Contains("crayon"));
component.SelectedState = decal?.ID ?? string.Empty;
Dirty(uid, component);
}
private void OnCrayonDropped(EntityUid uid, CrayonComponent component, DroppedEvent args)
{
// TODO: Use the existing event.
_uiSystem.CloseUi(uid, SharedCrayonComponent.CrayonUiKey.Key, args.User);
_uiSystem.CloseUi(uid, CrayonUiKey.Key, args.User);
}
private void UseUpCrayon(EntityUid uid, EntityUid user)