An easy to use Unity 3D library for creating illustrated Interactive Fiction games and more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.0 KiB

// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// 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;
using System;
namespace Fungus
{
/// <summary>
/// A Character that can be used in dialogue via the Say, Conversation and Portrait commands.
/// </summary>
[ExecuteInEditMode]
public class Character : MonoBehaviour, ILocalizable
{
[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<Sprite> 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<Character> activeCharacters = new List<Character>();
protected virtual void OnEnable()
{
if (!activeCharacters.Contains(this))
{
activeCharacters.Add(this);
}
}
protected virtual void OnDisable()
{
activeCharacters.Remove(this);
}
#region Public members
/// <summary>
/// Gets the list of active characters.
/// </summary>
public static List<Character> ActiveCharacters { get { return activeCharacters; } }
/// <summary>
/// Character name as displayed in Say Dialog.
/// </summary>
public virtual string NameText { get { return nameText; } }
/// <summary>
/// Color to display the character name in Say Dialog.
/// </summary>
public virtual Color NameColor { get { return nameColor; } }
/// <summary>
/// Sound effect to play when this character is speaking.
/// </summary>
/// <value>The sound effect.</value>
public virtual AudioClip SoundEffect { get { return soundEffect; } }
/// <summary>
/// List of portrait images that can be displayed for this character.
/// </summary>
public virtual List<Sprite> Portraits { get { return portraits; } }
/// <summary>
/// Direction that portrait sprites face.
/// </summary>
public virtual FacingDirection PortraitsFace { get { return portraitsFace; } }
/// <summary>
/// Currently display profile sprite for this character.
/// </summary>
/// <value>The profile sprite.</value>
public virtual Sprite ProfileSprite { get; set; }
/// <summary>
/// Current display state of this character's portrait.
/// </summary>
/// <value>The state.</value>
public virtual PortraitState State { get { return portaitState; } }
/// <summary>
/// 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.
/// </summary>
public virtual SayDialog SetSayDialog { get { return setSayDialog; } }
/// <summary>
/// Returns the name of the game object.
/// </summary>
public string GetObjectName() { return gameObject.name; }
/// <summary>
/// Returns true if the character name starts with the specified string. Case insensitive.
/// </summary>
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
}
/// <summary>
/// Looks for a portrait by name on a character
/// If none is found, give a warning and return a blank sprite
/// </summary>
public virtual Sprite GetPortrait(string portraitString)
9 years ago
{
if (String.IsNullOrEmpty(portraitString))
9 years ago
{
return null;
9 years ago
}
for (int i = 0; i < portraits.Count; i++)
9 years ago
{
if (portraits[i] != null && String.Compare(portraits[i].name, portraitString, true) == 0)
9 years ago
{
return portraits[i];
9 years ago
}
}
return null;
9 years ago
}
#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.<Character Name>
return "CHARACTER." + nameText;
}
#endregion
}
}