Turn brain behavior into a component/system (#5281)

This commit is contained in:
mirrorcult
2021-11-12 23:47:50 -07:00
committed by GitHub
parent 753d198c1a
commit 828025d020
7 changed files with 172 additions and 73 deletions

View File

@@ -1,70 +0,0 @@
using Content.Server.Ghost;
using Content.Server.Ghost.Components;
using Content.Server.Mind.Components;
using Content.Shared.Body.Components;
using Content.Shared.Body.Part;
using Content.Shared.Movement.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.Body.Behavior
{
public class BrainBehavior : MechanismBehavior
{
protected override void OnAddedToBody(SharedBodyComponent body)
{
base.OnAddedToBody(body);
HandleMind(body.Owner, Owner);
}
protected override void OnAddedToPart(SharedBodyPartComponent part)
{
base.OnAddedToPart(part);
HandleMind(part.Owner, Owner);
}
protected override void OnAddedToPartInBody(SharedBodyComponent body, SharedBodyPartComponent part)
{
base.OnAddedToPartInBody(body, part);
HandleMind(body.Owner, Owner);
}
protected override void OnRemovedFromBody(SharedBodyComponent old)
{
base.OnRemovedFromBody(old);
HandleMind(Part!.Owner, old.Owner);
}
protected override void OnRemovedFromPart(SharedBodyPartComponent old)
{
base.OnRemovedFromPart(old);
HandleMind(Owner, old.Owner);
}
protected override void OnRemovedFromPartInBody(SharedBodyComponent oldBody, SharedBodyPartComponent oldPart)
{
base.OnRemovedFromPartInBody(oldBody, oldPart);
HandleMind(oldBody.Owner, Owner);
}
private void HandleMind(IEntity newEntity, IEntity oldEntity)
{
newEntity.EnsureComponent<MindComponent>();
var oldMind = oldEntity.EnsureComponent<MindComponent>();
if (!newEntity.HasComponent<IGhostOnMove>())
newEntity.AddComponent<GhostOnMoveComponent>();
// TODO: This is an awful solution.
if (!newEntity.HasComponent<IMoverComponent>())
newEntity.AddComponent<SharedDummyInputMoverComponent>();
oldMind.Mind?.TransferTo(newEntity);
}
}
}

View File

@@ -0,0 +1,12 @@
using Content.Server.Body.Systems;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
namespace Content.Server.Body.Components
{
[RegisterComponent, Friend(typeof(BrainSystem))]
public class BrainComponent : Component
{
public override string Name => "Brain";
}
}

View File

@@ -0,0 +1,49 @@
using Content.Server.Body.Components;
using Content.Server.Ghost;
using Content.Server.Ghost.Components;
using Content.Server.Mind.Components;
using Content.Shared.Body.Events;
using Content.Shared.Movement.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.Body.Systems
{
public class BrainSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BrainComponent, AddedToBodyEvent>((uid, component, args) => HandleMind(args.Body.OwnerUid, uid));
SubscribeLocalEvent<BrainComponent, AddedToPartEvent>((uid, component, args) => HandleMind(args.Part.OwnerUid, uid));
SubscribeLocalEvent<BrainComponent, AddedToPartInBodyEvent>((uid, component, args) => HandleMind(args.Body.OwnerUid, uid));
SubscribeLocalEvent<BrainComponent, RemovedFromBodyEvent>(OnRemovedFromBody);
SubscribeLocalEvent<BrainComponent, RemovedFromPartEvent>((uid, component, args) => HandleMind(uid, args.Old.OwnerUid));
SubscribeLocalEvent<BrainComponent, RemovedFromPartInBodyEvent>((uid, component, args) => HandleMind(args.OldBody.OwnerUid, uid));
}
private void OnRemovedFromBody(EntityUid uid, BrainComponent component, RemovedFromBodyEvent args)
{
// This one needs to be special, okay?
if (!EntityManager.TryGetComponent(uid, out MechanismComponent mech))
return;
HandleMind(mech.Part!.OwnerUid, args.Old.OwnerUid);
}
private void HandleMind(EntityUid newEntity, EntityUid oldEntity)
{
EntityManager.EnsureComponent<MindComponent>(newEntity);
var oldMind = EntityManager.EnsureComponent<MindComponent>(oldEntity);
if (!EntityManager.HasComponent<IGhostOnMove>(newEntity))
EntityManager.AddComponent<GhostOnMoveComponent>(newEntity);
// TODO: This is an awful solution.
if (!EntityManager.HasComponent<IMoverComponent>(newEntity))
EntityManager.AddComponent<SharedDummyInputMoverComponent>(newEntity);
oldMind.Mind?.TransferTo(EntityManager.GetEntity(newEntity));
}
}
}