Browse Source

Added punctuation pause

master
chrisgregan 10 years ago
parent
commit
e0358c316d
  1. 69
      Assets/Fungus/Dialog/Scripts/Dialog.cs
  2. BIN
      Assets/Shuttle/ShuttleGame.unity

69
Assets/Fungus/Dialog/Scripts/Dialog.cs

@ -17,9 +17,10 @@ namespace Fungus.Script
Right Right
}; };
public float writingSpeed; public float writingSpeed = 60;
public AudioClip writingSound; public AudioClip writingSound;
public bool loopWritingSound = true; public bool loopWritingSound = true;
public float punctuationPause = 0.25f;
public Canvas dialogCanvas; public Canvas dialogCanvas;
public Text nameText; public Text nameText;
@ -27,7 +28,8 @@ namespace Fungus.Script
public Image leftImage; public Image leftImage;
public Image rightImage; public Image rightImage;
float speed; float currentSpeed;
float currentPunctuationPause;
bool boldActive; bool boldActive;
bool italicActive; bool italicActive;
bool colorActive; bool colorActive;
@ -130,7 +132,8 @@ namespace Fungus.Script
italicActive = false; italicActive = false;
colorActive = false; colorActive = false;
colorText = ""; colorText = "";
speed = writingSpeed; currentSpeed = writingSpeed;
currentPunctuationPause = punctuationPause;
List<Glyph> glyphs = MakeGlyphList(text); List<Glyph> glyphs = MakeGlyphList(text);
@ -141,7 +144,7 @@ namespace Fungus.Script
// Zero speed means write instantly // Zero speed means write instantly
// Also write instantly if text contains rich text markup tags // Also write instantly if text contains rich text markup tags
if (speed == 0 || if (currentSpeed == 0 ||
text.Contains("<")) text.Contains("<"))
{ {
foreach (Glyph glyph in glyphs) foreach (Glyph glyph in glyphs)
@ -171,9 +174,9 @@ namespace Fungus.Script
} }
float writeDelay = 0f; float writeDelay = 0f;
if (speed > 0) if (currentSpeed > 0)
{ {
writeDelay = (1f / (float)speed); writeDelay = (1f / (float)currentSpeed);
} }
float timeAccumulator = 0f; float timeAccumulator = 0f;
@ -221,6 +224,20 @@ namespace Fungus.Script
storyText.text += start + glyph.param + end; storyText.text += start + glyph.param + end;
} }
// Add a wait glyph on punctuation marks
if (punctuationPause > 0 &&
IsPunctuation(glyph.param))
{
// Ignore if next glyph is also punctuation, or if punctuation is the last character.
bool skip = (i < glyphs.Count - 1 &&
glyphs[i + 1].type == GlyphType.Character &&
IsPunctuation(glyphs[i + 1].param));
if (!skip)
yield return new WaitForSeconds(currentPunctuationPause);
}
break; break;
case GlyphType.BoldStart: case GlyphType.BoldStart:
@ -276,14 +293,14 @@ namespace Fungus.Script
break; break;
case GlyphType.Speed: case GlyphType.Speed:
if (!Single.TryParse(glyph.param, out speed)) if (!Single.TryParse(glyph.param, out currentSpeed))
{ {
speed = 0f; currentSpeed = 0f;
} }
writeDelay = 0; writeDelay = 0;
if (speed > 0) if (currentSpeed > 0)
{ {
writeDelay = (1f / (float)speed); writeDelay = (1f / (float)currentSpeed);
} }
break; break;
@ -302,6 +319,10 @@ namespace Fungus.Script
yield break; yield break;
case GlyphType.WaitOnPunctuation: case GlyphType.WaitOnPunctuation:
if (!Single.TryParse(glyph.param, out currentPunctuationPause))
{
currentPunctuationPause = 0f;
}
break; break;
} }
@ -340,6 +361,14 @@ namespace Fungus.Script
} }
} }
bool IsPunctuation(string character)
{
return character == "." ||
character == "?" ||
character == "!" ||
character == ",";
}
List<Glyph> MakeGlyphList(string storyText) List<Glyph> MakeGlyphList(string storyText)
{ {
List<Glyph> glyphList = new List<Glyph>(); List<Glyph> glyphList = new List<Glyph>();
@ -358,14 +387,10 @@ namespace Fungus.Script
foreach (char c in preText) foreach (char c in preText)
{ {
glyphList.Add(MakeCharacterGlyph(c.ToString())); AddCharacterGlyph(glyphList, c.ToString());
} }
Glyph tagGlyph = MakeTagGlyph(tagText); AddTagGlyph(glyphList, tagText);
if (tagGlyph != null)
{
glyphList.Add (tagGlyph);
}
position = m.Index + tagText.Length; position = m.Index + tagText.Length;
m = m.NextMatch(); m = m.NextMatch();
@ -376,28 +401,28 @@ namespace Fungus.Script
string postText = storyText.Substring(position, storyText.Length - position); string postText = storyText.Substring(position, storyText.Length - position);
foreach (char c in postText) foreach (char c in postText)
{ {
glyphList.Add(MakeCharacterGlyph(c.ToString())); AddCharacterGlyph(glyphList, c.ToString());
} }
} }
return glyphList; return glyphList;
} }
Glyph MakeCharacterGlyph(string character) void AddCharacterGlyph(List<Glyph> glyphList, string character)
{ {
Glyph glyph = new Glyph(); Glyph glyph = new Glyph();
glyph.type = GlyphType.Character; glyph.type = GlyphType.Character;
glyph.param = character; glyph.param = character;
return glyph; glyphList.Add(glyph);
} }
Glyph MakeTagGlyph(string tagText) void AddTagGlyph(List<Glyph> glyphList, string tagText)
{ {
if (tagText.Length < 3 || if (tagText.Length < 3 ||
tagText.Substring(0,1) != "{" || tagText.Substring(0,1) != "{" ||
tagText.Substring(tagText.Length - 1,1) != "}") tagText.Substring(tagText.Length - 1,1) != "}")
{ {
return null; return;
} }
string tag = tagText.Substring(1, tagText.Length - 2); string tag = tagText.Substring(1, tagText.Length - 2);
@ -478,7 +503,7 @@ namespace Fungus.Script
glyph.type = type; glyph.type = type;
glyph.param = paramText.Trim(); glyph.param = paramText.Trim();
return glyph; glyphList.Add(glyph);
} }
protected IEnumerator WaitForInput(Action onInput) protected IEnumerator WaitForInput(Action onInput)

BIN
Assets/Shuttle/ShuttleGame.unity

Binary file not shown.
Loading…
Cancel
Save