using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.Cinemachine
{
///
/// An event-driven class that broadcasts an impulse signal to listeners.
///
/// This is the base class for custom impulse sources. It contains an impulse
/// definition, where the characteristics of the impulse signal are defined.
///
/// API methods are provided for actually broadcasting the impulse. Call these
/// methods from your custom code, or hook them up to game events in the Editor.
///
///
[SaveDuringPlay]
[AddComponentMenu("Cinemachine/Helpers/Cinemachine Impulse Source")]
[HelpURL(Documentation.BaseURL + "manual/CinemachineImpulseSource.html")]
public class CinemachineImpulseSource : MonoBehaviour
{
///
/// This defines the complete impulse signal that will be broadcast.
///
[FormerlySerializedAs("m_ImpulseDefinition")]
public CinemachineImpulseDefinition ImpulseDefinition = new CinemachineImpulseDefinition();
///
/// The default direction and force of the Impulse Signal in the absence of any
/// specified overrides. Overrides can be specified by calling the appropriate
/// GenerateImpulse method in the API.
///
[Header("Default Invocation")]
[Tooltip("The default direction and force of the Impulse Signal in the absense "
+ "of any specified overrides. Overrides can be specified by calling the appropriate "
+ "GenerateImpulse method in the API.")]
[FormerlySerializedAs("m_DefaultVelocity")]
public Vector3 DefaultVelocity = Vector3.down;
void OnValidate()
{
ImpulseDefinition.OnValidate();
}
void Reset()
{
ImpulseDefinition = new CinemachineImpulseDefinition
{
ImpulseChannel = 1,
ImpulseShape = CinemachineImpulseDefinition.ImpulseShapes.Bump,
CustomImpulseShape = new AnimationCurve(),
ImpulseDuration = 0.2f,
ImpulseType = CinemachineImpulseDefinition.ImpulseTypes.Uniform,
DissipationDistance = 100,
DissipationRate = 0.25f,
PropagationSpeed = 343
};
DefaultVelocity = Vector3.down;
}
/// Broadcast the Impulse Signal onto the appropriate channels,
/// using a custom position and impact velocity
/// The world-space position from which the impulse will emanate
/// The impact magnitude and direction
public void GenerateImpulseAtPositionWithVelocity(Vector3 position, Vector3 velocity)
{
if (ImpulseDefinition != null)
ImpulseDefinition.CreateEvent(position, velocity);
}
/// Broadcast the Impulse Signal onto the appropriate channels, using
/// a custom impact velocity, and this transfom's position.
/// The impact magnitude and direction
public void GenerateImpulseWithVelocity(Vector3 velocity)
{
GenerateImpulseAtPositionWithVelocity(transform.position, velocity);
}
/// Broadcast the Impulse Signal onto the appropriate channels, using
/// a custom impact force, with the standard direction, and this transfom's position.
/// The impact magnitude. 1 is normal
public void GenerateImpulseWithForce(float force)
{
GenerateImpulseAtPositionWithVelocity(transform.position, DefaultVelocity * force);
}
/// Broadcast the Impulse Signal onto the appropriate channels,
/// with default velocity = (0, -1, 0), and a default position which is
/// this transform's location.
public void GenerateImpulse()
{
GenerateImpulseWithVelocity(DefaultVelocity);
}
/// Legacy API: Please use GenerateImpulseAtPositionWithVelocity() instead.
/// Broadcast the Impulse Signal onto the appropriate channels,
/// using a custom position and impact velocity
/// The world-space position from which the impulse will emanate
/// The impact magnitude and direction
public void GenerateImpulseAt(Vector3 position, Vector3 velocity)
=> GenerateImpulseAtPositionWithVelocity(position, velocity);
/// Legacy API: Please use GenerateImpulseWithVelocity() instead.
/// Broadcast the Impulse Signal onto the appropriate channels, using
/// a custom impact velocity, and this transfom's position.
/// The impact magnitude and direction
public void GenerateImpulse(Vector3 velocity) => GenerateImpulseWithVelocity(velocity);
/// Legacy API: Please use GenerateImpulseWithForce() instead.
/// Broadcast the Impulse Signal onto the appropriate channels, using
/// a custom impact force, with the standard direction, and this transfom's position.
/// The impact magnitude. 1 is normal
public void GenerateImpulse(float force) => GenerateImpulseWithForce(force);
}
}