Kill bobby 2.0 (#6023)
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using Content.Server.Ghost;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Part;
|
||||
@@ -16,8 +15,7 @@ namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedBodyComponent))]
|
||||
[ComponentReference(typeof(IGhostOnMove))]
|
||||
public class BodyComponent : SharedBodyComponent, IGhostOnMove
|
||||
public class BodyComponent : SharedBodyComponent
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
|
||||
|
||||
@@ -1,50 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Surgery;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedBodyPartComponent))]
|
||||
public class BodyPartComponent : SharedBodyPartComponent, IAfterInteract
|
||||
public class BodyPartComponent : SharedBodyPartComponent
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
|
||||
private readonly Dictionary<int, object> _optionsCache = new();
|
||||
private SharedBodyComponent? _owningBodyCache;
|
||||
private int _idHash;
|
||||
private EntityUid? _surgeonCache;
|
||||
private Container _mechanismContainer = default!;
|
||||
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SurgeryUIKey.Key);
|
||||
|
||||
public override bool CanAddMechanism(SharedMechanismComponent mechanism)
|
||||
public override bool CanAddMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
return base.CanAddMechanism(mechanism) &&
|
||||
_mechanismContainer.CanInsert(mechanism.Owner);
|
||||
}
|
||||
|
||||
protected override void OnAddMechanism(SharedMechanismComponent mechanism)
|
||||
protected override void OnAddMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
base.OnAddMechanism(mechanism);
|
||||
|
||||
_mechanismContainer.Insert(mechanism.Owner);
|
||||
}
|
||||
|
||||
protected override void OnRemoveMechanism(SharedMechanismComponent mechanism)
|
||||
protected override void OnRemoveMechanism(MechanismComponent mechanism)
|
||||
{
|
||||
base.OnRemoveMechanism(mechanism);
|
||||
|
||||
@@ -65,156 +49,14 @@ namespace Content.Server.Body.Components
|
||||
{
|
||||
var entity = _entMan.SpawnEntity(mechanismId, _entMan.GetComponent<TransformComponent>(Owner).MapPosition);
|
||||
|
||||
if (!_entMan.TryGetComponent(entity, out SharedMechanismComponent? mechanism))
|
||||
if (!_entMan.TryGetComponent(entity, out MechanismComponent? mechanism))
|
||||
{
|
||||
Logger.Error($"Entity {mechanismId} does not have a {nameof(SharedMechanismComponent)} component.");
|
||||
Logger.Error($"Entity {mechanismId} does not have a {nameof(MechanismComponent)} component.");
|
||||
continue;
|
||||
}
|
||||
|
||||
TryAddMechanism(mechanism, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += OnUIMessage;
|
||||
}
|
||||
|
||||
foreach (var mechanism in Mechanisms)
|
||||
{
|
||||
mechanism.Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
||||
{
|
||||
// TODO BODY
|
||||
if (eventArgs.Target == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CloseAllSurgeryUIs();
|
||||
_optionsCache.Clear();
|
||||
_surgeonCache = null;
|
||||
_owningBodyCache = null;
|
||||
|
||||
if (_entMan.TryGetComponent(eventArgs.Target.Value, out SharedBodyComponent? body))
|
||||
{
|
||||
SendSlots(eventArgs, body);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void SendSlots(AfterInteractEventArgs eventArgs, SharedBodyComponent body)
|
||||
{
|
||||
// Create dictionary to send to client (text to be shown : data sent back if selected)
|
||||
var toSend = new Dictionary<string, int>();
|
||||
|
||||
// Here we are trying to grab a list of all empty BodySlots adjacent to an existing BodyPart that can be
|
||||
// attached to. i.e. an empty left hand slot, connected to an occupied left arm slot would be valid.
|
||||
foreach (var slot in body.EmptySlots)
|
||||
{
|
||||
if (slot.PartType != PartType)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var connection in slot.Connections)
|
||||
{
|
||||
if (connection.Part == null ||
|
||||
!connection.Part.CanAttachPart(this))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_optionsCache.Add(_idHash, slot);
|
||||
toSend.Add(slot.Id, _idHash++);
|
||||
}
|
||||
}
|
||||
|
||||
if (_optionsCache.Count > 0)
|
||||
{
|
||||
OpenSurgeryUI(_entMan.GetComponent<ActorComponent>(eventArgs.User).PlayerSession);
|
||||
BodyPartSlotRequest(_entMan.GetComponent<ActorComponent>(eventArgs.User).PlayerSession,
|
||||
toSend);
|
||||
_surgeonCache = eventArgs.User;
|
||||
_owningBodyCache = body;
|
||||
}
|
||||
else // If surgery cannot be performed, show message saying so.
|
||||
{
|
||||
eventArgs.Target?.PopupMessage(eventArgs.User,
|
||||
Loc.GetString("bodypart-component-no-way-to-install-message", ("partName", Owner)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called after the client chooses from a list of possible
|
||||
/// BodyPartSlots to install the limb on.
|
||||
/// </summary>
|
||||
private void ReceiveBodyPartSlot(int key)
|
||||
{
|
||||
if (_surgeonCache == null ||
|
||||
!_entMan.TryGetComponent(_surgeonCache.Value, out ActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CloseSurgeryUI(actor.PlayerSession);
|
||||
|
||||
if (_owningBodyCache == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: sanity checks to see whether user is in range, user is still able-bodied, target is still the same, etc etc
|
||||
if (!_optionsCache.TryGetValue(key, out var targetObject))
|
||||
{
|
||||
_owningBodyCache.Owner.PopupMessage(_surgeonCache.Value,
|
||||
Loc.GetString("bodypart-component-no-way-to-attach-message", ("partName", Owner)));
|
||||
}
|
||||
|
||||
var target = (string) targetObject!;
|
||||
var message = _owningBodyCache.TryAddPart(target, this)
|
||||
? Loc.GetString("bodypart-component-attach-success-message",("partName", Owner))
|
||||
: Loc.GetString("bodypart-component-attach-fail-message",("partName", Owner));
|
||||
|
||||
_owningBodyCache.Owner.PopupMessage(_surgeonCache.Value, message);
|
||||
}
|
||||
|
||||
private void OpenSurgeryUI(IPlayerSession session)
|
||||
{
|
||||
UserInterface?.Open(session);
|
||||
}
|
||||
|
||||
private void BodyPartSlotRequest(IPlayerSession session, Dictionary<string, int> options)
|
||||
{
|
||||
UserInterface?.SendMessage(new RequestBodyPartSlotSurgeryUIMessage(options), session);
|
||||
}
|
||||
|
||||
private void CloseSurgeryUI(IPlayerSession session)
|
||||
{
|
||||
UserInterface?.Close(session);
|
||||
}
|
||||
|
||||
private void CloseAllSurgeryUIs()
|
||||
{
|
||||
UserInterface?.CloseAll();
|
||||
}
|
||||
|
||||
private void OnUIMessage(ServerBoundUserInterfaceMessage message)
|
||||
{
|
||||
switch (message.Message)
|
||||
{
|
||||
case ReceiveBodyPartSlotSurgeryUIMessage msg:
|
||||
ReceiveBodyPartSlot(msg.SelectedOptionId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Surgery;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Body.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedMechanismComponent))]
|
||||
public class MechanismComponent : SharedMechanismComponent, IAfterInteract
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entities = default!;
|
||||
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SurgeryUIKey.Key);
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += OnUIMessage;
|
||||
}
|
||||
}
|
||||
|
||||
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
||||
{
|
||||
if (eventArgs.Target == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
CloseAllSurgeryUIs();
|
||||
OptionsCache.Clear();
|
||||
PerformerCache = null;
|
||||
BodyCache = null;
|
||||
|
||||
if (_entities.TryGetComponent(eventArgs.Target.Value, out SharedBodyComponent? body))
|
||||
{
|
||||
SendBodyPartListToUser(eventArgs, body);
|
||||
}
|
||||
else if (_entities.TryGetComponent<SharedBodyPartComponent?>(eventArgs.Target.Value, out var part))
|
||||
{
|
||||
DebugTools.AssertNotNull(part);
|
||||
|
||||
if (!part.TryAddMechanism(this))
|
||||
{
|
||||
eventArgs.Target.Value.PopupMessage(eventArgs.User, Loc.GetString("mechanism-component-cannot-fit-message"));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void SendBodyPartListToUser(AfterInteractEventArgs eventArgs, SharedBodyComponent body)
|
||||
{
|
||||
// Create dictionary to send to client (text to be shown : data sent back if selected)
|
||||
var toSend = new Dictionary<string, int>();
|
||||
|
||||
foreach (var (part, slot) in body.Parts)
|
||||
{
|
||||
// For each limb in the target, add it to our cache if it is a valid option.
|
||||
if (part.CanAddMechanism(this))
|
||||
{
|
||||
OptionsCache.Add(IdHash, slot);
|
||||
toSend.Add(part + ": " + part.Name, IdHash++);
|
||||
}
|
||||
}
|
||||
|
||||
if (OptionsCache.Count > 0 &&
|
||||
_entities.TryGetComponent(eventArgs.User, out ActorComponent? actor))
|
||||
{
|
||||
OpenSurgeryUI(actor.PlayerSession);
|
||||
UpdateSurgeryUIBodyPartRequest(actor.PlayerSession, toSend);
|
||||
PerformerCache = eventArgs.User;
|
||||
BodyCache = body;
|
||||
}
|
||||
else // If surgery cannot be performed, show message saying so.
|
||||
{
|
||||
eventArgs.Target?.PopupMessage(eventArgs.User,
|
||||
Loc.GetString("mechanism-component-no-way-to-install-message", ("partName", Name: _entities.GetComponent<MetaDataComponent>(Owner).EntityName)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called after the client chooses from a list of possible BodyParts that can be operated on.
|
||||
/// </summary>
|
||||
private void HandleReceiveBodyPart(int key)
|
||||
{
|
||||
if (PerformerCache == null ||
|
||||
!_entities.TryGetComponent(PerformerCache.Value, out ActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CloseSurgeryUI(actor.PlayerSession);
|
||||
|
||||
if (BodyCache == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: sanity checks to see whether user is in range, user is still able-bodied, target is still the same, etc etc
|
||||
if (!OptionsCache.TryGetValue(key, out var targetObject))
|
||||
{
|
||||
BodyCache.Owner.PopupMessage(PerformerCache.Value,
|
||||
Loc.GetString("mechanism-component-no-useful-way-to-use-message",("partName", Name: _entities.GetComponent<MetaDataComponent>(Owner).EntityName)));
|
||||
return;
|
||||
}
|
||||
|
||||
var target = (SharedBodyPartComponent) targetObject;
|
||||
var message = target.TryAddMechanism(this)
|
||||
? Loc.GetString("mechanism-component-jam-inside-message",("ownerName", Owner),("them", PerformerCache))
|
||||
: Loc.GetString("mechanism-component-cannot-fit-message");
|
||||
|
||||
BodyCache.Owner.PopupMessage(PerformerCache.Value, message);
|
||||
|
||||
// TODO: {1:theName}
|
||||
}
|
||||
|
||||
private void OpenSurgeryUI(IPlayerSession session)
|
||||
{
|
||||
UserInterface?.Open(session);
|
||||
}
|
||||
|
||||
private void UpdateSurgeryUIBodyPartRequest(IPlayerSession session, Dictionary<string, int> options)
|
||||
{
|
||||
UserInterface?.SendMessage(new RequestBodyPartSurgeryUIMessage(options), session);
|
||||
}
|
||||
|
||||
private void CloseSurgeryUI(IPlayerSession session)
|
||||
{
|
||||
UserInterface?.Close(session);
|
||||
}
|
||||
|
||||
private void CloseAllSurgeryUIs()
|
||||
{
|
||||
UserInterface?.CloseAll();
|
||||
}
|
||||
|
||||
private void OnUIMessage(ServerBoundUserInterfaceMessage message)
|
||||
{
|
||||
switch (message.Message)
|
||||
{
|
||||
case ReceiveBodyPartSurgeryUIMessage msg:
|
||||
HandleReceiveBodyPart(msg.SelectedOptionId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user