Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traits Refactor #765

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Content.Server/Abilities/Psionics/PsionicAbilitiesSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ public void InitializePsionicPower(EntityUid uid, PsionicPowerPrototype proto, P
/// <param name="playPopup"></param>
public void InitializePsionicPower(EntityUid uid, PsionicPowerPrototype proto, bool playPopup = true)
{
if (!TryComp<PsionicComponent>(uid, out var psionic))
return;
EnsureComp<PsionicComponent>(uid, out var psionic);

InitializePsionicPower(uid, proto, psionic, playPopup);
}
Expand Down
84 changes: 72 additions & 12 deletions Content.Server/Traits/TraitSystem.cs
VMSolidus marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using System.Linq;
using Content.Shared.Actions;
using Content.Server.GameTicking;
using Content.Server.Players.PlayTimeTracking;
using Content.Shared.Customization.Systems;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Players;
using Content.Shared.Roles;
using Content.Shared.Traits;
using Pidgin.Configuration;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager;
using Content.Server.Abilities.Psionics;

namespace Content.Server.Traits;

public sealed class TraitSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly ISerializationManager _serialization = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly CharacterRequirementsSystem _characterRequirements = default!;
[Dependency] private readonly PlayTimeTrackingManager _playTimeTracking = default!;
[Dependency] private readonly IConfigurationManager _configuration = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly PsionicAbilitiesSystem _psionicAbilities = default!;

public override void Initialize()
{
Expand Down Expand Up @@ -49,16 +49,76 @@ private void OnPlayerSpawnComplete(PlayerSpawnCompleteEvent args)
out _))
continue;

// Add all components required by the prototype
foreach (var entry in traitPrototype.Components.Values)
{
if (HasComp(args.Mob, entry.Component.GetType()))
continue;
AddTrait(args.Mob, traitPrototype);
}
}

/// <summary>
/// Adds a single Trait Prototype to an Entity.
/// </summary>
/// <param name="uid"></param>
/// <param name="traitPrototype"></param>
public void AddTrait(EntityUid uid, TraitPrototype traitPrototype)
{
AddTraitComponents(uid, traitPrototype);
AddTraitActions(uid, traitPrototype);
AddTraitPsionics(uid, traitPrototype);
}

/// <summary>
/// Adds all Components included with a Trait.
/// </summary>
/// <param name="uid"></param>
/// <param name="traitPrototype"></param>
public void AddTraitComponents(EntityUid uid, TraitPrototype traitPrototype)
{
if (traitPrototype.Components is null)
return;

foreach (var entry in traitPrototype.Components.Values)
{
if (HasComp(uid, entry.Component.GetType()))
continue;

var comp = (Component) _serialization.CreateCopy(entry.Component, notNullableOverride: true);
comp.Owner = args.Mob;
EntityManager.AddComponent(args.Mob, comp);
var comp = (Component) _serialization.CreateCopy(entry.Component, notNullableOverride: true);
comp.Owner = uid;
EntityManager.AddComponent(uid, comp);
}
}

/// <summary>
/// Add all actions associated with a specific Trait
/// </summary>
/// <param name="uid"></param>
/// <param name="proto"></param>
/// <param name="psionic"></param>
public void AddTraitActions(EntityUid uid, TraitPrototype traitPrototype)
{
if (traitPrototype.Actions is null)
return;

foreach (var id in traitPrototype.Actions)
{
EntityUid? actionId = null;
if (_actions.AddAction(uid, ref actionId, id))
{
_actions.StartUseDelay(actionId);
}
}
}

/// <summary>
/// If a trait includes any Psionic Powers, this enters the powers into PsionicSystem to be initialized.
/// If the lack of logic here seems startling, it's okay. All of the logic necessary for adding Psionics is handled by InitializePsionicPower.
/// </summary>
/// <param name="uid"></param>
/// <param name="traitPrototype"></param>
public void AddTraitPsionics(EntityUid uid, TraitPrototype traitPrototype)
{
if (traitPrototype.PsionicPowers is null)
return;

foreach (var powerProto in traitPrototype.PsionicPowers)
_psionicAbilities.InitializePsionicPower(uid, powerProto, false);
}
}
18 changes: 15 additions & 3 deletions Content.Shared/Traits/Prototypes/TraitPrototype.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Content.Shared.Customization.Systems;
using Content.Shared.Whitelist;
using Content.Shared.Psionics;
using Robust.Shared.Prototypes;

namespace Content.Shared.Traits;
Expand Down Expand Up @@ -34,6 +34,18 @@ public sealed partial class TraitPrototype : IPrototype
/// <summary>
/// The components that get added to the player when they pick this trait.
/// </summary>
[DataField(required: true)]
public ComponentRegistry Components { get; private set; } = default!;
[DataField]
public ComponentRegistry? Components { get; private set; } = default!;

/// <summary>
/// The list of each Action that this trait adds in the form of ActionId and ActionEntity
/// </summary>
[DataField]
public List<EntProtoId>? Actions { get; private set; } = default!;

/// <summary>
/// The list of all Psionic Powers that this trait adds. If this list is not empty, the trait will also Ensure that a player is Psionic.
/// </summary>
[DataField]
public List<PsionicPowerPrototype>? PsionicPowers { get; private set; } = default!;
}
Loading