131 lines
3.6 KiB
C#
131 lines
3.6 KiB
C#
|
using Sandbox.Utils;
|
||
|
public sealed class RagdollController : Component
|
||
|
{
|
||
|
[Group("Setup"), Order(-100), Property] public ModelPhysics bodyPhysics { get; set; }
|
||
|
[Group("Setup"), Order(-100), Property] public SkinnedModelRenderer bodyRenderer { get; set; }
|
||
|
|
||
|
[Group("Config"), Order(0), Property, Range(0, 15)] public int impaledPhysicsBodyIndex { get; set; }
|
||
|
[Group("Config"), Order(0), Property] public bool isLocked { get; set; }
|
||
|
[Group("Runtime"), Order(100), Property] public PhysicsBody impaledPhysicsBody { get; set; }
|
||
|
|
||
|
// Sensible Indexes
|
||
|
// 3 - HEad
|
||
|
|
||
|
public void Toggle()
|
||
|
{
|
||
|
bool enable = !bodyPhysics.Enabled;
|
||
|
bodyPhysics.Enabled = enable;
|
||
|
bodyPhysics.MotionEnabled = enable;
|
||
|
bodyRenderer.UseAnimGraph = !enable;
|
||
|
}
|
||
|
|
||
|
public void EnableRagdoll()
|
||
|
{
|
||
|
bool enable = true;
|
||
|
bodyPhysics.Enabled = enable;
|
||
|
bodyPhysics.MotionEnabled = enable;
|
||
|
bodyRenderer.UseAnimGraph = !enable;
|
||
|
}
|
||
|
|
||
|
public void DisableRagdoll()
|
||
|
{
|
||
|
bool enable = false;
|
||
|
}
|
||
|
|
||
|
void UpdateBody()
|
||
|
{
|
||
|
//impaledPhysicsBodyIndexRemapped = GetClosestSafeIndex(impaledPhysicsBodyIndex);
|
||
|
|
||
|
var bodyLock = new PhysicsLock();
|
||
|
bodyLock.Pitch = isLocked;
|
||
|
bodyLock.Yaw = isLocked;
|
||
|
bodyLock.Roll = isLocked;
|
||
|
bodyLock.X = isLocked;
|
||
|
bodyLock.Y = isLocked;
|
||
|
bodyLock.Z = isLocked;
|
||
|
|
||
|
bodyPhysics.Locking = bodyLock;
|
||
|
bodyPhysics.MotionEnabled = !isLocked;
|
||
|
|
||
|
if (isLocked)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!bodyPhysics.Enabled)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
impaledPhysicsBody = bodyPhysics.PhysicsGroup.Bodies.ElementAt(impaledPhysicsBodyIndex);
|
||
|
|
||
|
var followToPos = GameObject.Transform.World.Position;// - (GameObject.Transform.World.Forward * 30.0f);
|
||
|
|
||
|
// If this was
|
||
|
if (impaledPhysicsBodyIndex == Bones.Terry.spine_0)
|
||
|
{
|
||
|
followToPos += Vector3.Up * 10.0f;
|
||
|
}
|
||
|
|
||
|
//hitPhysicsBody.Position = followToPos;
|
||
|
var delta = Vector3.Direction(impaledPhysicsBody.Position, followToPos);
|
||
|
|
||
|
impaledPhysicsBody.Velocity = delta * 500.0f;
|
||
|
//impaledPhysicsBody.Velocity = Vector3.Zero;
|
||
|
//impaledPhysicsBody.Position = MathY.MoveTowards(impaledPhysicsBody.Position, followToPos, Time.Delta * 100.0f);
|
||
|
//Log.Info($"delta.Length: {delta.Length}, impaledByHarpoonSpear.isInFlight: {impaledByHarpoonSpear.isInFlight}");
|
||
|
|
||
|
bool useOriginalMethod = true;
|
||
|
if (useOriginalMethod) //delta.Length < 15.0f)// && !impaledByHarpoonSpear.isInFlight)
|
||
|
{
|
||
|
impaledPhysicsBody.Velocity = delta * 1000.0f;
|
||
|
impaledPhysicsBody.Velocity = Vector3.Zero;
|
||
|
impaledPhysicsBody.AngularVelocity = MathY.MoveTowards(impaledPhysicsBody.AngularVelocity, Vector3.Zero, Time.Delta * 15.0f);
|
||
|
impaledPhysicsBody.AngularVelocity = Vector3.Zero;
|
||
|
impaledPhysicsBody.Position = followToPos;
|
||
|
|
||
|
|
||
|
|
||
|
//impaledPhysicsBody.Velocity = delta * 100.0f;
|
||
|
//impaledPhysicsBody.Position = MathY.MoveTowards(impaledPhysicsBody.Position, followToPos, Time.Delta * 1000.0f);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
float smoothRate = 0.075f;
|
||
|
smoothRate = 0.0001f;
|
||
|
|
||
|
float smoothRateRot = 0.075f;
|
||
|
//smoothRateRot = 0.0001f;
|
||
|
|
||
|
|
||
|
var velocity = impaledPhysicsBody.Velocity;
|
||
|
Vector3.SmoothDamp(impaledPhysicsBody.Position, WorldPosition, ref velocity, smoothRate, Time.Delta);
|
||
|
impaledPhysicsBody.Velocity = velocity;
|
||
|
|
||
|
var angularVelocity = impaledPhysicsBody.AngularVelocity;
|
||
|
Rotation.SmoothDamp(impaledPhysicsBody.Rotation, WorldRotation, ref angularVelocity, smoothRateRot, Time.Delta);
|
||
|
impaledPhysicsBody.AngularVelocity = angularVelocity;
|
||
|
|
||
|
//impaledPhysicsBody.AngularVelocity = Vector3.Zero;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnUpdate()
|
||
|
{
|
||
|
if ( Network.IsOwner )
|
||
|
{
|
||
|
if(Input.Pressed( "Ragdoll" ))
|
||
|
{
|
||
|
Toggle();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
UpdateBody();
|
||
|
}
|
||
|
|
||
|
protected override void OnFixedUpdate()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|