Browse Source

Safely get active stage, handles case where no stage present.

Ignore blank lines and Lua style comments in conversation string.
SetParams checks input params in order of priority, handles some cases of missing params (not all yet)
master
Christopher 8 years ago committed by lealeelu
parent
commit
86855857d8
  1. 94
      Assets/Fungus/Thirdparty/FungusLua/Scripts/ConversationManager.cs

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

@ -8,7 +8,6 @@ namespace Fungus
{ {
public class ConversationManager public class ConversationManager
{ {
protected Stage stage;
protected Character[] characters; protected Character[] characters;
protected Character currentCharacter; protected Character currentCharacter;
@ -23,12 +22,21 @@ namespace Fungus
public ConversationManager () public ConversationManager ()
{ {
stage = Stage.activeStages[0];
// cache characters for faster lookup // cache characters for faster lookup
characters = GameObject.FindObjectsOfType<Fungus.Character>(); characters = GameObject.FindObjectsOfType<Fungus.Character>();
} }
protected Stage GetActiveStage()
{
if (Stage.activeStages == null ||
Stage.activeStages.Count == 0)
{
return null;
}
return Stage.activeStages[0];
}
/// <summary> /// <summary>
/// Parse and execute a conversation string /// Parse and execute a conversation string
/// </summary> /// </summary>
@ -91,6 +99,8 @@ namespace Fungus
sayDialog.SetCharacter(currentCharacter); sayDialog.SetCharacter(currentCharacter);
} }
Stage stage = GetActiveStage();
if (stage != null && currentCharacter != null && (currentPortrait != previousPortrait || currentPosition != previousPosition)) if (stage != null && currentCharacter != null && (currentPortrait != previousPortrait || currentPosition != previousPosition))
{ {
PortraitOptions portraitOptions = new PortraitOptions(true); PortraitOptions portraitOptions = new PortraitOptions(true);
@ -102,6 +112,12 @@ namespace Fungus
stage.Show(portraitOptions); stage.Show(portraitOptions);
} }
// Ignore Lua style comments and blank lines
if (text.StartsWith("--") || text.Trim() == "")
{
continue;
}
exitSayWait = false; exitSayWait = false;
sayDialog.Say(text, true, true, true, false, null, () => { sayDialog.Say(text, true, true, true, false, null, () => {
exitSayWait = true; exitSayWait = true;
@ -123,29 +139,81 @@ namespace Fungus
/// <param name="sayParams">The list of say parameters</param> /// <param name="sayParams">The list of say parameters</param>
private void SetParams(string[] sayParams) private void SetParams(string[] sayParams)
{ {
Character character = null;
Sprite portrait = null; Sprite portrait = null;
RectTransform position = null; RectTransform position = null;
//find the character first, since we need to get its portrait // try to find the character first, since we need to get its portrait
for (int i = 0; i < sayParams.Length; i++) int characterIndex = -1;
for (int i = 0; character == null && i < sayParams.Length; i++)
{ {
for (int j = 0; j < characters.Length; j++) for (int j = 0; j < characters.Length; j++)
{ {
if (characters[j].NameStartsWith(sayParams[i])) if (characters[j].NameStartsWith(sayParams[i]))
{ {
currentCharacter = characters[j]; characterIndex = i;
character = characters[j];
break; break;
} }
} }
} }
for (int i = 0; i < sayParams.Length; i++) // Assume last used character if none is specified now
{ if (character == null)
if (portrait == null) portrait = currentCharacter.GetPortrait(sayParams[i]); {
if (position == null) position = stage.GetPosition(sayParams[i]); character = currentCharacter;
} }
currentPosition = position;
currentPortrait = portrait; // Next see if we can find a portrait for this character
int portraitIndex = -1;
if (character != null)
{
for (int i = 0; i < sayParams.Length; i++)
{
if (portrait == null &&
character != null &&
i != characterIndex)
{
Sprite s = character.GetPortrait(sayParams[i]);
if (s != null)
{
portraitIndex = i;
portrait = s;
break;
}
}
}
}
// Next check if there's a position parameter
Stage stage = GetActiveStage();
if (stage != null)
{
for (int i = 0; i < sayParams.Length; i++)
{
if (i != characterIndex &&
i != portraitIndex)
{
position = stage.GetPosition(sayParams[i]);
break;
}
}
}
if (character != null)
{
currentCharacter = character;
}
if (portrait != null)
{
currentPortrait = portrait;
}
if (position != null)
{
currentPosition = position;
}
} }
} }
} }
Loading…
Cancel
Save