SpawnEntityTableOnTrigger (#39909)

* commit

* comment

* empty

* better xform

---------

Co-authored-by: iaada <iaada@users.noreply.github.com>
This commit is contained in:
āda
2025-09-10 11:59:42 -05:00
committed by GitHub
parent ebfcddc62f
commit f7e3a2f881
4 changed files with 78 additions and 13 deletions

View File

@@ -0,0 +1,33 @@
using Content.Shared.EntityTable.EntitySelectors;
using Robust.Shared.GameStates;
namespace Content.Shared.Trigger.Components.Effects;
/// <summary>
/// Spawns an entity table at this entity when triggered.
/// If TargetUser is true it will be spawned at their location.
/// </summary>
/// <seealso cref="SpawnOnTriggerComponent"/>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class SpawnEntityTableOnTriggerComponent : BaseXOnTriggerComponent
{
/// <summary>
/// The table to spawn.
/// </summary>
[DataField(required: true), AutoNetworkedField]
public EntityTableSelector Table;
/// <summary>
/// Use MapCoordinates for spawning?
/// Set to true if you don't want the new entity parented to the spawner.
/// </summary>
[DataField, AutoNetworkedField]
public bool UseMapCoords;
/// <summary>
/// Whether to use predicted spawning.
/// </summary>
/// <remarks>Randomization in EntityTables is not currently predicted! Use with caution.</remarks>
[DataField, AutoNetworkedField]
public bool Predicted;
}

View File

@@ -7,6 +7,7 @@ namespace Content.Shared.Trigger.Components.Effects;
/// Spawns a protoype when triggered.
/// If TargetUser is true it will be spawned at their location.
/// </summary>
/// <seealso cref="SpawnEntityTableOnTriggerComponent"/>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class SpawnOnTriggerComponent : BaseXOnTriggerComponent
{

View File

@@ -1,16 +1,17 @@
using Content.Shared.Trigger.Components.Effects;
using Content.Shared.Trigger.Components.Triggers;
using Robust.Shared.Prototypes;
namespace Content.Shared.Trigger.Systems;
public sealed partial class TriggerSystem
{
private void InitializeSpawn()
{
SubscribeLocalEvent<TriggerOnSpawnComponent, MapInitEvent>(OnSpawnInit);
SubscribeLocalEvent<SpawnOnTriggerComponent, TriggerEvent>(HandleSpawnOnTrigger);
SubscribeLocalEvent<SpawnEntityTableOnTriggerComponent, TriggerEvent>(HandleSpawnTableOnTrigger);
SubscribeLocalEvent<DeleteOnTriggerComponent, TriggerEvent>(HandleDeleteOnTrigger);
}
@@ -30,27 +31,55 @@ public sealed partial class TriggerSystem
return;
var xform = Transform(target.Value);
SpawnTriggerHelper((target.Value, xform), ent.Comp.Proto, ent.Comp.UseMapCoords, ent.Comp.Predicted);
}
if (ent.Comp.UseMapCoords)
private void HandleSpawnTableOnTrigger(Entity<SpawnEntityTableOnTriggerComponent> ent, ref TriggerEvent args)
{
if (args.Key != null && !ent.Comp.KeysIn.Contains(args.Key))
return;
var target = ent.Comp.TargetUser ? args.User : ent.Owner;
if (target == null)
return;
var xform = Transform(target.Value);
var spawns = _entityTable.GetSpawns(ent.Comp.Table);
foreach (var proto in spawns)
{
var mapCoords = _transform.GetMapCoordinates(target.Value, xform);
if (ent.Comp.Predicted)
EntityManager.PredictedSpawn(ent.Comp.Proto, mapCoords);
else if (_net.IsServer)
Spawn(ent.Comp.Proto, mapCoords);
SpawnTriggerHelper((target.Value, xform), proto, ent.Comp.UseMapCoords, ent.Comp.Predicted);
}
}
/// <summary>
/// Helper function to combine HandleSpawnOnTrigger and HandleSpawnTableOnTrigger.
/// </summary>
/// <param name="target">The entity to spawn attached to or at the feet of.</param>
/// <param name="proto">The entity to spawn.</param>
/// <param name="useMapCoords">If true, spawn at target's MapCoordinates. If false, spawn attached to target.</param>
/// <param name="predicted">Whether to use predicted spawning.</param>
private void SpawnTriggerHelper(Entity<TransformComponent> target, EntProtoId proto, bool useMapCoords, bool predicted)
{
if (useMapCoords)
{
var mapCoords = _transform.GetMapCoordinates(target);
if (predicted)
EntityManager.PredictedSpawn(proto, mapCoords);
else if (_net.IsServer)
Spawn(proto, mapCoords);
}
else
{
var coords = xform.Coordinates;
var coords = target.Comp.Coordinates;
if (!coords.IsValid(EntityManager))
return;
if (ent.Comp.Predicted)
PredictedSpawnAttachedTo(ent.Comp.Proto, coords);
if (predicted)
PredictedSpawnAttachedTo(proto, coords);
else if (_net.IsServer)
SpawnAttachedTo(ent.Comp.Proto, coords);
SpawnAttachedTo(proto, coords);
}
}

View File

@@ -1,6 +1,7 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.DeviceLinking;
using Content.Shared.EntityTable;
using Content.Shared.Item.ItemToggle;
using Content.Shared.Popups;
using Content.Shared.Timing;
@@ -38,6 +39,7 @@ public sealed partial class TriggerSystem : EntitySystem
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
[Dependency] private readonly ItemToggleSystem _itemToggle = default!;
[Dependency] private readonly SharedDeviceLinkSystem _deviceLink = default!;
[Dependency] private readonly EntityTableSystem _entityTable = default!;
public const string DefaultTriggerKey = "trigger";