SpawnEntityTableOnTrigger (#39909)
* commit * comment * empty * better xform --------- Co-authored-by: iaada <iaada@users.noreply.github.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user