Browse Source

Cache stage and portrait info and add ability to rearrange params

master
lealeelu 9 years ago
parent
commit
4992ea9925
  1. 30
      Assets/Fungus/Narrative/Scripts/Character.cs
  2. 34
      Assets/Fungus/Narrative/Scripts/PortraitController.cs
  3. 21
      Assets/Fungus/Narrative/Scripts/Stage.cs
  4. 131
      Assets/Fungus/Thirdparty/FungusLua/Scripts/ConversationManager.cs
  5. 13
      Assets/FungusExamples/FungusLua/Narrative/PortaitControlExample.unity

30
Assets/Fungus/Narrative/Scripts/Character.cs

@ -20,6 +20,7 @@ namespace Fungus
public AudioClip soundEffect; public AudioClip soundEffect;
public Sprite profileSprite; public Sprite profileSprite;
public List<Sprite> portraits; public List<Sprite> portraits;
public Sprite[] cachedPortraits;
public FacingDirection portraitsFace; public FacingDirection portraitsFace;
public PortraitState state; public PortraitState state;
@ -38,6 +39,12 @@ namespace Fungus
{ {
activeCharacters.Add(this); activeCharacters.Add(this);
} }
if (cachedPortraits == null)
{
cachedPortraits = new Sprite[portraits.Count];
portraits.CopyTo(cachedPortraits);
}
} }
protected virtual void OnDisable() protected virtual void OnDisable()
@ -59,30 +66,33 @@ namespace Fungus
nameText = standardText; nameText = standardText;
} }
public bool NameStartsWith(string matchString)
{
return name.StartsWith(matchString, true, System.Globalization.CultureInfo.CurrentCulture)
|| nameText.StartsWith(matchString, true, System.Globalization.CultureInfo.CurrentCulture);
}
/// <summary> /// <summary>
/// Looks for a portrait by name on a character /// Looks for a portrait by name on a character
/// If none is found, give a warning and return a blank sprite /// If none is found, give a warning and return a blank sprite
/// </summary> /// </summary>
/// <param name="portrait_string"></param> /// <param name="portrait_string"></param>
/// <returns>Character portrait sprite</returns> /// <returns>Character portrait sprite</returns>
public virtual Sprite GetPortrait(string portrait_string) public virtual Sprite GetPortrait(string portrait_string)
{ {
if (portrait_string == null) if (String.IsNullOrEmpty(portrait_string))
{ {
Debug.LogWarning("No portrait specifed for character " + name); return null;
//Would be nice to have a <picture missing> sprite show up instead
return new Sprite();
} }
foreach (Sprite portrait in portraits) for (int i = 0; i < cachedPortraits.Length; i++)
{ {
if ( String.Compare(portrait.name, portrait_string, true) == 0) if ( String.Compare(cachedPortraits[i].name, portrait_string, true) == 0)
{ {
return portrait; return cachedPortraits[i];
} }
} }
Debug.LogWarning("No portrait \"" + portrait_string + "\" found for character \"" + name + "\""); return null;
return new Sprite();
} }
public virtual string GetDescription() public virtual string GetDescription()

34
Assets/Fungus/Narrative/Scripts/PortraitController.cs

