An easy to use Unity 3D library for creating illustrated Interactive Fiction games and more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.1 KiB

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Fungus.Script
{
[CommandInfo("Dialog",
"Choose",
"Presents a list of options for the player to choose from, with an optional timeout. Add options using preceding AddOption commands.")]
public class Choose : FungusCommand
{
public class Option
{
public string optionText;
public Sequence targetSequence;
}
static public List<Option> options = new List<Option>();
public string chooseText;
public Character character;
public AudioClip voiceOverClip;
public float timeoutDuration;
bool showBasicGUI;
public override void OnEnter()
{
ChooseDialog dialog = SetChooseDialog.activeDialog;
showBasicGUI = false;
if (dialog == null)
{
showBasicGUI = true;
return;
}
if (options.Count == 0)
{
Continue();
}
else
{
dialog.ShowDialog(true);
dialog.SetCharacter(character);
List<ChooseDialog.Option> dialogOptions = new List<ChooseDialog.Option>();
foreach (Option option in options)
{
ChooseDialog.Option dialogOption = new ChooseDialog.Option();
dialogOption.text = option.optionText;
Sequence onSelectSequence = option.targetSequence;
dialogOption.onSelect = delegate {
dialog.ShowDialog(false);
if (onSelectSequence == null)
{
Continue ();
}
else
{
ExecuteSequence(onSelectSequence);
}
};
dialogOptions.Add(dialogOption);
}
options.Clear();
if (voiceOverClip != null)
{
MusicController.GetInstance().PlaySound(voiceOverClip, 1f);
}
dialog.Choose(chooseText, dialogOptions, timeoutDuration, delegate {
dialog.ShowDialog(false);
Continue();
});
}
}
public override string GetSummary()
{
return "\"" + chooseText + "\"";
}
public override void GetConnectedSequences (ref List<Sequence> connectedSequences)
{
foreach (Option option in options)
{
if (option.targetSequence != null)
{
connectedSequences.Add(option.targetSequence);
}
}
}
void OnGUI()
{
if (!showBasicGUI)
{
return;
}
// Draw a basic GUI to use when no uGUI dialog has been set
// Does not support drawing character images
GUILayout.BeginHorizontal(GUILayout.Width(Screen.width));
GUILayout.FlexibleSpace();
GUILayout.BeginVertical(GUILayout.Height(Screen.height));
GUILayout.FlexibleSpace();
GUILayout.BeginVertical(new GUIStyle(GUI.skin.box));
if (character != null)
{
GUILayout.Label(character.nameText);
GUILayout.Space(10);
}
GUILayout.Label(chooseText);
foreach (Option option in options)
{
if (GUILayout.Button(option.optionText))
{
options.Clear();
showBasicGUI = false;
ExecuteSequence(option.targetSequence);
}
}
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
public override Color GetButtonColor()
{
return new Color32(184, 210, 235, 255);
}
}
}