Browse Source

Create Conversation Function in LuaUtils

master
lealeelu 9 years ago
parent
commit
7420ffeefb
  1. 2
      Assets/Fungus/Narrative/Scripts/Stage.cs
  2. 7
      Assets/Fungus/Thirdparty/FungusLua/Resources/Lua/fungus.txt
  3. 112
      Assets/Fungus/Thirdparty/FungusLua/Scripts/ConversationManager.cs
  4. 12
      Assets/Fungus/Thirdparty/FungusLua/Scripts/ConversationManager.cs.meta
  5. 13
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs
  6. 117
      Assets/FungusExamples/FungusLua/Narrative/PortaitControlExample.unity

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

@ -69,7 +69,7 @@ namespace Fungus
foreach (RectTransform position in positions)
{
if ( String.Compare(position.name, position_string, true) == 0 )
if ( String.Compare(position.name.Replace(" ", ""), position_string.Replace(" ", ""), true) == 0 )
{
return position;
}

7
Assets/Fungus/Thirdparty/FungusLua/Resources/Lua/fungus.txt vendored

@ -197,6 +197,13 @@ function M.say(text, voiceclip)
M.runwait(e)
end
-- Say series of lines
-- conv: A string of conversational lines
function M.conversation(conv)
local e = luautils.Conversation(conv)
M.runwait(e)
end
--------------
-- Menu Dialog
--------------

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

@ -0,0 +1,112 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
namespace Fungus
{
public class ConversationManager
{
protected Stage stage;
protected CharacterItem[] characters;
protected struct CharacterItem
{
public string name;
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;
public ConversationManager () {
stage = Stage.activeStages[0];
Character[] characterObjects = GameObject.FindObjectsOfType<Fungus.Character>();
characters = new CharacterItem[characterObjects.Length];
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>
/// Parse and execute a conversation string
/// </summary>
/// <param name="conv"></param>
public IEnumerator Conversation(string conv)
{
if (string.IsNullOrEmpty(conv))
{
yield break;
}
SayDialog sayDialog = SayDialog.GetSayDialog();
if (sayDialog == null)
{
yield break;
}
//find SimpleScript say strings with portrait options
//You can test regex matches here: http://regexstorm.net/tester
var sayRegex = new Regex(@"((?<character>\w*)( ?(?<portrait>\w*)( ?(?<position>\w*)))?:)?(?<text>.*\r*\n)");
var sayMatches = sayRegex.Matches(conv);
foreach (Match match in sayMatches)
{
string characterKey = match.Groups["character"].Value;
string portrait = match.Groups["portrait"].Value;
string position = match.Groups["position"].Value;
string text = match.Groups["text"].Value;
Character character = null;
sayDialog.gameObject.SetActive(true);
if (!string.IsNullOrEmpty(characterKey))
{
for (int i = 0; i < characters.Length; i++)
{
if( characters[i].CharacterMatch(characterKey)) {
character = characters[i].character;
break;
}
}
}
//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 (character != null)
{
sayDialog.SetCharacter(character);
string currentPosition = null;
if (character.state.position != null) currentPosition = character.state.position.name;
if (stage != null) stage.Show(character, portrait, currentPosition ?? position, position);
}
exitSayWait = false;
sayDialog.Say(text, true, true, true, false, null, () => {
exitSayWait = true;
});
while (!exitSayWait)
{
yield return null;
}
exitSayWait = false;
}
}
}
}

12
Assets/Fungus/Thirdparty/FungusLua/Scripts/ConversationManager.cs.meta vendored

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6ee240b2e8b559946a44b33a90cca00f
timeCreated: 1467644516
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

13
Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs vendored

@ -72,6 +72,8 @@ namespace Fungus
protected StringSubstituter stringSubstituter;
protected ConversationManager conversationManager;
/// <summary>
/// Called by LuaEnvironment when initializing.
/// </summary>
@ -283,6 +285,8 @@ namespace Fungus
stringSubstituter = new StringSubstituter();
conversationManager = new ConversationManager();
if (fungusModule == FungusModuleOptions.UseGlobalVariables)
{
// Copy all items from the Fungus table to global variables
@ -466,6 +470,15 @@ namespace Fungus
}
return null;
}
/// <summary>
/// Use the conversation manager to play out a conversation
/// </summary>
/// <param name="conv"></param>
public virtual IEnumerator Conversation(string conv)
{
return conversationManager.Conversation(conv);
}
}
}

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