@ -53,7 +53,7 @@ namespace Fungus
waitUntilFinished = false; waitUntilFinished = false;
onComplete = null; onComplete = null;
// Special values that can be overriden // Special values that can be overridden
fadeDuration = 0.5f; fadeDuration = 0.5f;
moveDuration = 1f; moveDuration = 1f;
this.useDefaultSettings = useDefaultSettings; this.useDefaultSettings = useDefaultSettings;
@ -112,6 +112,7 @@ namespace Fungus
void Awake() void Awake()
{ {
stage = GetComponentInParent<Stage>(); stage = GetComponentInParent<Stage>();
stage.CachePositions();
} }
/// <summary> /// <summary>
@ -394,11 +395,13 @@ namespace Fungus
options.moveDuration = moveDuration; options.moveDuration = moveDuration;
options.waitUntilFinished = waitUntilFinished; options.waitUntilFinished = waitUntilFinished;
DoMoveTween(CleanPortraitOptions(options)); DoMoveTween(options);
} }
public void DoMoveTween(PortraitOptions options) public void DoMoveTween(PortraitOptions options)
{ {
CleanPortraitOptions(options);
// LeanTween doesn't handle 0 duration properly // LeanTween doesn't handle 0 duration properly
float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon; float duration = (options.moveDuration > 0f) ? options.moveDuration : float.Epsilon;
@ -422,7 +425,7 @@ namespace Fungus
options.character = character; options.character = character;
options.fromPosition = options.toPosition = stage.GetPosition(position); options.fromPosition = options.toPosition = stage.GetPosition(position);
Show(CleanPortraitOptions(options)); Show(options);
} }
/// <summary> /// <summary>
@ -441,7 +444,7 @@ namespace Fungus
options.toPosition = stage.GetPosition(toPosition); options.toPosition = stage.GetPosition(toPosition);
options.move = true; options.move = true;
Show(CleanPortraitOptions(options)); Show(options);
} }
/// <summary> /// <summary>
@ -453,7 +456,7 @@ namespace Fungus
/// <param name="optionsTable">Moonsharp Table</param> /// <param name="optionsTable">Moonsharp Table</param>
public void Show(Table optionsTable) public void Show(Table optionsTable)
{ {
Show(CleanPortraitOptions(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage))); Show(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage));
} }
/// <summary> /// <summary>
@ -462,6 +465,8 @@ namespace Fungus
/// <param name="options"></param> /// <param name="options"></param>
public void Show(PortraitOptions options) public void Show(PortraitOptions options)
{ {
options = CleanPortraitOptions(options);
if (options.shiftIntoPlace) if (options.shiftIntoPlace)
{ {
options.fromPosition = Instantiate(options.toPosition) as RectTransform; options.fromPosition = Instantiate(options.toPosition) as RectTransform;
@ -507,9 +512,12 @@ namespace Fungus
} }
// Fade in the new sprite image // Fade in the new sprite image
options.character.state.portraitImage.sprite = options.portrait; if (options.character.state.portraitImage.sprite != options.portrait)
options.character.state.portraitImage.color = new Color(1f, 1f, 1f, 0f); {
LeanTween.alpha(options.character.state.portraitImage.rectTransform, 1f, duration).setEase(stage.fadeEaseType); options.character.state.portraitImage.sprite = options.portrait;
options.character.state.portraitImage.color = new Color(1f, 1f, 1f, 0f);
LeanTween.alpha(options.character.state.portraitImage.rectTransform, 1f, duration).setEase(stage.fadeEaseType);
}
DoMoveTween(options); DoMoveTween(options);
@ -548,7 +556,7 @@ namespace Fungus
options.fromPosition = options.toPosition = character.state.position; options.fromPosition = options.toPosition = character.state.position;
} }
Show(CleanPortraitOptions(options)); Show(options);
} }
/// <summary> /// <summary>
@ -560,7 +568,7 @@ namespace Fungus
PortraitOptions options = new PortraitOptions(true); PortraitOptions options = new PortraitOptions(true);
options.character = character; options.character = character;
Hide(CleanPortraitOptions(options)); Hide(options);
} }
/// <summary> /// <summary>
@ -575,7 +583,7 @@ namespace Fungus
options.toPosition = stage.GetPosition(toPosition); options.toPosition = stage.GetPosition(toPosition);
options.move = true; options.move = true;
Hide(CleanPortraitOptions(options)); Hide(options);
} }
/// <summary> /// <summary>
@ -587,7 +595,7 @@ namespace Fungus
/// <param name="optionsTable">Moonsharp Table</param> /// <param name="optionsTable">Moonsharp Table</param>
public void Hide(Table optionsTable) public void Hide(Table optionsTable)
{ {
Hide(CleanPortraitOptions(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage))); Hide(PortraitUtil.ConvertTableToPortraitOptions(optionsTable, stage));
} }
/// <summary> /// <summary>
@ -596,6 +604,8 @@ namespace Fungus
/// <param name="options"></param> /// <param name="options"></param>
public void Hide(PortraitOptions options) public void Hide(PortraitOptions options)
{ {
CleanPortraitOptions(options);
if (options.character.state.display == DisplayType.None) if (options.character.state.display == DisplayType.None)
{ {
return; return;

21
Assets/Fungus/Narrative/Scripts/Stage.cs

@ -25,6 +25,7 @@ namespace Fungus
public Vector2 shiftOffset; public Vector2 shiftOffset;
public Image defaultPosition; public Image defaultPosition;
public List<RectTransform> positions; public List<RectTransform> positions;
public RectTransform[] cachedPositions;
public List<Character> charactersOnStage = new List<Character>(); public List<Character> charactersOnStage = new List<Character>();
[HideInInspector] [HideInInspector]
@ -38,6 +39,12 @@ namespace Fungus
} }
} }
public void CachePositions()
{
cachedPositions = new RectTransform[positions.Count];
positions.CopyTo(cachedPositions);
}
protected virtual void OnDisable() protected virtual void OnDisable()
{ {
activeStages.Remove(this); activeStages.Remove(this);
@ -61,21 +68,19 @@ namespace Fungus
/// <returns></returns> /// <returns></returns>
public RectTransform GetPosition(String position_string) public RectTransform GetPosition(String position_string)
{ {
if (position_string == null) if (string.IsNullOrEmpty(position_string))
{ {
Debug.LogWarning("Missing stage position."); return null;
return new RectTransform();
} }
foreach (RectTransform position in positions) for (int i = 0; i < cachedPositions.Length; i++)
{ {
if ( String.Compare(position.name.Replace(" ", ""), position_string.Replace(" ", ""), true) == 0 ) if ( String.Compare(cachedPositions[i].name, position_string, true) == 0 )
{ {
return position; return cachedPositions[i];
} }
} }
Debug.LogWarning("Unidentified stage position: " + position_string); return null;
return new RectTransform();
} }
} }
} }

