more tweaking

This commit is contained in:
JohnCorby 2023-02-03 13:27:17 -08:00
parent 7ca84069df
commit d11c998dd6
2 changed files with 28 additions and 61 deletions

View File

@ -236,6 +236,7 @@ namespace NewHorizons.Builder.Props
{ {
var addPhysics = prop.AddComponent<AddPhysics>(); var addPhysics = prop.AddComponent<AddPhysics>();
addPhysics.Sector = sector; addPhysics.Sector = sector;
addPhysics.Mass = detail.physicsMass;
addPhysics.Radius = detail.physicsRadius; addPhysics.Radius = detail.physicsRadius;
} }
@ -415,103 +416,64 @@ namespace NewHorizons.Builder.Props
} }
} }
// TODO: simulate in sector
// BUG: detector collider is not included in groups // BUG: detector collider is not included in groups
// TODO: mass
private class AddPhysics : MonoBehaviour private class AddPhysics : MonoBehaviour
{ {
public Sector Sector; public Sector Sector;
public float? Radius; public float Mass;
public float Radius;
private IEnumerator Start() private IEnumerator Start()
{ {
if (!Sector) if (!Sector)
Logger.LogError($"Prop {name} has physics but no sector! This will fall through things when surrounding area is unloaded"); Logger.LogError($"Prop {name} has physics but no sector! Will fall through things when surrounding area is unloaded");
yield return new WaitForSeconds(.1f); yield return new WaitForSeconds(3f);
var parentBody = GetComponentInParent<OWRigidbody>(); var parentBody = GetComponentInParent<OWRigidbody>();
// just disable all non triggers // just disable all non triggers
foreach (var meshCollider in GetComponentsInChildren<MeshCollider>(true)) foreach (var collider in GetComponentsInChildren<Collider>(true))
if (!meshCollider.isTrigger) if (!collider.isTrigger)
meshCollider.enabled = false; collider.enabled = false;
var bodyGo = new GameObject($"{name}_Body"); var bodyGo = new GameObject($"{name}_Body");
bodyGo.SetActive(false); bodyGo.SetActive(false);
bodyGo.transform.position = gameObject.transform.position; bodyGo.transform.position = transform.position;
bodyGo.transform.rotation = gameObject.transform.rotation; bodyGo.transform.rotation = transform.rotation;
var owRigidbody = bodyGo.AddComponent<OWRigidbody>(); var owRigidbody = bodyGo.AddComponent<OWRigidbody>();
owRigidbody._simulateInSector = Sector; owRigidbody._simulateInSector = Sector;
var detector = new GameObject("Detector"); bodyGo.layer = LayerMask.NameToLayer("PhysicalDetector");
detector.transform.SetParent(bodyGo.transform, false); bodyGo.tag = "DynamicPropDetector";
detector.layer = LayerMask.NameToLayer("AdvancedDetector"); bodyGo.AddComponent<SphereCollider>().radius = Radius;
detector.tag = "DynamicPropDetector"; bodyGo.AddComponent<DynamicForceDetector>();
var sphereCollider = detector.AddComponent<SphereCollider>(); bodyGo.AddComponent<DynamicFluidDetector>();
if (Radius.HasValue)
sphereCollider.radius = Radius.Value;
detector.AddComponent<DynamicForceDetector>();
detector.AddComponent<DynamicFluidDetector>();
var impactSensor = bodyGo.AddComponent<ImpactSensor>(); var impactSensor = bodyGo.AddComponent<ImpactSensor>();
var impactAudio = new GameObject("ImpactAudio"); var audioSource = bodyGo.AddComponent<AudioSource>();
impactAudio.transform.SetParent(bodyGo.transform, false);
var audioSource = impactAudio.AddComponent<AudioSource>();
audioSource.maxDistance = 30; audioSource.maxDistance = 30;
audioSource.dopplerLevel = 0; audioSource.dopplerLevel = 0;
audioSource.rolloffMode = AudioRolloffMode.Custom; audioSource.rolloffMode = AudioRolloffMode.Custom;
audioSource.playOnAwake = false; audioSource.playOnAwake = false;
audioSource.spatialBlend = 1; audioSource.spatialBlend = 1;
var owAudioSource = impactAudio.AddComponent<OWAudioSource>(); var owAudioSource = bodyGo.AddComponent<OWAudioSource>();
owAudioSource._audioSource = audioSource; owAudioSource._audioSource = audioSource;
owAudioSource._track = OWAudioMixer.TrackName.Environment; owAudioSource._track = OWAudioMixer.TrackName.Environment;
var objectImpactAudio = impactAudio.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;
owRigidbody.SetMass(Mass);
owRigidbody.SetVelocity(parentBody.GetPointVelocity(transform.position)); owRigidbody.SetVelocity(parentBody.GetPointVelocity(transform.position));
transform.SetParent(bodyGo.transform, false);
Destroy(this); Destroy(this);
} }
} }
/*
private static void AddPhysics(GameObject prop, Sector sector)
{
var primaryBody = prop.GetComponentInParent<OWRigidbody>();
var rb = prop.AddComponent<Rigidbody>();
var owrb = prop.AddComponent<OWRigidbody>();
var kine = prop.AddComponent<KinematicRigidbody>();
rb.isKinematic = true;
var offsetApplier = prop.AddComponent<CenterOfTheUniverseOffsetApplier>();
offsetApplier.Init(owrb);
owrb._simulateInSector = sector;
owrb._kinematicSimulation = true;
owrb._kinematicRigidbody = kine;
owrb._offsetApplier = offsetApplier;
prop.AddComponent<InitialMotion>().SetPrimaryBody(primaryBody);
prop.AddComponent<MatchInitialMotion>().SetBodyToMatch(primaryBody);
var detector = new GameObject("Detector");
detector.transform.parent = prop.transform;
detector.transform.localPosition = Vector3.zero;
detector.tag = "DynamicPropDetector";
var shape = detector.AddComponent<SphereShape>();
shape.SetCollisionMode(Shape.CollisionMode.Detector);
shape.SetLayer(Shape.Layer.Default);
shape.layerMask = 5;
detector.AddComponent<DynamicForceDetector>();
detector.AddComponent<ForceApplier>();
}
*/
} }
} }

View File

@ -231,14 +231,19 @@ namespace NewHorizons.External.Modules
public bool keepLoaded; public bool keepLoaded;
/// <summary> /// <summary>
/// Should this object dynamically move around /// Should this object dynamically move around?
/// </summary> /// </summary>
public bool hasPhysics; public bool hasPhysics;
/// <summary> /// <summary>
/// Optionally create a SphereCollider of the given radius that physics will use for collision /// The mass of the physics object.
/// </summary> /// </summary>
public float? physicsRadius; [DefaultValue(1f)] public float physicsMass = 1f;
/// <summary>
/// The radius that the added sphere collider will use for physics collision.
/// </summary>
[DefaultValue(0.5f)] public float physicsRadius = 0.5f;
} }
[JsonObject] [JsonObject]