Browse Source

Refactored ConversationManager to use IConversationManager interface

master
Christopher 8 years ago
parent
commit
c5899cde89
  1. 235
      Assets/Fungus/Narrative/Scripts/ConversationManager.cs
  2. 21
      Assets/Fungus/Narrative/Scripts/IConversationManager.cs
  3. 12
      Assets/Fungus/Narrative/Scripts/IConversationManager.cs.meta

235
Assets/Fungus/Narrative/Scripts/ConversationManager.cs

@ -6,10 +6,7 @@ using System.Text;
namespace Fungus namespace Fungus
{ {
/// <summary> public class ConversationManager : IConversationManager
/// Helper class to manage parsing and executing the conversation format.
/// </summary>
public class ConversationManager
{ {
protected struct ConversationItem protected struct ConversationItem
{ {
@ -26,12 +23,6 @@ namespace Fungus
protected bool exitSayWait; protected bool exitSayWait;
public void PopulateCharacterCache()
{
// cache characters for faster lookup
characters = UnityEngine.Object.FindObjectsOfType<Character>();
}
protected ISayDialog GetSayDialog(Character character) protected ISayDialog GetSayDialog(Character character)
{ {
ISayDialog sayDialog = null; ISayDialog sayDialog = null;
@ -51,115 +42,6 @@ namespace Fungus
return sayDialog; return sayDialog;
} }
/// <summary>
/// Parse and execute a conversation string
/// </summary>
/// <param name="conv"></param>
public IEnumerator DoConversation(string conv)
{
if (string.IsNullOrEmpty(conv))
{
yield break;
}
var conversationItems = Parse(conv);
if (conversationItems.Count == 0)
{
yield break;
}
// Track the current and previous parameter values
Character currentCharacter = null;
Sprite currentPortrait = null;
RectTransform currentPosition = null;
Character previousCharacter = null;
// Play the conversation
for (int i = 0; i < conversationItems.Count; ++i)
{
ConversationItem item = conversationItems[i];
if (item.Character != null)
{
currentCharacter = item.Character;
}
currentPortrait = item.Portrait;
currentPosition = item.Position;
ISayDialog sayDialog = GetSayDialog(currentCharacter);
if (sayDialog == null)
{
// Should never happen
yield break;
}
sayDialog.SetActive(true);
if (currentCharacter != null &&
currentCharacter != previousCharacter)
{
sayDialog.SetCharacter(currentCharacter);
}
var stage = Stage.GetActiveStage();
if (stage != null && currentCharacter != null &&
(currentPortrait != currentCharacter.State.portrait ||
currentPosition != currentCharacter.State.position))
{
var portraitOptions = new PortraitOptions(true);
portraitOptions.display = item.Hide ? DisplayType.Hide : DisplayType.Show;
portraitOptions.character = currentCharacter;
portraitOptions.fromPosition = currentCharacter.State.position;
portraitOptions.toPosition = currentPosition;
portraitOptions.portrait = currentPortrait;
//Flip option - Flip the opposite direction the character is currently facing
if (item.Flip) portraitOptions.facing = item.FacingDirection;
// Do a move tween if the character is already on screen and not yet at the specified position
if (currentCharacter.State.onScreen &&
currentPosition != currentCharacter.State.position)
{
portraitOptions.move = true;
}
if (item.Hide)
{
stage.Hide(portraitOptions);
}
else
{
stage.Show(portraitOptions);
}
}
if (stage == null &&
currentPortrait != null)
{
sayDialog.SetCharacterImage(currentPortrait);
}
previousCharacter = currentCharacter;
if (!string.IsNullOrEmpty(item.Text)) {
exitSayWait = false;
sayDialog.Say(item.Text, true, true, true, false, null, () => {
exitSayWait = true;
});
while (!exitSayWait)
{
yield return null;
}
exitSayWait = false;
}
}
}
protected virtual List<ConversationItem> Parse(string conv) protected virtual List<ConversationItem> Parse(string conv)
{ {
//find SimpleScript say strings with portrait options //find SimpleScript say strings with portrait options
@ -372,5 +254,120 @@ namespace Fungus
return results.ToArray(); return results.ToArray();
} }
#region IConversationManager
public void PopulateCharacterCache()
{
// cache characters for faster lookup
characters = UnityEngine.Object.FindObjectsOfType<Character>();
}
public IEnumerator DoConversation(string conv)
{
if (string.IsNullOrEmpty(conv))
{
yield break;
}
var conversationItems = Parse(conv);
if (conversationItems.Count == 0)
{
yield break;
}
// Track the current and previous parameter values
Character currentCharacter = null;
Sprite currentPortrait = null;
RectTransform currentPosition = null;
Character previousCharacter = null;
// Play the conversation
for (int i = 0; i < conversationItems.Count; ++i)
{
ConversationItem item = conversationItems[i];
if (item.Character != null)
{
currentCharacter = item.Character;
}
currentPortrait = item.Portrait;
currentPosition = item.Position;
ISayDialog sayDialog = GetSayDialog(currentCharacter);
if (sayDialog == null)
{
// Should never happen
yield break;
}
sayDialog.SetActive(true);
if (currentCharacter != null &&
currentCharacter != previousCharacter)
{
sayDialog.SetCharacter(currentCharacter);
}
var stage = Stage.GetActiveStage();
if (stage != null && currentCharacter != null &&
(currentPortrait != currentCharacter.State.portrait ||
currentPosition != currentCharacter.State.position))
{
var portraitOptions = new PortraitOptions(true);
portraitOptions.display = item.Hide ? DisplayType.Hide : DisplayType.Show;
portraitOptions.character = currentCharacter;
portraitOptions.fromPosition = currentCharacter.State.position;
portraitOptions.toPosition = currentPosition;
portraitOptions.portrait = currentPortrait;
//Flip option - Flip the opposite direction the character is currently facing
if (item.Flip) portraitOptions.facing = item.FacingDirection;
// Do a move tween if the character is already on screen and not yet at the specified position
if (currentCharacter.State.onScreen &&
currentPosition != currentCharacter.State.position)
{
portraitOptions.move = true;
}
if (item.Hide)
{
stage.Hide(portraitOptions);
}
else
{
stage.Show(portraitOptions);
}
}
if (stage == null &&
currentPortrait != null)
{
sayDialog.SetCharacterImage(currentPortrait);
}
previousCharacter = currentCharacter;
if (!string.IsNullOrEmpty(item.Text)) {
exitSayWait = false;
sayDialog.Say(item.Text, true, true, true, false, null, () => {
exitSayWait = true;
});
while (!exitSayWait)
{
yield return null;
}
exitSayWait = false;
}
}
}
#endregion
} }
} }

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

@ -0,0 +1,21 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/// <summary>
/// Helper class to manage parsing and executing the conversation format.
/// </summary>
public interface IConversationManager
{
/// <summary>
/// Caches the character objects in the scene for fast lookup during conversations.
/// </summary>
void PopulateCharacterCache();
/// <summary>
/// Parse and execute a conversation string.
/// </summary>
IEnumerator DoConversation(string conv);
}
}

12
Assets/Fungus/Narrative/Scripts/IConversationManager.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3a7ff247990a84e1a96765d1c03735b6
timeCreated: 1473679803
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save