131
Assets/Fungus/Thirdparty/FungusLua/Scripts/ConversationManager.cs vendored

@ -9,36 +9,24 @@ namespace Fungus
public class ConversationManager public class ConversationManager
{ {
protected Stage stage; protected Stage stage;
protected Character[] characters;
protected CharacterItem[] characters; protected Character currentCharacter;
protected Sprite currentPortrait;
protected RectTransform currentPosition;
protected struct CharacterItem protected Character previousCharacter;
{ protected Sprite previousPortrait;
public string name; protected RectTransform previousPosition;
public string nameText;
public Character character;
public bool CharacterMatch(string matchString)
{
return name.StartsWith(matchString, true, System.Globalization.CultureInfo.CurrentCulture)
|| nameText.StartsWith(matchString, true, System.Globalization.CultureInfo.CurrentCulture);
}
}
protected bool exitSayWait; protected bool exitSayWait;
public ConversationManager () { public ConversationManager ()
{
stage = Stage.activeStages[0]; stage = Stage.activeStages[0];
Character[] characterObjects = GameObject.FindObjectsOfType<Fungus.Character>(); // cache characters for faster lookup
characters = new CharacterItem[characterObjects.Length]; characters = GameObject.FindObjectsOfType<Fungus.Character>();
for (int i = 0; i < characterObjects.Length; i++)
{
characters[i].name = characterObjects[i].name;
characters[i].nameText = characterObjects[i].nameText;
characters[i].character = characterObjects[i];
}
} }
/// <summary> /// <summary>
@ -61,38 +49,57 @@ namespace Fungus
//find SimpleScript say strings with portrait options //find SimpleScript say strings with portrait options
//You can test regex matches here: http://regexstorm.net/tester //You can test regex matches here: http://regexstorm.net/tester
var sayRegex = new Regex(@"((?<character>\w*)( ?(?<portrait>\w*)( ?(?<position>\w*)))?:)?(?<text>.*\r*\n)"); Regex sayRegex = new Regex(@"((?<sayParams>[\w ,]*):)?(?<text>.*\r*\n)");
var sayMatches = sayRegex.Matches(conv); MatchCollection sayMatches = sayRegex.Matches(conv);
foreach (Match match in sayMatches) for (int i = 0; i < sayMatches.Count; i++)
{ {
string characterKey = match.Groups["character"].Value; previousCharacter = currentCharacter;
string portrait = match.Groups["portrait"].Value; previousPortrait = currentPortrait;
string position = match.Groups["position"].Value; previousPosition = currentPosition;
string text = match.Groups["text"].Value;
Character character = null;
sayDialog.gameObject.SetActive(true);
if (!string.IsNullOrEmpty(characterKey)) string sayParams = sayMatches[i].Groups["sayParams"].Value;
if (!string.IsNullOrEmpty(sayParams))
{ {
for (int i = 0; i < characters.Length; i++) string[] separateParams;
if (sayParams.Contains(","))
{ {
if( characters[i].CharacterMatch(characterKey)) { separateParams = sayParams.Split(',');
character = characters[i].character; for (int j = 0; j < separateParams.Length; j++)
break; {
separateParams[j] = separateParams[j].Trim();
} }
} }
else
{
separateParams = sayParams.Split(' ');
}
SetParams(separateParams);
}
else
{
//no params! Use previous settings?
}
string text = sayMatches[i].Groups["text"].Value;
sayDialog.gameObject.SetActive(true);
if (currentCharacter != null && currentCharacter != previousCharacter)
{
sayDialog.SetCharacter(currentCharacter);
} }
//We'll assume that if no character is specified, we'll leave it as the last one.
//It's probably a continuation of the last character's dialog if (stage != null && currentCharacter != null && (currentPortrait != previousPortrait || currentPosition != previousPosition))
if (character != null)
{ {
sayDialog.SetCharacter(character); PortraitOptions portraitOptions = new PortraitOptions(true);
portraitOptions.character = currentCharacter;
portraitOptions.fromPosition = currentCharacter.state.position ?? previousPosition;
portraitOptions.toPosition = currentPosition;
portraitOptions.portrait = currentPortrait;
string currentPosition = null; stage.Show(portraitOptions);
if (character.state.position != null) currentPosition = character.state.position.name;
if (stage != null) stage.Show(character, portrait, currentPosition ?? position, position);
} }
exitSayWait = false; exitSayWait = false;
@ -106,7 +113,39 @@ namespace Fungus
} }
exitSayWait = false; exitSayWait = false;
} }
}
/// <summary>
/// Using the string of say parameters before the ':',
/// set the current character, position and portrait if provided.
/// </summary>
/// <param name="sayParams">The list of say parameters</param>
private void SetParams(string[] sayParams)
{
Sprite portrait = null;
RectTransform position = null;
//find the character first, since we need to get its portrait
for (int i = 0; i < sayParams.Length; i++)
{
for (int j = 0; j < characters.Length; j++)
{
if (characters[j].NameStartsWith(sayParams[i]))
{
currentCharacter = characters[j];
break;
}
}
}
for (int i = 0; i < sayParams.Length; i++)
{
if (portrait == null) portrait = currentCharacter.GetPortrait(sayParams[i]);
if (position == null) position = stage.GetPosition(sayParams[i]);
}
currentPosition = position;
currentPortrait = portrait;
} }
} }
} }

