namespace Zinnia.Data.Operation.Mutation { using UnityEngine; using Zinnia.Extension; /// /// Mutates the properties of a with the benefit of being able to specify a containing as the target. /// public class RigidbodyPropertyMutator : MonoBehaviour { [Tooltip("The Rigidbody to mutate.")] [SerializeField] private Rigidbody target; /// /// The to mutate. /// public Rigidbody Target { get { return target; } set { target = value; } } /// /// The value. /// private float mass; public float Mass { get { return mass; } set { mass = value; if (this.IsMemberChangeAllowed()) { OnAfterMassChange(); } } } /// /// The value. /// private float drag; public float Drag { get { return drag; } set { drag = value; if (this.IsMemberChangeAllowed()) { OnAfterDragChange(); } } } /// /// The value. /// private float angularDrag; public float AngularDrag { get { return angularDrag; } set { angularDrag = value; if (this.IsMemberChangeAllowed()) { OnAfterAngularDragChange(); } } } /// /// The state. /// private bool useGravity; public bool UseGravity { get { return useGravity; } set { useGravity = value; if (this.IsMemberChangeAllowed()) { OnAfterUseGravityChange(); } } } /// /// The state. /// private bool isKinematic; public bool IsKinematic { get { return isKinematic; } set { isKinematic = value; if (this.IsMemberChangeAllowed()) { OnAfterIsKinematicChange(); } } } /// /// The value. /// private Vector3 velocity; public Vector3 Velocity { get { return velocity; } set { velocity = value; if (this.IsMemberChangeAllowed()) { OnAfterVelocityChange(); } } } /// /// The value. /// private Vector3 angularVelocity; public Vector3 AngularVelocity { get { return angularVelocity; } set { angularVelocity = value; if (this.IsMemberChangeAllowed()) { OnAfterAngularVelocityChange(); } } } /// /// The value. /// private float maxAngularVelocity; public float MaxAngularVelocity { get { return maxAngularVelocity; } set { maxAngularVelocity = value; if (this.IsMemberChangeAllowed()) { OnAfterMaxAngularVelocityChange(); } } } /// /// Clears . /// public virtual void ClearTarget() { if (!this.IsValidState()) { return; } Target = default; } /// /// Sets the based on the first found as either a direct, descendant or ancestor of the given . /// /// The to search for a on. public virtual void SetTarget(GameObject target) { if (!this.IsValidState() || target == null) { return; } Target = target.TryGetComponent(true, true); } /// /// Sets the x value. /// /// The value to set to. public virtual void SetVelocityX(float value) { Velocity = new Vector3(value, Velocity.y, Velocity.z); } /// /// Sets the y value. /// /// The value to set to. public virtual void SetVelocityY(float value) { Velocity = new Vector3(Velocity.x, value, Velocity.z); } /// /// Sets the z value. /// /// The value to set to. public virtual void SetVelocityZ(float value) { Velocity = new Vector3(Velocity.x, Velocity.y, value); } /// /// Clears . /// public virtual void ClearVelocity() { if (!this.IsValidState()) { return; } Velocity = default; } /// /// Sets the x value. /// /// The value to set to. public virtual void SetAngularVelocityX(float value) { AngularVelocity = new Vector3(value, AngularVelocity.y, AngularVelocity.z); } /// /// Sets the y value. /// /// The value to set to. public virtual void SetAngularVelocityY(float value) { AngularVelocity = new Vector3(AngularVelocity.x, value, AngularVelocity.z); } /// /// Sets the z value. /// /// The value to set to. public virtual void SetAngularVelocityZ(float value) { AngularVelocity = new Vector3(AngularVelocity.x, AngularVelocity.y, value); } /// /// Clears . /// public virtual void ClearAngularVelocity() { if (!this.IsValidState()) { return; } AngularVelocity = default; } /// /// Called after has been changed. /// protected virtual void OnAfterMassChange() { if (Target == null) { return; } Target.mass = Mass; } /// /// Called after has been changed. /// protected virtual void OnAfterDragChange() { if (Target == null) { return; } Target.drag = Drag; } /// /// Called after has been changed. /// protected virtual void OnAfterAngularDragChange() { if (Target == null) { return; } Target.angularDrag = AngularDrag; } /// /// Called after has been changed. /// protected virtual void OnAfterUseGravityChange() { if (Target == null) { return; } Target.useGravity = UseGravity; } /// /// Called after has been changed. /// protected virtual void OnAfterIsKinematicChange() { if (Target == null) { return; } Target.isKinematic = IsKinematic; } /// /// Called after has been changed. /// protected virtual void OnAfterVelocityChange() { if (Target == null || Target.isKinematic) { return; } Target.velocity = Velocity; } /// /// Called after has been changed. /// protected virtual void OnAfterAngularVelocityChange() { if (Target == null || Target.isKinematic) { return; } Target.angularVelocity = AngularVelocity; } /// /// Called after has been changed. /// protected virtual void OnAfterMaxAngularVelocityChange() { if (Target == null) { return; } Target.maxAngularVelocity = MaxAngularVelocity; } } }