Fix spawning glass shard for each glass sheet in stack (#25308)

* fix: SpawnEntitiesBehavior now works with stacks

Fixed the issue of SpawnEntitiesBehavior not executing multiple times on
entities with stack conponent.

Fixes #25287

* fix: reduced dictionary iterations
This commit is contained in:
Łukasz Mędrek
2024-02-16 18:42:43 +00:00
committed by GitHub
parent b5aa1e497f
commit 53270be66c

View File

@@ -31,29 +31,38 @@ namespace Content.Server.Destructible.Thresholds.Behaviors
var getRandomVector = () => new Vector2(system.Random.NextFloat(-Offset, Offset), system.Random.NextFloat(-Offset, Offset));
var executions = 1;
if (system.EntityManager.TryGetComponent<StackComponent>(owner, out var stack))
{
executions = stack.Count;
}
foreach (var (entityId, minMax) in Spawn)
{
var count = minMax.Min >= minMax.Max
? minMax.Min
: system.Random.Next(minMax.Min, minMax.Max + 1);
if (count == 0) continue;
if (EntityPrototypeHelpers.HasComponent<StackComponent>(entityId, system.PrototypeManager, system.ComponentFactory))
for (var execution = 0; execution < executions; execution++)
{
var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
system.StackSystem.SetCount(spawned, count);
var count = minMax.Min >= minMax.Max
? minMax.Min
: system.Random.Next(minMax.Min, minMax.Max + 1);
TransferForensics(spawned, system, owner);
}
else
{
for (var i = 0; i < count; i++)
if (count == 0) continue;
if (EntityPrototypeHelpers.HasComponent<StackComponent>(entityId, system.PrototypeManager, system.ComponentFactory))
{
var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
system.StackSystem.SetCount(spawned, count);
TransferForensics(spawned, system, owner);
}
else
{
for (var i = 0; i < count; i++)
{
var spawned = system.EntityManager.SpawnEntity(entityId, position.Offset(getRandomVector()));
TransferForensics(spawned, system, owner);
}
}
}
}
}