mucho hacks

This commit is contained in:
JohnCorby 2023-08-10 14:32:24 -07:00
parent d735e65032
commit c8eb55f506

View File

@ -1,7 +1,6 @@
using NewHorizons.Utility.OuterWilds; using NewHorizons.Utility.OuterWilds;
using System.Collections; using System.Collections;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem;
namespace NewHorizons.Components; namespace NewHorizons.Components;
@ -20,16 +19,18 @@ public class AddPhysics : MonoBehaviour
"If there's already good colliders on the detail, you can make this 0.")] "If there's already good colliders on the detail, you can make this 0.")]
public float Radius = 1f; public float Radius = 1f;
private OWRigidbody parentBody; private OWRigidbody _parentBody;
private OWRigidbody owRigidbody; private OWRigidbody _body;
private ImpactSensor _impactSensor;
private bool _maintainVelocity;
private Vector3 _centerOfMass;
private IEnumerator Start() private IEnumerator Start()
{ {
yield return new WaitForSeconds(.1f); // detectors dont detect unless we wait for some reason
yield return new WaitForSeconds(10f);
parentBody = GetComponentInParent<OWRigidbody>(); _parentBody = GetComponentInParent<OWRigidbody>();
// yield return new WaitUntil(() => Keyboard.current.lKey.isPressed);
// hack: make all mesh colliders convex // hack: make all mesh colliders convex
// triggers are already convex // triggers are already convex
@ -43,10 +44,10 @@ public class AddPhysics : MonoBehaviour
bodyGo.transform.position = transform.position; bodyGo.transform.position = transform.position;
bodyGo.transform.rotation = transform.rotation; bodyGo.transform.rotation = transform.rotation;
owRigidbody = bodyGo.AddComponent<OWRigidbody>(); _body = bodyGo.AddComponent<OWRigidbody>();
owRigidbody._simulateInSector = Sector; _body._simulateInSector = Sector;
// owRigidbody._autoGenerateCenterOfMass = false; _body._autoGenerateCenterOfMass = false;
// owRigidbody._centerOfMass = owRigidbody.transform.InverseTransformPoint(parentBody.GetPosition()); _body._centerOfMass = _body.transform.InverseTransformPoint(_parentBody.GetWorldCenterOfMass());
bodyGo.layer = Layer.PhysicalDetector; bodyGo.layer = Layer.PhysicalDetector;
bodyGo.tag = "DynamicPropDetector"; bodyGo.tag = "DynamicPropDetector";
@ -61,7 +62,7 @@ public class AddPhysics : MonoBehaviour
fluidDetector._buoyancy = Locator.GetProbe().GetOWRigidbody()._attachedFluidDetector._buoyancy; fluidDetector._buoyancy = Locator.GetProbe().GetOWRigidbody()._attachedFluidDetector._buoyancy;
fluidDetector._splashEffects = Locator.GetProbe().GetOWRigidbody()._attachedFluidDetector._splashEffects; fluidDetector._splashEffects = Locator.GetProbe().GetOWRigidbody()._attachedFluidDetector._splashEffects;
var impactSensor = bodyGo.AddComponent<ImpactSensor>(); _impactSensor = bodyGo.AddComponent<ImpactSensor>();
var audioSource = bodyGo.AddComponent<AudioSource>(); var audioSource = bodyGo.AddComponent<AudioSource>();
audioSource.maxDistance = 30; audioSource.maxDistance = 30;
audioSource.dopplerLevel = 0; audioSource.dopplerLevel = 0;
@ -74,23 +75,46 @@ public class AddPhysics : MonoBehaviour
var objectImpactAudio = bodyGo.AddComponent<ObjectImpactAudio>(); var objectImpactAudio = bodyGo.AddComponent<ObjectImpactAudio>();
objectImpactAudio._minPitch = 0.4f; objectImpactAudio._minPitch = 0.4f;
objectImpactAudio._maxPitch = 0.6f; objectImpactAudio._maxPitch = 0.6f;
objectImpactAudio._impactSensor = impactSensor; objectImpactAudio._impactSensor = _impactSensor;
bodyGo.SetActive(true); bodyGo.SetActive(true);
transform.parent = bodyGo.transform; transform.parent = bodyGo.transform;
owRigidbody.SetMass(Mass); _body.SetMass(Mass);
owRigidbody.SetVelocity(parentBody.GetPointVelocity(owRigidbody.GetWorldCenterOfMass())); _body.SetVelocity(_parentBody.GetPointVelocity(_body.GetWorldCenterOfMass()));
_body.SetAngularVelocity(_parentBody.GetAngularVelocity());
// hack: make center of mass at the parent body so it doesn't drift on rotating body
// _centerOfMass = _body.GetCenterOfMass();
// _body.SetCenterOfMass(_body.transform.InverseTransformPoint(_parentBody.GetWorldCenterOfMass()));
// _impactSensor.OnImpact += OnImpact;
// #536 - Physics objects in bramble dimensions not disabled on load // #536 - Physics objects in bramble dimensions not disabled on load
// sectors wait 3 frames and then call OnSectorOccupantsUpdated // sectors wait 3 frames and then call OnSectorOccupantsUpdated
// however we wait .1 real seconds which is longer // however we wait .1 real seconds which is longer
// so we have to manually call this // so we have to manually call this
if (owRigidbody._simulateInSector != null) if (_body._simulateInSector != null)
owRigidbody.OnSectorOccupantsUpdated(); _body.OnSectorOccupantsUpdated();
yield return new WaitUntil(() => Keyboard.current.kKey.isPressed); // it drifts otherwise for some reason
_maintainVelocity = true;
yield return new WaitForSeconds(10f);
_maintainVelocity = false;
}
private void FixedUpdate()
{
if (_maintainVelocity)
{
_body.SetVelocity(_parentBody.GetPointVelocity(_body.GetWorldCenterOfMass()));
_body.SetAngularVelocity(_parentBody.GetAngularVelocity());
}
}
private void OnImpact(ImpactData impact)
{
// revert it back to normal
_body.SetCenterOfMass(_centerOfMass);
_impactSensor.OnImpact -= OnImpact;
Destroy(this); Destroy(this);
} }
@ -102,12 +126,7 @@ public class AddPhysics : MonoBehaviour
private void OnRenderObject() private void OnRenderObject()
{ {
Popcron.Gizmos.Sphere(transform.position, Radius); Popcron.Gizmos.Sphere(transform.position, Radius);
Popcron.Gizmos.Line(transform.position, parentBody.GetPosition()); if (_body) Popcron.Gizmos.Line(transform.position, _body.GetWorldCenterOfMass(), Color.red);
Popcron.Gizmos.Line(transform.position, owRigidbody.GetWorldCenterOfMass()); if (_parentBody) Popcron.Gizmos.Line(transform.position, _parentBody.GetWorldCenterOfMass(), Color.green);
}
private void FixedUpdate()
{
// owRigidbody.SetVelocity(parentBody.GetPointVelocity(owRigidbody.GetWorldCenterOfMass()));
} }
} }