13
Assets/FungusExamples/FungusLua/Narrative/PortaitControlExample.unity

@ -399,9 +399,10 @@ MonoBehaviour:
indentLevel: 0 indentLevel: 0
luaEnvironment: {fileID: 1332690758} luaEnvironment: {fileID: 1332690758}
luaFile: {fileID: 0} luaFile: {fileID: 0}
luaScript: "conversation [[\r\nWhat is that smell??\nsherlock: Ugh.\r\nS bored:ew!\r\nwatson luaScript: "conversation [[\r\nsherlock right: Character+position.\r\nThis is a
annoyed left: That was me.\r\nW confident right: I'm not Sorry!\r\nW confident continuation of the last line.\nS bored left:All three options with name shortened.\nwatson
offscreenright: GOOD DAY!\n]]" annoyed right: Adding a new character on the right.\r\nW, confident, offscreen
right: Move Watson offscreen using commas.\r\n]]"
runAsCoroutine: 1 runAsCoroutine: 1
waitUntilFinished: 1 waitUntilFinished: 1
returnVariable: {fileID: 0} returnVariable: {fileID: 0}
@ -716,6 +717,9 @@ MonoBehaviour:
portraits: portraits:
- {fileID: 21300000, guid: a92b08a118b7d46f59dd091acb2e4102, type: 3} - {fileID: 21300000, guid: a92b08a118b7d46f59dd091acb2e4102, type: 3}
- {fileID: 21300000, guid: 03bc547cc0049594bae51f00903eedef, type: 3} - {fileID: 21300000, guid: 03bc547cc0049594bae51f00903eedef, type: 3}
cachedPortraits:
- {fileID: 21300000, guid: a92b08a118b7d46f59dd091acb2e4102, type: 3}
- {fileID: 21300000, guid: 03bc547cc0049594bae51f00903eedef, type: 3}
portraitsFace: 0 portraitsFace: 0
setSayDialog: {fileID: 0} setSayDialog: {fileID: 0}
description: description:
@ -766,6 +770,9 @@ MonoBehaviour:
portraits: portraits:
- {fileID: 21300000, guid: 8f5b5b110e73a414eb229eeead86200f, type: 3} - {fileID: 21300000, guid: 8f5b5b110e73a414eb229eeead86200f, type: 3}
- {fileID: 21300000, guid: c779e34c6eb8e45da98c70cf2802a54c, type: 3} - {fileID: 21300000, guid: c779e34c6eb8e45da98c70cf2802a54c, type: 3}
cachedPortraits:
- {fileID: 21300000, guid: 8f5b5b110e73a414eb229eeead86200f, type: 3}
- {fileID: 21300000, guid: c779e34c6eb8e45da98c70cf2802a54c, type: 3}
portraitsFace: 0 portraitsFace: 0
setSayDialog: {fileID: 0} setSayDialog: {fileID: 0}
description: description:

Loading…
Cancel
Save