using System;
using Microsoft.Xna.Framework;
using Terraria;
namespace CalamityMod.Physics;
internal class Rope
{
///
/// A 0-1 interpolant which dictates a dampning factor for velocity integration increments.
///
private float MovementSpeedDampingCoefficient
{
get;
set;
}
///
/// A timer that increments in respond to wind, assuming wind response is enabled in the .
///
private float WindTime
{
get;
set;
}
///
/// The set of segments that compose this rope.
///
public RopeSegment[] Segments
{
get;
set;
}
///
/// The set of positions that compose this rope.
///
public Vector2[] SegmentPositions
{
get;
private set;
}
///
/// The desired distance between each segment on the rope.
///
public float DistancePerSegment
{
get;
set;
}
///
/// The gravity force to exert on the rope when updating.
///
public Vector2 Gravity
{
get;
set;
}
///
/// The amount of steps to perform when constraining tiles to their desired lengths. Higher values equate to greater accuracy, but reduced performance.
///
public int ConstraintSteps
{
get;
private set;
}
///
/// Sets of settings that dictate how this rope should behave.
///
public RopeSettings Settings
{
get;
set;
}
///
/// Whether this rope should collide with tiles or not.
///
public bool InteractWithTiles => Settings.TileColliderArea is not null;
public Rope(Vector2 start, Vector2 end, int segmentCount, float distancePerSegment, Vector2 gravity, RopeSettings settings, int constraintSteps = 10)
{
Segments = new RopeSegment[segmentCount];
SegmentPositions = new Vector2[segmentCount];
for (var i = 0; i < segmentCount; i++)
{
var segmentPos = Vector2.Lerp(start, end, i / (segmentCount - 1f));
Segments[i] = new RopeSegment(segmentPos);
}
Segments[0].FixedInPlace = settings.StartIsFixed;
Segments[^1].FixedInPlace = settings.EndIsFixed;
RecalculateSegmentPositions();
DistancePerSegment = distancePerSegment;
Gravity = gravity;
ConstraintSteps = constraintSteps;
Settings = settings;
}
///
/// Recalculates the cache based on positions.
///
private void RecalculateSegmentPositions()
{
for (var i = 0; i < Segments.Length; i++)
SegmentPositions[i] = Segments[i].Position;
}
///
/// Moves a given position around, obeying tile interaction rules if this rope requires them.
///
/// The position vector to move.
/// The base velocity to consider.
private void Move(ref Vector2 position, Vector2 baseVelocity)
{
// Apply standard Eulerian integration if tile interactions are not required.
if (!InteractWithTiles || Settings.TileColliderArea is null)
{
position += baseVelocity;
return;
}
// If tile interactions *are* required, handle them before moving forward.
var width = (int)Settings.TileColliderArea.Value.X;
var height = (int)Settings.TileColliderArea.Value.Y;
var newVelocity = Collision.noSlopeCollision(position, baseVelocity, width, height + 2, true, true);
newVelocity = Collision.noSlopeCollision(position, newVelocity, width, height, true, true);
var finalVelocity = baseVelocity;
if (Math.Abs(baseVelocity.X) > Math.Abs(newVelocity.X))
finalVelocity.X = 0f;
if (Math.Abs(baseVelocity.Y) > Math.Abs(newVelocity.Y))
finalVelocity.Y = 0f;
position += finalVelocity;
}
///
/// Updates this rope, making it move around.
///
public void Update()
{
for (var i = 0; i < Segments.Length; i++)
{
var movementStep = (Segments[i].Position - Segments[i].OldPosition) * (1f - MovementSpeedDampingCoefficient);
if (movementStep.Length() < 0.02f)
movementStep = Vector2.Zero;
Segments[i].OldPosition = Segments[i].Position;
if (!Segments[i].FixedInPlace)
Move(ref Segments[i].Position, movementStep + Gravity);
}
for (var i = 0; i < ConstraintSteps; i++)
Constrain();
RecalculateSegmentPositions();
if (Settings.RespondToEntityMovement)
HandleEntityMovementResponse();
if (Settings.RespondToWind)
HandleWindResponse();
}
///
/// Makes this rope respond to the movement of entities.
///
private void HandleEntityMovementResponse()
{
for (var i = 0; i < Segments.Length; i++)
{
ref RopeSegment ropeSegment = ref Segments[i];
if (ropeSegment.FixedInPlace)
continue;
foreach (var player in Main.ActivePlayers)
{
float playerProximityInterpolant = Utils.GetLerpValue(37f, 10f, player.Distance(ropeSegment.Position), true);
ropeSegment.Position += player.velocity * playerProximityInterpolant / Settings.Mass * 0.08f;
}
}
}
///
/// Makes this entity respond to wind.
///
private void HandleWindResponse()
{
WindTime += Main.windSpeedCurrent / 60f;
if (MathF.Abs(WindTime) >= 4000f)
WindTime = 0f;
var windSpeed = Math.Clamp(Main.WindForVisuals * 2f, -1.3f, 1.3f);
var windWave = MathF.Cos(WindTime * 3.42f + Segments[0].Position.Length() * 0.06f);
var wind = Vector2.UnitX * (windWave + Main.windSpeedCurrent) * -0.2f;
Segments[^1].Position += wind * Utils.GetLerpValue(0.3f, 0.75f, windSpeed, true) / Settings.Mass;
}
///
/// Constrains segments on this rope, conserving their overall length.
///
public void Constrain()
{
for (var i = 0; i < Segments.Length - 1; i++)
{
// Determine how much each segment has to move in order to return to its desired resting distance.
var segmentLength = Segments[i].Position.Distance(Segments[i + 1].Position);
var distanceFromIdealLength = segmentLength - DistancePerSegment;
Vector2 correctiveForce = (Segments[i].Position - Segments[i + 1].Position).SafeNormalize(Vector2.Zero) * distanceFromIdealLength;
var pinned = Segments[i].FixedInPlace;
var nextPinned = Segments[i + 1].FixedInPlace;
correctiveForce *= pinned || nextPinned ? 1f : 0.5f;
if (!pinned)
Move(ref Segments[i].Position, -correctiveForce);
if (!nextPinned)
Move(ref Segments[i + 1].Position, correctiveForce);
}
}
}