Reapply "OBT1.1.0 Merge branch 'dev_pte' into dev"
This reverts commit 046483b9da.
This commit is contained in:
+30
-5
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user