--- /dev/null
+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;
+}
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);
}
return;
var xform = Transform(target.Value);
+ SpawnTriggerHelper((target.Value, xform), ent.Comp.Proto, ent.Comp.UseMapCoords, ent.Comp.Predicted);
+ }
+
+ 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)
+ {
+ SpawnTriggerHelper((target.Value, xform), proto, ent.Comp.UseMapCoords, ent.Comp.Predicted);
+ }
+ }
- if (ent.Comp.UseMapCoords)
+ /// <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.Value, xform);
- if (ent.Comp.Predicted)
- EntityManager.PredictedSpawn(ent.Comp.Proto, mapCoords);
+ var mapCoords = _transform.GetMapCoordinates(target);
+ if (predicted)
+ EntityManager.PredictedSpawn(proto, mapCoords);
else if (_net.IsServer)
- Spawn(ent.Comp.Proto, mapCoords);
-
+ 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);
}
}
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;
[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";