Universal weldable component (#7955)

* Weldable component for door

* Content update

* Examine message

* Universal visualizer

* Small fix

* Entity storage

* Content

* Fixed test

* Update Content.Shared/Storage/SharedStorageComponent.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Fixed loc string

* Add public API to change welding time

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Alex Evgrashin
2022-05-09 08:51:52 +03:00
committed by GitHub
parent 02de328d9c
commit df49c2fd57
29 changed files with 367 additions and 215 deletions

View File

@@ -18,6 +18,7 @@ using Robust.Shared.Containers;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Player;
using System.Linq;
using Content.Server.Tools.Systems;
using Content.Shared.Tools.Components;
namespace Content.Server.Doors.Systems;
@@ -39,8 +40,8 @@ public sealed class DoorSystem : SharedDoorSystem
SubscribeLocalEvent<DoorComponent, PryFinishedEvent>(OnPryFinished);
SubscribeLocalEvent<DoorComponent, PryCancelledEvent>(OnPryCancelled);
SubscribeLocalEvent<DoorComponent, WeldFinishedEvent>(OnWeldFinished);
SubscribeLocalEvent<DoorComponent, WeldCancelledEvent>(OnWeldCancelled);
SubscribeLocalEvent<DoorComponent, WeldableAttemptEvent>(OnWeldAttempt);
SubscribeLocalEvent<DoorComponent, WeldableChangedEvent>(OnWeldChanged);
SubscribeLocalEvent<DoorComponent, GotEmaggedEvent>(OnEmagged);
}
@@ -129,32 +130,27 @@ public sealed class DoorSystem : SharedDoorSystem
args.Handled = TryPryDoor(uid, args.Used, args.User, door);
return;
}
}
if (door.Weldable && tool.Qualities.Contains(door.WeldingQuality))
private void OnWeldAttempt(EntityUid uid, DoorComponent component, WeldableAttemptEvent args)
{
if (component.CurrentlyCrushing.Count > 0)
{
args.Handled = TryWeldDoor(uid, args.Used, args.User, door);
args.Cancel();
return;
}
if (component.State != DoorState.Closed && component.State != DoorState.Welded)
{
args.Cancel();
}
}
/// <summary>
/// Attempt to weld a door shut, or unweld it if it is already welded. This does not actually check if the user
/// is holding the correct tool.
/// </summary>
private bool TryWeldDoor(EntityUid target, EntityUid used, EntityUid user, DoorComponent door)
private void OnWeldChanged(EntityUid uid, DoorComponent component, WeldableChangedEvent args)
{
if (!door.Weldable || door.BeingWelded || door.CurrentlyCrushing.Count > 0)
return false;
// is the door in a weld-able state?
if (door.State != DoorState.Closed && door.State != DoorState.Welded)
return false;
// perform a do-after delay
door.BeingWelded = true;
_toolSystem.UseTool(used, user, target, 3f, 3f, door.WeldingQuality,
new WeldFinishedEvent(), new WeldCancelledEvent(), target);
return true; // we might not actually succeeded, but a do-after has started
if (component.State == DoorState.Closed)
SetState(uid, DoorState.Welded, component);
else if (component.State == DoorState.Welded)
SetState(uid, DoorState.Closed, component);
}
/// <summary>
@@ -182,24 +178,6 @@ public sealed class DoorSystem : SharedDoorSystem
return true; // we might not actually succeeded, but a do-after has started
}
private void OnWeldCancelled(EntityUid uid, DoorComponent door, WeldCancelledEvent args)
{
door.BeingWelded = false;
}
private void OnWeldFinished(EntityUid uid, DoorComponent door, WeldFinishedEvent args)
{
door.BeingWelded = false;
if (!door.Weldable)
return;
if (door.State == DoorState.Closed)
SetState(uid, DoorState.Welded, door);
else if (door.State == DoorState.Welded)
SetState(uid, DoorState.Closed, door);
}
private void OnPryCancelled(EntityUid uid, DoorComponent door, PryCancelledEvent args)
{
door.BeingPried = false;
@@ -324,5 +302,4 @@ public sealed class DoorSystem : SharedDoorSystem
public sealed class PryFinishedEvent : EntityEventArgs { }
public sealed class PryCancelledEvent : EntityEventArgs { }
public sealed class WeldFinishedEvent : EntityEventArgs { }
public sealed class WeldCancelledEvent : EntityEventArgs { }

View File

@@ -29,7 +29,7 @@ namespace Content.Server.Storage.Components
[Virtual]
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(IStorageComponent))]
public class EntityStorageComponent : Component, IActivate, IStorageComponent, IInteractUsing
public class EntityStorageComponent : Component, IActivate, IStorageComponent
{
[Dependency] private readonly IEntityManager _entMan = default!;
@@ -72,15 +72,6 @@ namespace Content.Server.Storage.Components
[DataField("open")]
public bool Open;
[DataField("weldingQuality", customTypeSerializer:typeof(PrototypeIdSerializer<ToolQualityPrototype>))]
private string _weldingQuality = "Welding";
[DataField("CanWeldShut")]
private bool _canWeldShut = true;
[DataField("IsWeldedShut")]
private bool _isWeldedShut;
[DataField("closeSound")]
private SoundSpecifier _closeSound = new SoundPathSpecifier("/Audio/Effects/closetclose.ogg");
@@ -116,32 +107,7 @@ namespace Content.Server.Storage.Components
}
[ViewVariables(VVAccess.ReadWrite)]
public bool IsWeldedShut
{
get => _isWeldedShut;
set
{
if (_isWeldedShut == value) return;
_isWeldedShut = value;
UpdateAppearance();
}
}
private bool _beingWelded;
[ViewVariables(VVAccess.ReadWrite)]
public bool CanWeldShut
{
get => _canWeldShut;
set
{
if (_canWeldShut == value) return;
_canWeldShut = value;
UpdateAppearance();
}
}
public bool IsWeldedShut;
[ViewVariables(VVAccess.ReadWrite)]
public float EnteringRange
@@ -165,8 +131,6 @@ namespace Content.Server.Storage.Components
{
EntitySystem.Get<PlaceableSurfaceSystem>().SetPlaceable(Owner, Open, surface);
}
UpdateAppearance();
}
public virtual void Activate(ActivateEventArgs eventArgs)
@@ -296,15 +260,6 @@ namespace Content.Server.Storage.Components
SoundSystem.Play(Filter.Pvs(Owner), _openSound.GetSound(), Owner);
}
private void UpdateAppearance()
{
if (_entMan.TryGetComponent(Owner, out AppearanceComponent? appearance))
{
appearance.SetData(StorageVisuals.CanWeld, _canWeldShut);
appearance.SetData(StorageVisuals.Welded, _isWeldedShut);
}
}
private void ModifyComponents()
{
if (!_isCollidableWhenOpen && _entMan.TryGetComponent<FixturesComponent?>(Owner, out var manager)
@@ -409,48 +364,6 @@ namespace Content.Server.Storage.Components
return Contents.CanInsert(entity);
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
if (_beingWelded)
return false;
if (Open)
{
_beingWelded = false;
return false;
}
if (!CanWeldShut)
{
_beingWelded = false;
return false;
}
if (Contents.Contains(eventArgs.User))
{
_beingWelded = false;
Owner.PopupMessage(eventArgs.User, Loc.GetString("entity-storage-component-already-contains-user-message"));
return false;
}
if (_beingWelded)
return false;
_beingWelded = true;
var toolSystem = EntitySystem.Get<ToolSystem>();
if (!await toolSystem.UseTool(eventArgs.Using, eventArgs.User, Owner, 1f, 1f, _weldingQuality))
{
_beingWelded = false;
return false;
}
_beingWelded = false;
IsWeldedShut ^= true;
return true;
}
protected virtual IEnumerable<EntityUid> DetermineCollidingEntities()
{
var entityLookup = EntitySystem.Get<EntityLookupSystem>();

View File

@@ -1,18 +1,47 @@
using System.Linq;
using Content.Server.Popups;
using Content.Server.Storage.Components;
using Content.Server.Tools.Systems;
using Content.Shared.Destructible;
using Content.Shared.Interaction;
using Robust.Shared.Physics;
using Robust.Shared.Player;
namespace Content.Server.Storage.EntitySystems;
public sealed class EntityStorageSystem : EntitySystem
{
[Dependency] private readonly PopupSystem _popupSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EntityStorageComponent, WeldableAttemptEvent>(OnWeldableAttempt);
SubscribeLocalEvent<EntityStorageComponent, WeldableChangedEvent>(OnWelded);
SubscribeLocalEvent<EntityStorageComponent, DestructionEventArgs>(OnDestroy);
}
private void OnWeldableAttempt(EntityUid uid, EntityStorageComponent component, WeldableAttemptEvent args)
{
if (component.Open)
{
args.Cancel();
return;
}
if (component.Contents.Contains(args.User))
{
var msg = Loc.GetString("entity-storage-component-already-contains-user-message");
_popupSystem.PopupEntity(msg, args.User, Filter.Entities(args.User));
args.Cancel();
}
}
private void OnWelded(EntityUid uid, EntityStorageComponent component, WeldableChangedEvent args)
{
component.IsWeldedShut = args.IsWelded;
}
private void OnDestroy(EntityUid uid, EntityStorageComponent component, DestructionEventArgs args)
{
component.Open = true;

View File

@@ -0,0 +1,61 @@
using Content.Server.Tools.Systems;
using Content.Shared.Tools;
using Content.Shared.Tools.Components;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Tools.Components;
/// <summary>
/// Allows users to weld/unweld doors, crates and lockers.
/// </summary>
[RegisterComponent]
[Friend(typeof(WeldableSystem))]
public sealed class WeldableComponent : SharedWeldableComponent
{
/// <summary>
/// Tool quality for welding.
/// </summary>
[DataField("weldingQuality", customTypeSerializer: typeof(PrototypeIdSerializer<ToolQualityPrototype>))]
[ViewVariables(VVAccess.ReadWrite)]
public string WeldingQuality = "Welding";
/// <summary>
/// Whether this entity can ever be welded shut.
/// </summary>
[DataField("weldable")]
[ViewVariables(VVAccess.ReadWrite)]
public bool Weldable = true;
/// <summary>
/// How much fuel does it take to weld/unweld entity.
/// </summary>
[DataField("fuel")]
[ViewVariables(VVAccess.ReadWrite)]
public float FuelConsumption = 1f;
/// <summary>
/// How much time does it take to weld/unweld entity.
/// </summary>
[DataField("time")]
[ViewVariables(VVAccess.ReadWrite)]
public TimeSpan WeldingTime = TimeSpan.FromSeconds(1f);
/// <summary>
/// Shown when welded entity is examined.
/// </summary>
[DataField("weldedExamineMessage")]
[ViewVariables(VVAccess.ReadWrite)]
public string? WeldedExamineMessage = "weldable-component-examine-is-welded";
/// <summary>
/// Whether something is currently using a welder on this so DoAfter isn't spammed.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public bool BeingWelded;
/// <summary>
/// Is this entity currently welded shut?
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public bool IsWelded;
}

View File

@@ -0,0 +1,159 @@
using Content.Server.Tools.Components;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Tools.Components;
namespace Content.Server.Tools.Systems;
public sealed class WeldableSystem : EntitySystem
{
[Dependency] private readonly ToolSystem _toolSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<WeldableComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<WeldableComponent, WeldFinishedEvent>(OnWeldFinished);
SubscribeLocalEvent<WeldableComponent, WeldCancelledEvent>(OnWeldCanceled);
SubscribeLocalEvent<WeldableComponent, ExaminedEvent>(OnExamine);
}
private void OnExamine(EntityUid uid, WeldableComponent component, ExaminedEvent args)
{
if (component.IsWelded && component.WeldedExamineMessage != null)
args.PushText(Loc.GetString(component.WeldedExamineMessage));
}
private void OnInteractUsing(EntityUid uid, WeldableComponent component, InteractUsingEvent args)
{
if (args.Handled)
return;
args.Handled = TryWeld(uid, args.Used, args.User, component);
}
private bool CanWeld(EntityUid uid, EntityUid tool, EntityUid user, WeldableComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
// Basic checks
if (!component.Weldable || component.BeingWelded)
return false;
if (!_toolSystem.HasQuality(tool, component.WeldingQuality))
return false;
// Other component systems
var attempt = new WeldableAttemptEvent(user, tool);
RaiseLocalEvent(uid, attempt);
if (attempt.Cancelled)
return false;
return true;
}
private bool TryWeld(EntityUid uid, EntityUid tool, EntityUid user, WeldableComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
if (!CanWeld(uid, tool, user, component))
return false;
component.BeingWelded = true;
_toolSystem.UseTool(tool, user, uid, component.FuelConsumption,
component.WeldingTime.Seconds, component.WeldingQuality,
new WeldFinishedEvent(user, tool), new WeldCancelledEvent(), uid);
return true;
}
private void OnWeldFinished(EntityUid uid, WeldableComponent component, WeldFinishedEvent args)
{
component.BeingWelded = false;
// Check if target is still valid
if (!CanWeld(uid, args.Tool, args.User, component))
return;
component.IsWelded = !component.IsWelded;
RaiseLocalEvent(uid, new WeldableChangedEvent(component.IsWelded));
UpdateAppearance(uid, component);
}
private void OnWeldCanceled(EntityUid uid, WeldableComponent component, WeldCancelledEvent args)
{
component.BeingWelded = false;
}
private void UpdateAppearance(EntityUid uid, WeldableComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (!TryComp(uid, out AppearanceComponent? appearance))
return;
appearance.SetData(WeldableVisuals.IsWelded, component.IsWelded);
}
public void SetWeldingTime(EntityUid uid, TimeSpan time, WeldableComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.WeldingTime = time;
}
/// <summary>
/// Raised after welding do_after has finished. It doesn't guarantee success,
/// use <see cref="WeldableChangedEvent"/> to get updated status.
/// </summary>
private sealed class WeldFinishedEvent : EntityEventArgs
{
public readonly EntityUid User;
public readonly EntityUid Tool;
public WeldFinishedEvent(EntityUid user, EntityUid tool)
{
User = user;
Tool = tool;
}
}
/// <summary>
/// Raised when entity welding has failed.
/// </summary>
private sealed class WeldCancelledEvent : EntityEventArgs
{
}
}
/// <summary>
/// Checks that entity can be weld/unweld.
/// Raised twice: before do_after and after to check that entity still valid.
/// </summary>
public sealed class WeldableAttemptEvent : CancellableEntityEventArgs
{
public readonly EntityUid User;
public readonly EntityUid Tool;
public WeldableAttemptEvent(EntityUid user, EntityUid tool)
{
User = user;
Tool = tool;
}
}
/// <summary>
/// Raised when <see cref="WeldableComponent.IsWelded"/> has changed.
/// </summary>
public sealed class WeldableChangedEvent : EntityEventArgs
{
public readonly bool IsWelded;
public WeldableChangedEvent(bool isWelded)
{
IsWelded = isWelded;
}
}