Add health analyzer and medical scanner ECS (#6907)
Co-authored-by: fishfish458 <fishfish458> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
27
Content.Server/Medical/Components/HealthAnalyzerComponent.cs
Normal file
27
Content.Server/Medical/Components/HealthAnalyzerComponent.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Threading;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.MedicalScanner;
|
||||
using Robust.Server.GameObjects;
|
||||
|
||||
namespace Content.Server.Medical.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// After scanning, retrieves the target Uid to use with its related UI.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedHealthAnalyzerComponent))]
|
||||
public sealed class HealthAnalyzerComponent : SharedHealthAnalyzerComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// How long it takes to scan someone.
|
||||
/// </summary>
|
||||
[DataField("scanDelay")]
|
||||
[ViewVariables]
|
||||
public float ScanDelay = 0.8f;
|
||||
/// <summary>
|
||||
/// Token for interrupting scanning do after.
|
||||
/// </summary>
|
||||
public CancellationTokenSource? CancelToken;
|
||||
public BoundUserInterface? UserInterface => Owner.GetUIOrNull(HealthAnalyzerUiKey.Key);
|
||||
}
|
||||
}
|
||||
@@ -1,249 +1,22 @@
|
||||
using System;
|
||||
using Content.Server.Climbing;
|
||||
using Content.Server.Cloning;
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Preferences.Managers;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.DragDrop;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.MedicalScanner;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Preferences;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Medical.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(SharedMedicalScannerComponent))]
|
||||
public sealed class MedicalScannerComponent : SharedMedicalScannerComponent, IActivate, IDestroyAct
|
||||
public sealed class MedicalScannerComponent : SharedMedicalScannerComponent
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entMan = default!;
|
||||
[Dependency] private readonly IServerPreferencesManager _prefsManager = null!;
|
||||
|
||||
public static readonly TimeSpan InternalOpenAttemptDelay = TimeSpan.FromSeconds(0.5);
|
||||
public TimeSpan LastInternalOpenAttempt;
|
||||
|
||||
private ContainerSlot _bodyContainer = default!;
|
||||
|
||||
[ViewVariables]
|
||||
private bool Powered => !_entMan.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
|
||||
[ViewVariables]
|
||||
private BoundUserInterface? UserInterface => Owner.GetUIOrNull(MedicalScannerUiKey.Key);
|
||||
|
||||
public bool IsOccupied => _bodyContainer.ContainedEntity != null;
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
|
||||
}
|
||||
|
||||
_bodyContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-bodyContainer");
|
||||
|
||||
// TODO: write this so that it checks for a change in power events and acts accordingly.
|
||||
var newState = GetUserInterfaceState();
|
||||
UserInterface?.SetState(newState);
|
||||
|
||||
UpdateUserInterface();
|
||||
}
|
||||
|
||||
private static readonly MedicalScannerBoundUserInterfaceState EmptyUIState =
|
||||
new(
|
||||
null,
|
||||
null,
|
||||
false);
|
||||
|
||||
private MedicalScannerBoundUserInterfaceState GetUserInterfaceState()
|
||||
{
|
||||
var body = _bodyContainer.ContainedEntity;
|
||||
if (body == null)
|
||||
{
|
||||
if (_entMan.TryGetComponent(Owner, out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance?.SetData(MedicalScannerVisuals.Status, MedicalScannerStatus.Open);
|
||||
}
|
||||
|
||||
return EmptyUIState;
|
||||
}
|
||||
|
||||
if (!_entMan.TryGetComponent(body.Value, out DamageableComponent? damageable))
|
||||
{
|
||||
return EmptyUIState;
|
||||
}
|
||||
|
||||
if (_bodyContainer.ContainedEntity == null)
|
||||
{
|
||||
return new MedicalScannerBoundUserInterfaceState(body, damageable, true);
|
||||
}
|
||||
|
||||
var cloningSystem = EntitySystem.Get<CloningSystem>();
|
||||
var scanned = _entMan.TryGetComponent(_bodyContainer.ContainedEntity.Value, out MindComponent? mindComponent) &&
|
||||
mindComponent.Mind != null &&
|
||||
cloningSystem.HasDnaScan(mindComponent.Mind);
|
||||
|
||||
return new MedicalScannerBoundUserInterfaceState(body, damageable, scanned);
|
||||
}
|
||||
|
||||
private void UpdateUserInterface()
|
||||
{
|
||||
if (!Powered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newState = GetUserInterfaceState();
|
||||
UserInterface?.SetState(newState);
|
||||
}
|
||||
|
||||
private MedicalScannerStatus GetStatusFromDamageState(MobStateComponent state)
|
||||
{
|
||||
if (state.IsAlive())
|
||||
{
|
||||
return MedicalScannerStatus.Green;
|
||||
}
|
||||
else if (state.IsCritical())
|
||||
{
|
||||
return MedicalScannerStatus.Red;
|
||||
}
|
||||
else if (state.IsDead())
|
||||
{
|
||||
return MedicalScannerStatus.Death;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MedicalScannerStatus.Yellow;
|
||||
}
|
||||
}
|
||||
|
||||
private MedicalScannerStatus GetStatus()
|
||||
{
|
||||
if (Powered)
|
||||
{
|
||||
var body = _bodyContainer.ContainedEntity;
|
||||
if (body == null)
|
||||
return MedicalScannerStatus.Open;
|
||||
|
||||
var state = _entMan.GetComponentOrNull<MobStateComponent>(body.Value);
|
||||
|
||||
return state == null ? MedicalScannerStatus.Open : GetStatusFromDamageState(state);
|
||||
}
|
||||
|
||||
return MedicalScannerStatus.Off;
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
if (_entMan.TryGetComponent(Owner, out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(MedicalScannerVisuals.Status, GetStatus());
|
||||
}
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs args)
|
||||
{
|
||||
if (!_entMan.TryGetComponent(args.User, out ActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Powered)
|
||||
return;
|
||||
|
||||
UserInterface?.Open(actor.PlayerSession);
|
||||
}
|
||||
|
||||
public void InsertBody(EntityUid user)
|
||||
{
|
||||
_bodyContainer.Insert(user);
|
||||
UpdateUserInterface();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public void EjectBody()
|
||||
{
|
||||
if (_bodyContainer.ContainedEntity is not {Valid: true} contained) return;
|
||||
_bodyContainer.Remove(contained);
|
||||
UpdateUserInterface();
|
||||
UpdateAppearance();
|
||||
EntitySystem.Get<ClimbSystem>().ForciblySetClimbing(contained);
|
||||
}
|
||||
|
||||
public void Update(float frameTime)
|
||||
{
|
||||
UpdateUserInterface();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
||||
{
|
||||
if (obj.Message is not UiButtonPressedMessage message || obj.Session.AttachedEntity == null) return;
|
||||
|
||||
switch (message.Button)
|
||||
{
|
||||
case UiButton.ScanDNA:
|
||||
if (_bodyContainer.ContainedEntity != null)
|
||||
{
|
||||
var cloningSystem = EntitySystem.Get<CloningSystem>();
|
||||
|
||||
if (!_entMan.TryGetComponent(_bodyContainer.ContainedEntity.Value, out MindComponent? mindComp) || mindComp.Mind == null)
|
||||
{
|
||||
obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("medical-scanner-component-msg-no-soul"));
|
||||
break;
|
||||
}
|
||||
|
||||
// Null suppression based on above check. Yes, it's explicitly needed
|
||||
var mind = mindComp.Mind!;
|
||||
|
||||
// We need the HumanoidCharacterProfile
|
||||
// TODO: Move this further 'outwards' into a DNAComponent or somesuch.
|
||||
// Ideally this ends with GameTicker & CloningSystem handing DNA to a function that sets up a body for that DNA.
|
||||
var mindUser = mind.UserId;
|
||||
|
||||
if (mindUser.HasValue == false || mind.Session == null)
|
||||
{
|
||||
// For now assume this means soul departed
|
||||
obj.Session.AttachedEntity.Value.PopupMessageCursor(Loc.GetString("medical-scanner-component-msg-soul-broken"));
|
||||
break;
|
||||
}
|
||||
|
||||
var profile = GetPlayerProfileAsync(mindUser.Value);
|
||||
cloningSystem.AddToDnaScans(new ClonerDNAEntry(mind, profile));
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
public ContainerSlot BodyContainer = default!;
|
||||
public BoundUserInterface? UserInterface => Owner.GetUIOrNull(MedicalScannerUiKey.Key);
|
||||
|
||||
// ECS this out!, when DragDropSystem and InteractionSystem refactored
|
||||
public override bool DragDropOn(DragDropEvent eventArgs)
|
||||
{
|
||||
_bodyContainer.Insert(eventArgs.Dragged);
|
||||
return true;
|
||||
}
|
||||
|
||||
void IDestroyAct.OnDestroy(DestructionEventArgs eventArgs)
|
||||
{
|
||||
EjectBody();
|
||||
}
|
||||
|
||||
private HumanoidCharacterProfile GetPlayerProfileAsync(NetUserId userId)
|
||||
{
|
||||
return (HumanoidCharacterProfile) _prefsManager.GetPreferences(userId).SelectedCharacter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
120
Content.Server/Medical/HealthAnalyzerSystem.cs
Normal file
120
Content.Server/Medical/HealthAnalyzerSystem.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using System.Threading;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Medical.Components;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using static Content.Shared.MedicalScanner.SharedHealthAnalyzerComponent;
|
||||
|
||||
namespace Content.Server.Medical
|
||||
{
|
||||
public sealed class HealthAnalyzerSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<HealthAnalyzerComponent, ActivateInWorldEvent>(HandleActivateInWorld);
|
||||
SubscribeLocalEvent<HealthAnalyzerComponent, AfterInteractEvent>(OnAfterInteract);
|
||||
SubscribeLocalEvent<TargetScanSuccessfulEvent>(OnTargetScanSuccessful);
|
||||
SubscribeLocalEvent<ScanCancelledEvent>(OnScanCancelled);
|
||||
}
|
||||
|
||||
private void HandleActivateInWorld(EntityUid uid, HealthAnalyzerComponent healthAnalyzer, ActivateInWorldEvent args)
|
||||
{
|
||||
OpenUserInterface(args.User, healthAnalyzer);
|
||||
}
|
||||
|
||||
private void OnAfterInteract(EntityUid uid, HealthAnalyzerComponent healthAnalyzer, AfterInteractEvent args)
|
||||
{
|
||||
if (healthAnalyzer.CancelToken != null)
|
||||
{
|
||||
healthAnalyzer.CancelToken.Cancel();
|
||||
healthAnalyzer.CancelToken = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.Target == null)
|
||||
return;
|
||||
|
||||
if (!args.CanReach)
|
||||
return;
|
||||
|
||||
if (healthAnalyzer.CancelToken != null)
|
||||
return;
|
||||
|
||||
if (!HasComp<MobStateComponent>(args.Target))
|
||||
return;
|
||||
|
||||
healthAnalyzer.CancelToken = new CancellationTokenSource();
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, healthAnalyzer.ScanDelay, healthAnalyzer.CancelToken.Token, target: args.Target)
|
||||
{
|
||||
BroadcastFinishedEvent = new TargetScanSuccessfulEvent(args.User, args.Target, healthAnalyzer),
|
||||
BroadcastCancelledEvent = new ScanCancelledEvent(healthAnalyzer),
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
BreakOnStun = true,
|
||||
NeedHand = true
|
||||
});
|
||||
}
|
||||
|
||||
private void OnTargetScanSuccessful(TargetScanSuccessfulEvent args)
|
||||
{
|
||||
args.Component.CancelToken = null;
|
||||
UpdateScannedUser(args.Component.Owner, args.User, args.Target, args.Component);
|
||||
}
|
||||
|
||||
private void OpenUserInterface(EntityUid user, HealthAnalyzerComponent healthAnalyzer)
|
||||
{
|
||||
if (!TryComp<ActorComponent>(user, out var actor))
|
||||
return;
|
||||
|
||||
healthAnalyzer.UserInterface?.Open(actor.PlayerSession);
|
||||
}
|
||||
|
||||
public void UpdateScannedUser(EntityUid uid, EntityUid user, EntityUid? target, HealthAnalyzerComponent? healthAnalyzer)
|
||||
{
|
||||
if (!Resolve(uid, ref healthAnalyzer))
|
||||
return;
|
||||
|
||||
if (target == null || healthAnalyzer.UserInterface == null)
|
||||
return;
|
||||
|
||||
if (!HasComp<DamageableComponent>(target))
|
||||
return;
|
||||
|
||||
OpenUserInterface(user, healthAnalyzer);
|
||||
healthAnalyzer.UserInterface?.SendMessage(new HealthAnalyzerScannedUserMessage(target));
|
||||
}
|
||||
|
||||
private static void OnScanCancelled(ScanCancelledEvent args)
|
||||
{
|
||||
args.HealthAnalyzer.CancelToken = null;
|
||||
}
|
||||
|
||||
private sealed class ScanCancelledEvent : EntityEventArgs
|
||||
{
|
||||
public readonly HealthAnalyzerComponent HealthAnalyzer;
|
||||
public ScanCancelledEvent(HealthAnalyzerComponent healthAnalyzer)
|
||||
{
|
||||
HealthAnalyzer = healthAnalyzer;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TargetScanSuccessfulEvent : EntityEventArgs
|
||||
{
|
||||
public EntityUid User { get; }
|
||||
public EntityUid? Target { get; }
|
||||
public HealthAnalyzerComponent Component { get; }
|
||||
|
||||
public TargetScanSuccessfulEvent(EntityUid user, EntityUid? target, HealthAnalyzerComponent component)
|
||||
{
|
||||
User = user;
|
||||
Target = target;
|
||||
Component = component;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,77 @@
|
||||
using Content.Server.Climbing;
|
||||
using Content.Server.Cloning;
|
||||
using Content.Server.Medical.Components;
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Server.Popups;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Preferences.Managers;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Shared.CharacterAppearance.Components;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.DragDrop;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Shared.Movement;
|
||||
using Content.Shared.Preferences;
|
||||
using Content.Shared.Verbs;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Player;
|
||||
using static Content.Shared.MedicalScanner.SharedMedicalScannerComponent;
|
||||
|
||||
namespace Content.Server.Medical
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class MedicalScannerSystem : EntitySystem
|
||||
public sealed class MedicalScannerSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IServerPreferencesManager _prefsManager = null!;
|
||||
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
|
||||
[Dependency] private readonly ClimbSystem _climbSystem = default!;
|
||||
[Dependency] private readonly CloningSystem _cloningSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
|
||||
private const float UpdateRate = 1f;
|
||||
private float _updateDif;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<MedicalScannerComponent, ComponentInit>(OnComponentInit);
|
||||
SubscribeLocalEvent<MedicalScannerComponent, ActivateInWorldEvent>(OnActivated);
|
||||
SubscribeLocalEvent<MedicalScannerComponent, RelayMovementEntityEvent>(OnRelayMovement);
|
||||
SubscribeLocalEvent<MedicalScannerComponent, GetVerbsEvent<InteractionVerb>>(AddInsertOtherVerb);
|
||||
SubscribeLocalEvent<MedicalScannerComponent, GetVerbsEvent<AlternativeVerb>>(AddAlternativeVerbs);
|
||||
SubscribeLocalEvent<MedicalScannerComponent, DestructionEventArgs>(OnDestroyed);
|
||||
SubscribeLocalEvent<MedicalScannerComponent, DragDropEvent>(HandleDragDropOn);
|
||||
SubscribeLocalEvent<MedicalScannerComponent, ScanButtonPressedMessage>(OnScanButtonPressed);
|
||||
}
|
||||
|
||||
private void OnComponentInit(EntityUid uid, MedicalScannerComponent scannerComponent, ComponentInit args)
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
scannerComponent.BodyContainer = scannerComponent.Owner.EnsureContainer<ContainerSlot>($"{scannerComponent.Name}-bodyContainer");
|
||||
UpdateUserInterface(uid, scannerComponent);
|
||||
}
|
||||
|
||||
private void OnActivated(EntityUid uid, MedicalScannerComponent scannerComponent, ActivateInWorldEvent args)
|
||||
{
|
||||
if (!TryComp<ActorComponent>(args.User, out var actor) || !IsPowered(scannerComponent))
|
||||
return;
|
||||
|
||||
scannerComponent.UserInterface?.Toggle(actor.PlayerSession);
|
||||
UpdateUserInterface(uid, scannerComponent);
|
||||
}
|
||||
|
||||
private void OnRelayMovement(EntityUid uid, MedicalScannerComponent scannerComponent, RelayMovementEntityEvent args)
|
||||
{
|
||||
if (!_blocker.CanInteract(args.Entity, scannerComponent.Owner))
|
||||
return;
|
||||
|
||||
EjectBody(uid, scannerComponent);
|
||||
}
|
||||
|
||||
private void AddInsertOtherVerb(EntityUid uid, MedicalScannerComponent component, GetVerbsEvent<InteractionVerb> args)
|
||||
@@ -31,14 +79,20 @@ namespace Content.Server.Medical
|
||||
if (args.Using == null ||
|
||||
!args.CanAccess ||
|
||||
!args.CanInteract ||
|
||||
component.IsOccupied ||
|
||||
IsOccupied(component) ||
|
||||
!component.CanInsert(args.Using.Value))
|
||||
return;
|
||||
|
||||
InteractionVerb verb = new();
|
||||
verb.Act = () => component.InsertBody(args.Using.Value);
|
||||
verb.Category = VerbCategory.Insert;
|
||||
verb.Text = EntityManager.GetComponent<MetaDataComponent>(args.Using.Value).EntityName;
|
||||
string name = "Unknown";
|
||||
if (TryComp<MetaDataComponent>(args.Using.Value, out var metadata))
|
||||
name = metadata.EntityName;
|
||||
|
||||
InteractionVerb verb = new()
|
||||
{
|
||||
Act = () => InsertBody(component.Owner, args.Target, component),
|
||||
Category = VerbCategory.Insert,
|
||||
Text = name
|
||||
};
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
@@ -48,52 +102,226 @@ namespace Content.Server.Medical
|
||||
return;
|
||||
|
||||
// Eject verb
|
||||
if (component.IsOccupied)
|
||||
if (IsOccupied(component))
|
||||
{
|
||||
AlternativeVerb verb = new();
|
||||
verb.Act = () => component.EjectBody();
|
||||
verb.Act = () => EjectBody(uid, component);
|
||||
verb.Category = VerbCategory.Eject;
|
||||
verb.Text = Loc.GetString("medical-scanner-verb-noun-occupant");
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
// Self-insert verb
|
||||
if (!component.IsOccupied &&
|
||||
if (!IsOccupied(component) &&
|
||||
component.CanInsert(args.User) &&
|
||||
_actionBlockerSystem.CanMove(args.User))
|
||||
_blocker.CanMove(args.User))
|
||||
{
|
||||
AlternativeVerb verb = new();
|
||||
verb.Act = () => component.InsertBody(args.User);
|
||||
verb.Act = () => InsertBody(component.Owner, args.User, component);
|
||||
verb.Text = Loc.GetString("medical-scanner-verb-enter");
|
||||
// TODO VERN ICON
|
||||
// TODO VERB CATEGORY
|
||||
// create a verb category for "enter"?
|
||||
// See also, disposal unit. Also maybe add verbs for entering lockers/body bags?
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRelayMovement(EntityUid uid, MedicalScannerComponent component, RelayMovementEntityEvent args)
|
||||
private void OnDestroyed(EntityUid uid, MedicalScannerComponent scannerComponent, DestructionEventArgs args)
|
||||
{
|
||||
if (_blocker.CanInteract(args.Entity, null))
|
||||
EjectBody(uid, scannerComponent);
|
||||
}
|
||||
|
||||
private void HandleDragDropOn(EntityUid uid, MedicalScannerComponent scannerComponent, DragDropEvent args)
|
||||
{
|
||||
InsertBody(uid, args.Dragged, scannerComponent);
|
||||
}
|
||||
|
||||
private void OnScanButtonPressed(EntityUid uid, MedicalScannerComponent scannerComponent, ScanButtonPressedMessage args)
|
||||
{
|
||||
TrySaveCloningData(uid, scannerComponent);
|
||||
}
|
||||
|
||||
private static readonly MedicalScannerBoundUserInterfaceState EmptyUIState =
|
||||
new(false);
|
||||
|
||||
private MedicalScannerBoundUserInterfaceState GetUserInterfaceState(EntityUid uid, MedicalScannerComponent scannerComponent)
|
||||
{
|
||||
EntityUid? containedBody = scannerComponent.BodyContainer.ContainedEntity;
|
||||
|
||||
if (containedBody == null)
|
||||
{
|
||||
if (_gameTiming.CurTime <
|
||||
component.LastInternalOpenAttempt + MedicalScannerComponent.InternalOpenAttemptDelay)
|
||||
UpdateAppearance(uid, scannerComponent);
|
||||
return EmptyUIState;
|
||||
}
|
||||
|
||||
if (!HasComp<DamageableComponent>(containedBody))
|
||||
return EmptyUIState;
|
||||
|
||||
if (!HasComp<HumanoidAppearanceComponent>(containedBody))
|
||||
return EmptyUIState;
|
||||
|
||||
if (!TryComp<MindComponent>(containedBody, out var mindComponent) || mindComponent.Mind == null)
|
||||
return EmptyUIState;
|
||||
|
||||
bool isScanned = _cloningSystem.HasDnaScan(mindComponent.Mind);
|
||||
|
||||
return new MedicalScannerBoundUserInterfaceState(!isScanned);
|
||||
}
|
||||
|
||||
private void UpdateUserInterface(EntityUid uid, MedicalScannerComponent scannerComponent)
|
||||
{
|
||||
if (!IsPowered(scannerComponent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newState = GetUserInterfaceState(uid, scannerComponent);
|
||||
scannerComponent.UserInterface?.SetState(newState);
|
||||
}
|
||||
|
||||
private MedicalScannerStatus GetStatus(MedicalScannerComponent scannerComponent)
|
||||
{
|
||||
if (IsPowered(scannerComponent))
|
||||
{
|
||||
var body = scannerComponent.BodyContainer.ContainedEntity;
|
||||
if (body == null)
|
||||
return MedicalScannerStatus.Open;
|
||||
|
||||
if (!TryComp<MobStateComponent>(body.Value, out var state))
|
||||
{
|
||||
return;
|
||||
return MedicalScannerStatus.Open;
|
||||
}
|
||||
|
||||
component.LastInternalOpenAttempt = _gameTiming.CurTime;
|
||||
component.EjectBody();
|
||||
return GetStatusFromDamageState(state);
|
||||
}
|
||||
return MedicalScannerStatus.Off;
|
||||
}
|
||||
|
||||
public bool IsPowered(MedicalScannerComponent scannerComponent)
|
||||
{
|
||||
if (TryComp<ApcPowerReceiverComponent>(scannerComponent.Owner, out var receiver))
|
||||
{
|
||||
return receiver.Powered;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsOccupied(MedicalScannerComponent scannerComponent)
|
||||
{
|
||||
return scannerComponent.BodyContainer.ContainedEntity != null;
|
||||
}
|
||||
|
||||
private MedicalScannerStatus GetStatusFromDamageState(MobStateComponent state)
|
||||
{
|
||||
if (state.IsAlive())
|
||||
return MedicalScannerStatus.Green;
|
||||
|
||||
if (state.IsCritical())
|
||||
return MedicalScannerStatus.Red;
|
||||
|
||||
if (state.IsDead())
|
||||
return MedicalScannerStatus.Death;
|
||||
|
||||
return MedicalScannerStatus.Yellow;
|
||||
}
|
||||
|
||||
private void UpdateAppearance(EntityUid uid, MedicalScannerComponent scannerComponent)
|
||||
{
|
||||
if (TryComp<AppearanceComponent>(scannerComponent.Owner, out var appearance))
|
||||
{
|
||||
appearance.SetData(MedicalScannerVisuals.Status, GetStatus(scannerComponent));
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
foreach (var comp in EntityManager.EntityQuery<MedicalScannerComponent>())
|
||||
base.Update(frameTime);
|
||||
|
||||
_updateDif += frameTime;
|
||||
if (_updateDif < UpdateRate)
|
||||
return;
|
||||
|
||||
_updateDif -= UpdateRate;
|
||||
|
||||
foreach (var scanner in EntityQuery<MedicalScannerComponent>())
|
||||
{
|
||||
comp.Update(frameTime);
|
||||
UpdateAppearance(scanner.Owner, scanner);
|
||||
}
|
||||
}
|
||||
|
||||
public void InsertBody(EntityUid uid, EntityUid user, MedicalScannerComponent? scannerComponent)
|
||||
{
|
||||
if (!Resolve(uid, ref scannerComponent))
|
||||
return;
|
||||
|
||||
if (scannerComponent.BodyContainer.ContainedEntity != null)
|
||||
return;
|
||||
|
||||
if (!TryComp<MobStateComponent>(user, out var comp))
|
||||
return;
|
||||
|
||||
scannerComponent.BodyContainer.Insert(user);
|
||||
UpdateUserInterface(uid, scannerComponent);
|
||||
UpdateAppearance(scannerComponent.Owner, scannerComponent);
|
||||
}
|
||||
|
||||
public void EjectBody(EntityUid uid, MedicalScannerComponent? scannerComponent)
|
||||
{
|
||||
if (!Resolve(uid, ref scannerComponent))
|
||||
return;
|
||||
|
||||
if (scannerComponent.BodyContainer.ContainedEntity is not {Valid: true} contained) return;
|
||||
|
||||
scannerComponent.BodyContainer.Remove(contained);
|
||||
_climbSystem.ForciblySetClimbing(contained);
|
||||
UpdateUserInterface(uid, scannerComponent);
|
||||
UpdateAppearance(scannerComponent.Owner, scannerComponent);
|
||||
}
|
||||
|
||||
public void TrySaveCloningData(EntityUid uid, MedicalScannerComponent? scannerComponent)
|
||||
{
|
||||
if (!Resolve(uid, ref scannerComponent))
|
||||
return;
|
||||
|
||||
EntityUid? body = scannerComponent.BodyContainer.ContainedEntity;
|
||||
|
||||
if (body == null)
|
||||
return;
|
||||
|
||||
// Check to see if they are humanoid
|
||||
if (!TryComp<HumanoidAppearanceComponent>(body, out var humanoid))
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("medical-scanner-component-msg-no-humanoid-component"), uid, Filter.Pvs(uid));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryComp<MindComponent>(body, out var mindComp) || mindComp.Mind == null)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("medical-scanner-component-msg-no-soul"), uid, Filter.Pvs(uid));
|
||||
return;
|
||||
}
|
||||
|
||||
// Null suppression based on above check. Yes, it's explicitly needed
|
||||
var mind = mindComp.Mind;
|
||||
// We need the HumanoidCharacterProfile
|
||||
// TODO: Move this further 'outwards' into a DNAComponent or somesuch.
|
||||
// Ideally this ends with GameTicker & CloningSystem handing DNA to a function that sets up a body for that DNA.
|
||||
var mindUser = mind.UserId;
|
||||
|
||||
if (mindUser.HasValue == false || mind.Session == null)
|
||||
{
|
||||
// For now assume this means soul departed
|
||||
_popupSystem.PopupEntity(Loc.GetString("medical-scanner-component-msg-soul-broken"), uid, Filter.Pvs(uid));
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO get synchronously
|
||||
// This must be changed to grab the details of the mob itself, not session preferences
|
||||
var profile = GetPlayerProfileAsync(mindUser.Value);
|
||||
_cloningSystem.AddToDnaScans(new ClonerDNAEntry(mind, profile));
|
||||
UpdateUserInterface(uid, scannerComponent);
|
||||
}
|
||||
|
||||
private HumanoidCharacterProfile GetPlayerProfileAsync(NetUserId userId)
|
||||
{
|
||||
return (HumanoidCharacterProfile) _prefsManager.GetPreferences(userId).SelectedCharacter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user