You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
6.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SiegeSong;
namespace SiegeSong
{
public class ActorPhysicsManager : MonoBehaviour
{
public Motor Motor;
public Rigidbody RigidBody;
public CapsuleCollider Collider;
public Rigidbody[] ragdollBodies = new Rigidbody[13];
public Collider[] ragdollColliders = new Collider[13];
public CharacterJoint[] ragdollJoints = new CharacterJoint[13];
public Hand[] Hands;
public Foot[] Feet;
public bool PhysicsEnabled;
public bool Ragdolling;
public bool Falling;
public float YOffset;
float staggerForceMagnitudeThreshold = 10.0f;
float ragdollForceMagnitudeThreshold = 30.0f;
float coyoteTime = 0.3f;
float coyoteTimer;
float gravityStrength = 0.40f;
string leftFootPath = "BoneRoot/Base HumanPelvis/Base HumanLThigh1/Base Human LThigh2/Base HumanLCalf/Base HumanLFoot/LeftFoot";
string rightFootPath = "BoneRoot/Base HumanPelvis/Base HumanRThigh1/Base Human RThigh2/Base HumanRCalf/Base HumanRFoot/RightFoot";
string leftFootName = "LeftFoot";
string rightFootName = "RightFoot";
public void GetPulled(Vector3 sublocation, Vector3 direction, float magnitude)
{
GetPushed(sublocation, -direction, magnitude);
}
public void GetPushed(Vector3 sublocation, Vector3 direction, float magnitude)
{
if ( magnitude > staggerForceMagnitudeThreshold)
{
Motor.Actor.Stats.Staggered = true;
}
if (magnitude > ragdollForceMagnitudeThreshold)
{
PhysicsEnabled = true;
}
if (PhysicsEnabled)
{
RigidBody.AddForceAtPosition(direction * magnitude * Time.deltaTime, sublocation);
}
}
public void StartFalling(bool autoOrient = false)
{
Falling = true;
PhysicsEnabled = true;
Ragdolling = autoOrient;
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<Ramp>() != null)
{
//ActorMotor.BodyCollidingWithRamp = true;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.GetComponent<Ramp>() != null)
{
//ActorMotor.BodyCollidingWithRamp = false;
}
}
void Start()
{
}
void Update()
{
//if (Feet[0] == null)
//{
// var feet = gameObject.GetComponentsInChildren<Foot>();
// foreach (var foot in feet)
// {
// if (foot.gameObject.name == leftFootName)
// {
// Feet[0] = foot;
// }
// }
//}
//if (Feet[0].PhysicsManager == null)
//{
// Feet[0].PhysicsManager = this;
//}
//if (Feet[1] == null)
//{
// var feet = gameObject.GetComponentsInChildren<Foot>();
// foreach (var foot in feet)
// {
// if (foot.gameObject.name == rightFootName)
// {
// Feet[1] = foot;
// }
// }
//}
//if (Feet[1].PhysicsManager == null)
//{
// Feet[1].PhysicsManager = this;
//}
//if (ActorMotor.UsingStation)
//{
// PhysicsEnabled = false;
//}
//var feetGrounded = 0;
//var feetUnstable = 0;
//for (var i = 0; i < Feet.Length; i++)
//{
// var foot = Feet[i];
// feetGrounded += foot.Grounded ? 1 : 0;
// feetUnstable += foot.Unstable ? 1 : 0;
//}
//var grounded = feetGrounded == 0;
////var grounded = feetGrounded >= (Feet.Length / 2);
//var unstable = feetUnstable > (Feet.Length / 2);
//if (!grounded)
//{
// PhysicsEnabled = true;
// if (!grounded && !Falling)
// {
// coyoteTimer += Time.deltaTime;
// if (coyoteTimer > coyoteTime)
// {
// StartFalling(!ActorMotor.Stats.OffBalance);
// Debug.Log("StartFalling");
// }
// }
// if (Falling)
// {
// Debug.Log("Falling");
// //Debug.Log($"Gravity - {-Vector3.up * gravityStrength * Time.deltaTime}");
// RigidBody.AddForce(-Vector3.up * gravityStrength * Time.deltaTime);
// transform.Translate(-Vector3.up * gravityStrength * Time.deltaTime);
// }
//}
//if (grounded)
//{
// Debug.Log("Grounded");
// PhysicsEnabled = false;
// Falling = false;
// transform.position = new Vector3(transform.position.x, Terrain.activeTerrain.SampleHeight(transform.position) + YOffset, transform.position.z);
//}
foreach (var rigidbody in ragdollBodies)
{
rigidbody.isKinematic = !Ragdolling;
rigidbody.useGravity = Ragdolling;
}
foreach (var collider in ragdollColliders)
collider.enabled = Ragdolling;
foreach (var joint in ragdollJoints)
joint.massScale = Ragdolling ? 1.0f : 0.00001f;
RigidBody.isKinematic = Ragdolling;
//if (PhysicsEnabled)
//{
RigidBody.constraints = !Ragdolling ? RigidbodyConstraints.FreezeRotation : RigidbodyConstraints.None;
RigidBody.useGravity = !Ragdolling;
//}
}
}
}