Add krakensbane
This commit is contained in:
parent
dfb439bac7
commit
a8ba625bfa
51
src/Craft/Krakensbane.cs
Normal file
51
src/Craft/Krakensbane.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace Quadratic.Carto.Craft;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Krakensbane manages the position of its direct descendants, to ensure the
|
||||||
|
/// focused vessel is not too far from the origin.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// The name Krakensbane is a legacy of the original KSP module which does the
|
||||||
|
/// same thing, i.e. preventing floating point errors from causing the vessel
|
||||||
|
/// to be thrown into deep space.
|
||||||
|
/// </remarks>
|
||||||
|
public sealed partial class Krakensbane : Node3D
|
||||||
|
{
|
||||||
|
Node3D? focusedVessel = null;
|
||||||
|
|
||||||
|
const float maxDistance = 500.0f;
|
||||||
|
|
||||||
|
public override void _PhysicsProcess(double delta)
|
||||||
|
{
|
||||||
|
Debug.Assert(
|
||||||
|
focusedVessel == null || focusedVessel.GetParent() == this,
|
||||||
|
"Focused vessel must be a direct descendant of Krakensbane"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (focusedVessel == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var vesselPosition = focusedVessel.Transform.Origin;
|
||||||
|
var vesselDistance = vesselPosition.Length();
|
||||||
|
if (vesselDistance > maxDistance)
|
||||||
|
{
|
||||||
|
// Reset the vessel's position to the origin
|
||||||
|
// TODO: save the total offset; we don't yet have a double-precision vector3
|
||||||
|
var applyTransform = Transform3D.Identity.Translated(-vesselPosition);
|
||||||
|
|
||||||
|
int n_children = GetChildCount();
|
||||||
|
for (int i = 0; i < n_children; i++)
|
||||||
|
{
|
||||||
|
var child = GetChild<Node3D>(i);
|
||||||
|
child.Transform = applyTransform * child.Transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
GD.Print($"Resetting position; delta = {-vesselPosition}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user