namespace Zinnia.Tracking.Follow.Modifier
{
using UnityEngine;
using Zinnia.Extension;
using Zinnia.Tracking.Follow.Modifier.Property;
///
/// Composes the logic to modify the position, rotation and scale of a .
///
public class FollowModifier : MonoBehaviour
{
[Tooltip("The modifier to change the scale.")]
[SerializeField]
private PropertyModifier scaleModifier;
///
/// The modifier to change the scale.
///
public PropertyModifier ScaleModifier
{
get
{
return scaleModifier;
}
set
{
scaleModifier = value;
}
}
[Tooltip("The modifier to change the rotation.")]
[SerializeField]
private PropertyModifier rotationModifier;
///
/// The modifier to change the rotation.
///
public PropertyModifier RotationModifier
{
get
{
return rotationModifier;
}
set
{
rotationModifier = value;
}
}
[Tooltip("The modifier to change the position.")]
[SerializeField]
private PropertyModifier positionModifier;
///
/// The modifier to change the position.
///
public PropertyModifier PositionModifier
{
get
{
return positionModifier;
}
set
{
positionModifier = value;
}
}
///
/// Emitted before the follow is modified.
///
public ObjectFollower.FollowEvent Premodified = new ObjectFollower.FollowEvent();
///
/// Emitted after the follow is modified.
///
public ObjectFollower.FollowEvent Modified = new ObjectFollower.FollowEvent();
/// The current source being used in the modifier process.
public GameObject CachedSource { get; protected set; }
/// The current target being used in the modifier process.
public GameObject CachedTarget { get; protected set; }
///
/// The current being used as the offset in the modifier process.
///
public GameObject CachedOffset { get; protected set; }
///
/// The event data to emit before and after each property type has been modified.
///
protected readonly ObjectFollower.EventData eventData = new ObjectFollower.EventData();
///
/// Clears .
///
public virtual void ClearScaleModifier()
{
if (!this.IsValidState())
{
return;
}
ScaleModifier = default;
}
///
/// Clears .
///
public virtual void ClearRotationModifier()
{
if (!this.IsValidState())
{
return;
}
RotationModifier = default;
}
///
/// Clears .
///
public virtual void ClearPositionModifier()
{
if (!this.IsValidState())
{
return;
}
PositionModifier = default;
}
///
/// Attempts to call each of the given options to modify the target.
///
/// The source to utilize in the modification.
/// The target to modify.
/// The offset of the target against the source when modifying.
public virtual void Modify(GameObject source, GameObject target, GameObject offset = null)
{
if (!this.IsValidState() || !ValidateCache(source, target, offset))
{
return;
}
Premodified?.Invoke(eventData.Set(source, target, offset));
if (ScaleModifier != null)
{
ScaleModifier.Modify(source, target, offset);
}
if (RotationModifier != null)
{
RotationModifier.Modify(source, target, offset);
}
if (PositionModifier != null)
{
PositionModifier.Modify(source, target, offset);
}
Modified?.Invoke(eventData.Set(source, target, offset));
}
///
/// Caches the given source and target then determines if the set cache is valid.
///
/// The source to utilize in the modification.
/// The target to modify.
/// The offset of the target against the source when modifying.
/// Whether the cache contains a valid source and target.
protected virtual bool ValidateCache(GameObject source, GameObject target, GameObject offset = null)
{
CachedSource = source;
CachedTarget = target;
CachedOffset = offset;
return CachedSource != null && CachedTarget != null;
}
}
}