// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
namespace PixelCrushers.DialogueSystem.SequencerCommands
{
///
/// Base class for Sequencer commands.
///
public abstract class SequencerCommand : MonoBehaviour
{
///
/// Indicates whether the command is still playing. When your custom command is done, it
/// should call Stop() to set this to false.
///
///
/// true if this instance is still playing; otherwise, false.
///
[HideInInspector] public bool isPlaying = true;
///
/// Reference to the Sequencer, so you can access its properties such as SequencerCamera
/// and CameraAngles.
///
///
/// The sequencer.
///
protected Sequencer sequencer
{
get
{
if (m_sequencer == null) m_sequencer = Sequencer.s_awakeSequencer;
return m_sequencer;
}
private set { m_sequencer = value; }
}
private Sequencer m_sequencer = null;
///
/// The parameters for the command.
///
///
/// The parameters.
///
protected string[] parameters
{
get
{
if (m_parameters == null) m_parameters = Sequencer.s_awakeArgs;
return m_parameters;
}
private set { m_parameters = value; }
}
private string[] m_parameters = null;
///
/// Optional message to send the sequencer when the command completes. The sequencer
/// sends this message. The command itself is not responsible for sending it.
///
/// The end message.
public string endMessage
{
get
{
if (m_endMessage == null) m_endMessage = Sequencer.s_awakeEndMessage;
return m_endMessage;
}
private set { m_endMessage = value; }
}
private string m_endMessage = null;
private Transform m_speaker = null;
protected Transform speaker { get { return (m_speaker != null) ? m_speaker : (Sequencer != null) ? Sequencer.Speaker : null; } }
private Transform m_listener = null;
protected Transform listener { get { return (m_listener != null) ? m_listener : (Sequencer != null) ? Sequencer.Listener : null; } }
/// @cond FOR_V1_COMPATIBILITY
public bool IsPlaying { get { return isPlaying; } protected set { isPlaying = value; } }
protected Sequencer Sequencer { get { return sequencer; } private set { sequencer = value; } }
protected string[] Parameters { get { return parameters; } private set { parameters = value; } }
/// @endcond
///
/// Initializes the base properties. The Sequencer calls this method before handing control
/// to the command.
///
///
/// A reference to the Sequencer.
///
///
/// The parameters for the command.
///
public void Initialize(Sequencer sequencer, string endMessage, Transform speaker, Transform listener, params string[] parameters)
{
this.sequencer = sequencer;
this.endMessage = endMessage;
this.parameters = parameters;
this.m_speaker = speaker;
this.m_listener = listener;
}
///
/// Initializes the base properties. The Sequencer calls this method before handing control
/// to the command.
///
///
/// A reference to the Sequencer.
///
///
/// The parameters for the command.
///
public void Initialize(Sequencer sequencer, Transform speaker, Transform listener, params string[] parameters)
{
Initialize(sequencer, null, speaker, listener, parameters);
}
///
/// Call this method to indicate that the command is done playing.
///
protected void Stop()
{
isPlaying = false;
}
///
/// Sequencer commands usually specify a subject to which the command applies (e.g., where
/// to aim the camera). This utility function returns the specified subject.
///
///
/// The transform of the specified subject, or null if the specifier names a game object
/// that isn't in the scene.
///
///
/// "speaker", "listener", or the name of a game object in the scene.
///
///
/// Default subject (overrides speaker).
///
protected Transform GetSubject(string specifier, Transform defaultSubject = null)
{
return SequencerTools.GetSubject(specifier, speaker, listener, defaultSubject);
}
///
/// Sequencer commands usually specify a subject to which the command applies (e.g., where
/// to aim the camera). This utility function returns the specified subject.
///
///
/// The transform of the specified subject, or null if the specifier names a game object
/// that isn't in the scene.
///
///
/// The parameter index number (zero-based) that names the subject.
///
///
/// Default subject (overrides speaker).
///
protected Transform GetSubject(int i, Transform defaultSubject = null)
{
return GetSubject(GetParameter(i), defaultSubject);
}
///
/// Gets the i-th parameter (zero-based).
///
///
/// The i-th parameter, or the specified default value if i is out of range.
///
///
/// The parameter index number (zero-based).
///
///
/// The default value to return if i is out of range.
///
protected string GetParameter(int i, string defaultValue = null)
{
return SequencerTools.GetParameter(parameters, i, defaultValue);
}
///
/// Gets the i-th parameter (zero-based) as a specified type.
///
///
/// The i-th parameter as type T, or the specified default value if i is out of
/// range or the parameter can't be converted to type T.
///
///
/// The parameter index number (zero-based).
///
///
/// The default value to return if i is out of range or the parameter can't be
/// converted to type T.
///
///
/// The type to convert the parameter to.
///
///
/// // Get the second parameter as a float, defaulting to 5f:
/// float duration = GetParameterAs(1, 5f);
///
protected T GetParameterAs(int i, T defaultValue)
{
return SequencerTools.GetParameterAs(parameters, i, defaultValue);
}
///
/// Gets the i-th parameter as a float.
///
///
/// The parameter as float, or defaultValue if out of range.
///
protected float GetParameterAsFloat(int i, float defaultValue = 0)
{
return GetParameterAs(i, defaultValue);
}
///
/// Gets the i-th parameter as an int.
///
///
/// The parameter as int, or defaultValue if out of range.
///
protected int GetParameterAsInt(int i, int defaultValue = 0)
{
return GetParameterAs(i, defaultValue);
}
///
/// Gets the i-th parameter as a bool.
///
///
/// The parameter as bool, or defaultValue if out of range.
///
///
/// The zero-based index of the parameter.
///
///
/// If set to true default value.
///
protected bool GetParameterAsBool(int i, bool defaultValue = false)
{
return GetParameterAs(i, defaultValue);
}
///
/// Gets the parameters as a comma-separated string.
///
///
/// The parameters.
///
protected string GetParameters()
{
return (parameters != null) ? string.Join(",", parameters) : string.Empty;
}
///
/// Checks whether a Lua variable "Mute" is defined and true. If so, this
/// indicates that audio should be muted.
///
/// true if audio is muted; otherwise, false.
protected bool IsAudioMuted()
{
return SequencerTools.IsAudioMuted();
}
}
}