Add audio to dialogues

This commit is contained in:
Michal Pikulski
2025-10-17 15:20:29 +02:00
parent 2fe872d8dc
commit c3c9fd95a4
14 changed files with 322 additions and 46 deletions

View File

@@ -9,6 +9,7 @@ using PuzzleS;
namespace Dialogue
{
[AddComponentMenu("AppleHills/Dialogue/Dialogue Component")]
[RequireComponent(typeof(AudioSource))]
public class DialogueComponent : MonoBehaviour
{
[SerializeField] private RuntimeDialogueGraph dialogueGraph;
@@ -17,6 +18,7 @@ namespace Dialogue
private int currentLineIndex;
private bool initialized = false;
private SpeechBubble speechBubble;
private AudioSource audioSource;
// Flag to track when a condition has been met but dialogue hasn't advanced yet
private bool _conditionSatisfiedPendingAdvance = false;
@@ -35,6 +37,15 @@ namespace Dialogue
private void Start()
{
// Get required components
audioSource = GetComponent<AudioSource>();
speechBubble = GetComponentInChildren<SpeechBubble>();
if (speechBubble == null)
{
Debug.LogError("SpeechBubble component is missing on Dialogue Component");
}
// Register for global events
if (PuzzleManager.Instance != null)
PuzzleManager.Instance.OnStepCompleted += OnAnyPuzzleStepCompleted;
@@ -49,13 +60,6 @@ namespace Dialogue
ItemManager.Instance.OnItemsCombined += OnAnyItemsCombined;
}
speechBubble = GetComponentInChildren<SpeechBubble>();
if (speechBubble == null)
{
Debug.LogError("SpeechBubble component is missing on Dialogue Component");
}
// Auto-start the dialogue
// StartDialogue();
@@ -88,6 +92,9 @@ namespace Dialogue
// and pass whether there are more lines available for prompt display
speechBubble.DisplayDialogueContent(content, HasAnyLines());
// Play audio if available
PlayDialogueAudio(content.Audio);
// Log the content type for debugging
Logging.Debug($"Displaying content type: {content.ContentType} - {(content.ContentType == DialogueContentType.Text ? content.Text : content.Image?.name)}");
}
@@ -101,7 +108,28 @@ namespace Dialogue
Logging.Debug($"Displaying legacy text: {line}");
}
}
/// <summary>
/// Play the audio clip for the current dialogue content
/// </summary>
/// <param name="clip">Audio clip to play</param>
private void PlayDialogueAudio(AudioClip clip)
{
// Stop any currently playing audio
if (audioSource.isPlaying)
{
audioSource.Stop();
}
// Play the new clip if it exists
if (clip != null)
{
audioSource.clip = clip;
audioSource.Play();
Logging.Debug($"Playing dialogue audio: {clip.name}");
}
}
/// <summary>
/// Get the current dialogue content (text or image)
/// </summary>

View File

@@ -21,6 +21,7 @@ namespace Dialogue
[SerializeField] private DialogueContentType _contentType = DialogueContentType.Text;
[SerializeField] private string _text = string.Empty;
[SerializeField] private Sprite _image = null;
[SerializeField] private AudioClip _audio = null;
/// <summary>
/// The type of content this entry contains
@@ -37,16 +38,23 @@ namespace Dialogue
/// </summary>
public Sprite Image => _image;
/// <summary>
/// The audio clip to play with this content
/// </summary>
public AudioClip Audio => _audio;
/// <summary>
/// Create text content
/// </summary>
/// <param name="text">The text to display</param>
public static DialogueContent CreateText(string text)
/// <param name="audio">Optional audio clip to play</param>
public static DialogueContent CreateText(string text, AudioClip audio = null)
{
return new DialogueContent
{
_contentType = DialogueContentType.Text,
_text = text
_text = text,
_audio = audio
};
}
@@ -54,12 +62,14 @@ namespace Dialogue
/// Create image content
/// </summary>
/// <param name="image">The image to display</param>
public static DialogueContent CreateImage(Sprite image)
/// <param name="audio">Optional audio clip to play</param>
public static DialogueContent CreateImage(Sprite image, AudioClip audio = null)
{
return new DialogueContent
{
_contentType = DialogueContentType.Image,
_image = image
_image = image,
_audio = audio
};
}
@@ -68,9 +78,13 @@ namespace Dialogue
/// </summary>
public override string ToString()
{
return ContentType == DialogueContentType.Text
string contentDesc = ContentType == DialogueContentType.Text
? $"Text: {_text}"
: $"Image: {_image?.name ?? "None"}";
return _audio != null
? $"{contentDesc} (with audio: {_audio.name})"
: contentDesc;
}
}
}