Entity Tables (EntitySpawnEntry replacement) (#30579)

* Entity table code

* entity table examples

* fix dat shit

* access

* tests tests tests

* sloth review
This commit is contained in:
Nemanja
2024-08-09 22:12:40 -04:00
committed by GitHub
parent b692ab1850
commit 437c861622
19 changed files with 1198 additions and 566 deletions

View File

@@ -1,9 +1,10 @@
using System.Numerics;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.Spawners.Components;
using Content.Shared.EntityTable;
using Content.Shared.GameTicking.Components;
using JetBrains.Annotations;
using Robust.Shared.Map;
using Robust.Shared.Random;
namespace Content.Server.Spawners.EntitySystems
@@ -13,6 +14,7 @@ namespace Content.Server.Spawners.EntitySystems
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly GameTicker _ticker = default!;
[Dependency] private readonly EntityTableSystem _entityTable = default!;
public override void Initialize()
{
@@ -21,6 +23,7 @@ namespace Content.Server.Spawners.EntitySystems
SubscribeLocalEvent<GameRuleStartedEvent>(OnRuleStarted);
SubscribeLocalEvent<ConditionalSpawnerComponent, MapInitEvent>(OnCondSpawnMapInit);
SubscribeLocalEvent<RandomSpawnerComponent, MapInitEvent>(OnRandSpawnMapInit);
SubscribeLocalEvent<EntityTableSpawnerComponent, MapInitEvent>(OnEntityTableSpawnMapInit);
}
private void OnCondSpawnMapInit(EntityUid uid, ConditionalSpawnerComponent component, MapInitEvent args)
@@ -35,6 +38,13 @@ namespace Content.Server.Spawners.EntitySystems
QueueDel(uid);
}
private void OnEntityTableSpawnMapInit(Entity<EntityTableSpawnerComponent> ent, ref MapInitEvent args)
{
Spawn(ent);
if (ent.Comp.DeleteSpawnerAfterSpawn && !TerminatingOrDeleted(ent) && Exists(ent))
QueueDel(ent);
}
private void OnRuleStarted(ref GameRuleStartedEvent args)
{
var query = EntityQueryEnumerator<ConditionalSpawnerComponent>();
@@ -110,5 +120,23 @@ namespace Content.Server.Spawners.EntitySystems
EntityManager.SpawnEntity(_robustRandom.Pick(component.Prototypes), coordinates);
}
private void Spawn(Entity<EntityTableSpawnerComponent> ent)
{
if (TerminatingOrDeleted(ent) || !Exists(ent))
return;
var coords = Transform(ent).Coordinates;
var spawns = _entityTable.GetSpawns(ent.Comp.Table);
foreach (var proto in spawns)
{
var xOffset = _robustRandom.NextFloat(-ent.Comp.Offset, ent.Comp.Offset);
var yOffset = _robustRandom.NextFloat(-ent.Comp.Offset, ent.Comp.Offset);
var trueCoords = coords.Offset(new Vector2(xOffset, yOffset));
Spawn(proto, trueCoords);
}
}
}
}