Reapply "OBT1.1.0 Merge branch 'dev_pte' into dev"

This reverts commit 046483b9da.
This commit is contained in:
NotAlwaysTrue
2026-04-30 21:59:54 +08:00
parent 02689d0d86
commit 25683dcf39
85 changed files with 2413 additions and 779 deletions
@@ -13,6 +13,12 @@ namespace Barotrauma.Items.Components
private Holdable holdable;
private float deattachTimer;
/// <summary>
/// Flag to prevent multiple queued creation requests.
/// Uses volatile to ensure visibility across threads.
/// </summary>
private volatile bool triggerBodyCreationQueued;
[Serialize(1.0f, IsPropertySaveable.No, description: "How long it takes to deattach the item from the level walls (in seconds).")]
public float DeattachDuration
@@ -86,13 +92,16 @@ namespace Barotrauma.Items.Components
{
if (trigger != null && amount.LengthSquared() > 0.00001f)
{
// Defer physics operation if in parallel context (Farseer is not thread-safe)
var capturedTrigger = trigger;
var capturedPos = item.SimPosition;
if (ignoreContacts)
{
trigger.SetTransformIgnoreContacts(item.SimPosition, 0.0f);
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransformIgnoreContacts(capturedPos, 0.0f));
}
else
{
trigger.SetTransform(item.SimPosition, 0.0f);
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransform(capturedPos, 0.0f));
}
}
}
@@ -109,13 +118,29 @@ namespace Barotrauma.Items.Components
}
else
{
if (trigger == null)
if (trigger == null && !triggerBodyCreationQueued)
{
CreateTriggerBody();
// Queue the physics body creation to be processed on the main thread.
// This is necessary because physics body creation is not thread-safe
// and Update() may be called from a parallel loop.
triggerBodyCreationQueued = true;
PhysicsBodyQueue.EnqueueCreation(() =>
{
// Double-check that trigger hasn't been created yet
// (in case this was called multiple times before queue processing)
if (trigger == null && !item.Removed)
{
CreateTriggerBody();
}
triggerBodyCreationQueued = false;
});
}
if (trigger != null && Vector2.DistanceSquared(item.SimPosition, trigger.SimPosition) > 0.01f)
{
trigger.SetTransform(item.SimPosition, 0.0f);
// Defer physics operation if in parallel context (Farseer is not thread-safe)
var capturedTrigger = trigger;
var capturedPos = item.SimPosition;
PhysicsBodyQueue.ExecuteOrDefer(() => capturedTrigger.SetTransform(capturedPos, 0.0f));
}
IsActive = false;
}