// This code is part of the Fungus library (https://github.com/snozbot/fungus) // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) using UnityEngine; using UnityEngine.Serialization; using System.Collections.Generic; namespace Fungus { /// /// A Character that can be used in dialogue via the Say, Conversation and Portrait commands. /// [ExecuteInEditMode] public class Character : MonoBehaviour, ILocalizable, IComparer { [Tooltip("Character name as displayed in Say Dialog.")] [SerializeField] protected string nameText; // We need a separate name as the object name is used for character variations (e.g. "Smurf Happy", "Smurf Sad") [Tooltip("Color to display the character name in Say Dialog.")] [SerializeField] protected Color nameColor = Color.white; [Tooltip("Sound effect to play when this character is speaking.")] [SerializeField] protected AudioClip soundEffect; [Tooltip("List of portrait images that can be displayed for this character.")] [SerializeField] protected List portraits; [Tooltip("Direction that portrait sprites face.")] [SerializeField] protected FacingDirection portraitsFace; [Tooltip("Sets the active Say dialog with a reference to a Say Dialog object in the scene. This Say Dialog will be used whenever the character speaks.")] [SerializeField] protected SayDialog setSayDialog; [FormerlySerializedAs("notes")] [TextArea(5,10)] [SerializeField] protected string description; protected PortraitState portaitState = new PortraitState(); protected static List activeCharacters = new List(); protected virtual void OnEnable() { if (!activeCharacters.Contains(this)) { activeCharacters.Add(this); activeCharacters.Sort(this); } } protected virtual void OnDisable() { activeCharacters.Remove(this); } #region Public members /// /// Gets the list of active characters. /// public static List ActiveCharacters { get { return activeCharacters; } } /// /// Character name as displayed in Say Dialog. /// public virtual string NameText { get { return nameText; } } /// /// Color to display the character name in Say Dialog. /// public virtual Color NameColor { get { return nameColor; } } /// /// Sound effect to play when this character is speaking. /// /// The sound effect. public virtual AudioClip SoundEffect { get { return soundEffect; } } /// /// List of portrait images that can be displayed for this character. /// public virtual List Portraits { get { return portraits; } } /// /// Direction that portrait sprites face. /// public virtual FacingDirection PortraitsFace { get { return portraitsFace; } } /// /// Currently display profile sprite for this character. /// /// The profile sprite. public virtual Sprite ProfileSprite { get; set; } /// /// Current display state of this character's portrait. /// /// The state. public virtual PortraitState State { get { return portaitState; } } /// /// Sets the active Say dialog with a reference to a Say Dialog object in the scene. This Say Dialog will be used whenever the character speaks. /// public virtual SayDialog SetSayDialog { get { return setSayDialog; } } /// /// Returns the name of the game object. /// public string GetObjectName() { return gameObject.name; } /// /// Returns true if the character name starts with the specified string. Case insensitive. /// public virtual bool NameStartsWith(string matchString) { #if NETFX_CORE return name.StartsWith(matchString, StringComparison.CurrentCultureIgnoreCase) || nameText.StartsWith(matchString, StringComparison.CurrentCultureIgnoreCase); #else return name.StartsWith(matchString, true, System.Globalization.CultureInfo.CurrentCulture) || nameText.StartsWith(matchString, true, System.Globalization.CultureInfo.CurrentCulture); #endif } public int Compare(Character x, Character y) { if (x == y) return 0; if (y == null) return 1; if (x == null) return -1; return x.name.CompareTo(y.name); } /// /// Looks for a portrait by name on a character /// If none is found, give a warning and return a blank sprite /// public virtual Sprite GetPortrait(string portraitString) { if (string.IsNullOrEmpty(portraitString)) { return null; } for (int i = 0; i < portraits.Count; i++) { if (portraits[i] != null && string.Compare(portraits[i].name, portraitString, true) == 0) { return portraits[i]; } } return null; } #endregion #region ILocalizable implementation public virtual string GetStandardText() { return nameText; } public virtual void SetStandardText(string standardText) { nameText = standardText; } public virtual string GetDescription() { return description; } public virtual string GetStringId() { // String id for character names is CHARACTER. return "CHARACTER." + nameText; } #endregion protected virtual void OnValidate() { if (portraits != null && portraits.Count > 1) { portraits.Sort(PortraitUtil.PortraitCompareTo); } } } }