using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Terraria.ModLoader;
namespace CalamityMod.Physics;
// Yes, I was silently reading over the Nightshade dev channels when learning about this.
// So, thanks Tomat and 1-3 for the idea! -Lucille
///
/// A handle that contains a reference to a given globally managed rope instance.
///
public readonly struct RopeHandle
{
///
/// The value used to identify this handle's associated rope managed by the central system.
///
private readonly int Identifier;
///
/// The rope associated with this handle.
///
private readonly Rope Rope => ModContent.GetInstance().Ropes[Identifier]!;
///
/// The set of all positions maintained by the underlying rope.
///
public readonly IEnumerable Positions => Rope.SegmentPositions;
///
/// The amount of segments this rope has.
///
public int SegmentCount => Rope.Segments.Length;
///
/// The starting position of the underlying rope.
///
public ref Vector2 Start => ref Rope.Segments[0].Position;
///
/// The ending position of the underlying rope.
///
public ref Vector2 End => ref Rope.Segments[^1].Position;
///
/// The gravity associated with the rope.
///
public Vector2 Gravity
{
get => Rope.Gravity;
set => Rope.Gravity = value;
}
internal RopeHandle(int identifier) => Identifier = identifier;
///
/// Forces this rope to settle by performing a series of constrained updates.
///
public void Settle()
{
for (var i = 0; i < 20; i++)
Rope.Update();
}
///
/// Indicates that the rope associated with this handle should be returned back to the pool.
///
public void Dispose()
{
var chunkIndex = Identifier / RopeManagerSystem.BitsPerChunk;
var bitIndex = Identifier % RopeManagerSystem.BitsPerChunk;
ModContent.GetInstance().ToggleActivityIndex(chunkIndex, bitIndex);
}
}