@ -168,7 +168,6 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
@ -185,6 +184,7 @@ GameObject:
- 114: {fileID: 574033267}
- 114: {fileID: 574033272}
- 114: {fileID: 574033266}
- 114: {fileID: 574033273}
m_Layer: 0
m_Name: Flowchart
m_TagString: Untagged
@ -289,6 +289,7 @@ MonoBehaviour:
description:
eventHandler: {fileID: 574033267}
commandList:
- {fileID: 574033273}
- {fileID: 574033272}
- {fileID: 574033266}
--- !u!114 &574033269
@ -317,7 +318,7 @@ MonoBehaviour:
height: 859
selectedBlock: {fileID: 574033268}
selectedCommands:
- {fileID: 574033266}
- {fileID: 574033273}
variables: []
description:
stepPause: 0
@ -336,10 +337,9 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_RootOrder: 5
--- !u!114 &574033272
MonoBehaviour:
m_ObjectHideFlags: 2
@ -383,6 +383,28 @@ MonoBehaviour:
runAsCoroutine: 1
waitUntilFinished: 1
returnVariable: {fileID: 0}
--- !u!114 &574033273
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 574033265}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 71f455683d4ba4405b8dbba457159620, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 6
errorMessage:
indentLevel: 0
luaEnvironment: {fileID: 1332690758}
luaFile: {fileID: 0}
luaScript: "conversation [[\r\nWhat is that smell??\nsherlock: Ugh.\r\nS bored:ew!\r\nwatson
annoyed left: That was me.\r\nW confident right: I'm not Sorry!\r\nW confident
offscreenright: GOOD DAY!\n]]"
runAsCoroutine: 1
waitUntilFinished: 1
returnVariable: {fileID: 0}
--- !u!1 &653560666 stripped
GameObject:
m_PrefabParentObject: {fileID: 110274, guid: c6289d5f8fa843145a2355af9cb09719, type: 2}
@ -470,10 +492,9 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_RootOrder: 3
--- !u!1 &856587351
GameObject:
m_ObjectHideFlags: 0
@ -545,10 +566,81 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_RootOrder: 8
--- !u!1 &1332690755
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 100640, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1332690759}
- 114: {fileID: 1332690756}
- 114: {fileID: 1332690758}
- 114: {fileID: 1332690757}
m_Layer: 0
m_Name: LuaEnvironment
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1332690756
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1332690755}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3e93d27e4a7119643a1592b568a905df, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1332690757
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11486636, guid: 49031c561e16d4fcf91c12153f8e0b25,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1332690755}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c10f0b861365b42b0928858f7b086ff3, type: 3}
m_Name:
m_EditorClassIdentifier:
fungusModule: 0
activeLanguage: en
stringTables: []
registerTypes:
- {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3}
- {fileID: 4900000, guid: 93fddea8208764a2dbb189cc238aed40, type: 3}
--- !u!114 &1332690758
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11493126, guid: 49031c561e16d4fcf91c12153f8e0b25,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1332690755}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ba19c26c1ba7243d2b57ebc4329cc7c6, type: 3}
m_Name:
m_EditorClassIdentifier:
remoteDebugger: 0
--- !u!4 &1332690759
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1332690755}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
--- !u!1 &1756024128
GameObject:
m_ObjectHideFlags: 1
@ -586,10 +678,9 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_RootOrder: 4
--- !u!1 &1818980091
GameObject:
m_ObjectHideFlags: 0
@ -637,10 +728,9 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_RootOrder: 6
--- !u!1 &1876668763
GameObject:
m_ObjectHideFlags: 0
@ -688,10 +778,9 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_RootOrder: 7
--- !u!1001 &2059718440
Prefab:
m_ObjectHideFlags: 0

Loading…
Cancel
Save