namespace Brutal.JoltApi;
///
/// A batch of created bodies being added to the physics system in one go, from
/// . adds them; frees the
/// batch, and aborts the add first if it was never committed. Dispose it either way.
///
///
/// Adding bodies in a batch builds their broad-phase nodes once for the whole set, which is much cheaper than one
/// per body, and the prepare step can run on a background thread. The batch
/// keeps its own copy of the IDs, in the order Jolt prepared them. It holds on to the body interface it came from,
/// so dispose it before the physics system goes; it has no finalizer for that reason. and
/// are safe to call from different threads: whichever runs second sees the first's result.
///
public sealed class BodyAddBatch : IDisposable, INullable
{
// Commit and Dispose both reach the native batch, so they take turns: Dispose can't free it mid-commit
private readonly Lock _gate = new();
private nint _batch;
private bool _committed;
internal BodyAddBatch(nint batch) => _batch = batch;
/// Whether this is the empty (already disposed) batch.
public bool IsNull() => _batch == 0;
/// Whether has run; until then, aborts the add.
public bool IsCommitted => _committed;
///
/// Adds every body in the batch to the physics system, under one lock. Committing again does nothing.
///
/// Whether the bodies start active.
/// The batch has been disposed.
public void Commit(Activation activation = Activation.Activate)
{
lock (_gate)
{
ObjectDisposedException.ThrowIf(_batch == 0, this);
if (_committed)
return;
Jolt.PInvoke.BodyAddBatchCommit(_batch, activation);
_committed = true;
}
}
/// Frees the batch, aborting the add if it was never committed. Idempotent.
public void Dispose()
{
lock (_gate)
{
if (_batch == 0)
return;
Jolt.PInvoke.BodyAddBatchDestroy(_batch);
_batch = 0;
}
}
}