fish-station/Content.Server/Body/Systems/BrainSystem.cs
2025-09-27 00:48:54 +03:00

59 lines
2.1 KiB
C#

using Content.Server.Body.Components;
using Content.Server.Ghost.Components;
using Content.Shared.Body.Events;
using Content.Shared.Mind;
using Content.Shared.Mind.Components;
using Content.Shared.Mobs.Components;
using Content.Shared.Pointing;
namespace Content.Server.Body.Systems;
public sealed class BrainSystem : EntitySystem
{
[Dependency] private readonly SharedMindSystem _mindSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BrainComponent, ComponentInit>(OnBrainInit);
SubscribeLocalEvent<BrainComponent, OrganAddedToBodyEvent>((uid, _, args) => HandleMind(args.Body, uid));
SubscribeLocalEvent<BrainComponent, OrganRemovedFromBodyEvent>((uid, _, args) => HandleMind(uid, args.OldBody));
SubscribeLocalEvent<BrainComponent, PointAttemptEvent>(OnPointAttempt);
}
private void HandleMind(EntityUid newEntity, EntityUid oldEntity)
{
if (TerminatingOrDeleted(newEntity) || TerminatingOrDeleted(oldEntity))
return;
var newMindContainer = EnsureComp<MindContainerComponent>(newEntity);
var oldMindContainer = EnsureComp<MindContainerComponent>(oldEntity);
// Enable mind examination for brains
_mindSystem.SetExamineInfo(newEntity, true);
_mindSystem.SetExamineInfo(oldEntity, true);
var ghostOnMove = EnsureComp<GhostOnMoveComponent>(newEntity);
ghostOnMove.MustBeDead = HasComp<MobStateComponent>(newEntity); // Don't ghost living players out of their bodies.
if (!_mindSystem.TryGetMind(oldEntity, out var mindId, out var mind))
return;
_mindSystem.TransferTo(mindId, newEntity, mind: mind);
}
private void OnBrainInit(Entity<BrainComponent> ent, ref ComponentInit args)
{
// Ensure brain has mind container with examination enabled
var mindContainer = EnsureComp<MindContainerComponent>(ent);
// Use the mind system to set the examine info
_mindSystem.SetExamineInfo(ent, true);
}
private void OnPointAttempt(Entity<BrainComponent> ent, ref PointAttemptEvent args)
{
args.Cancel();
}
}