다이얼로그 작업중
This commit is contained in:
8
Assets/02_Scripts/Communication/Dialog/DialogChoice.cs
Normal file
8
Assets/02_Scripts/Communication/Dialog/DialogChoice.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
[Serializable]
|
||||
public class DialogChoice
|
||||
{
|
||||
public DialogNode DestinationNode;
|
||||
public string ChoiceText;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bd5ea8e7c904dd24e967d91b86004efb
|
||||
8
Assets/02_Scripts/Communication/Dialog/DialogGroup.cs
Normal file
8
Assets/02_Scripts/Communication/Dialog/DialogGroup.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(menuName = "Communication/Dialog Group")]
|
||||
public class DialogGroup : ScriptableObject
|
||||
{
|
||||
public string DialogGroupName;
|
||||
public DialogNode StartNode;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 72ef984dbd5eb29498ece3d2dae297ea
|
||||
31
Assets/02_Scripts/Communication/Dialog/DialogNode.cs
Normal file
31
Assets/02_Scripts/Communication/Dialog/DialogNode.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(menuName = "Communication/Dialog Node")]
|
||||
public class DialogNode : ScriptableObject
|
||||
{
|
||||
[Header("Speaker")]
|
||||
public CharacterData Speaker;
|
||||
|
||||
[Header("Content")]
|
||||
[TextArea(2,5)] public string TalkText;
|
||||
public GestureData Gesture;
|
||||
public ExpressionData Expression;
|
||||
public VoiceClip Voice;
|
||||
public float LineDuration; //자동 넘김 시간
|
||||
//LineDuration=0 → 플레이어 입력 대기 (수동)
|
||||
//Voice 있음 → 클립 길이만큼 대기
|
||||
//Voice 없음 → LineDuration 대기
|
||||
|
||||
|
||||
[Header("Behavior")]
|
||||
public bool LookAtPlayer;
|
||||
|
||||
[Header("Flow")]
|
||||
public DialogNode Next; // 선택지 없을 때 자동으로 갈 노드
|
||||
public List<DialogChoice> Choices; // 있으면 플레이어 선택 대기
|
||||
|
||||
[Header("ChoiceQuestion")]
|
||||
[TextArea(2,5)] public string ChoiceQuestion;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb30abdb4b5671b458096d48d11c7f27
|
||||
196
Assets/02_Scripts/Communication/Dialog/DialogPlayer.cs
Normal file
196
Assets/02_Scripts/Communication/Dialog/DialogPlayer.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CharacterVoiceObject))]
|
||||
public class DialogPlayer : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private List<DialogGroup> _dialogGroups;
|
||||
private Dictionary<string, DialogGroup> _dialogGroupMap;
|
||||
private Animator _animator;
|
||||
private int _initialGestureHash;
|
||||
private int _initialExpressionHash;
|
||||
private bool _hasInitialExpression;
|
||||
private readonly Dictionary<Transform, Quaternion> _originalRotations = new();
|
||||
public bool IsPlaying { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_dialogGroupMap = new Dictionary<string, DialogGroup>();
|
||||
foreach (var g in _dialogGroups)
|
||||
_dialogGroupMap[g.DialogGroupName] = g;
|
||||
|
||||
_animator = GetComponentInChildren<Animator>();
|
||||
|
||||
if (_animator != null)
|
||||
{
|
||||
_initialGestureHash = _animator.GetCurrentAnimatorStateInfo(0).fullPathHash;
|
||||
if (_animator.layerCount > 1)
|
||||
{
|
||||
_initialExpressionHash = _animator.GetCurrentAnimatorStateInfo(1).fullPathHash;
|
||||
_hasInitialExpression = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
if(_dialogGroups.Count > 0)
|
||||
_ = Play(_dialogGroups[0].DialogGroupName);
|
||||
|
||||
}
|
||||
|
||||
public async Awaitable Play(string groupName)
|
||||
{
|
||||
if (IsPlaying) return;
|
||||
if (!_dialogGroupMap.TryGetValue(groupName, out var group))
|
||||
{
|
||||
Debug.LogWarning($"[DialogPlayer] 그룹 없음: {groupName}");
|
||||
return;
|
||||
}
|
||||
|
||||
IsPlaying = true;
|
||||
try
|
||||
{
|
||||
var node = group.StartNode;
|
||||
while (node != null)
|
||||
{
|
||||
await PlayNode(node);
|
||||
|
||||
if (node.Choices != null && node.Choices.Count > 0)
|
||||
{
|
||||
int picked = await WaitForChoice(node);
|
||||
node = node.Choices[picked].DestinationNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
node = node.Next;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsPlaying = false;
|
||||
RestoreDefaultAnimations();
|
||||
RestoreRotations();
|
||||
}
|
||||
|
||||
Debug.Log("[DialogPlayer] 대화 종료");
|
||||
}
|
||||
|
||||
private void RestoreDefaultAnimations()
|
||||
{
|
||||
if (_animator == null) return;
|
||||
_animator.CrossFade(_initialGestureHash, 0.3f, 0, normalizedTimeOffset: 0f);
|
||||
if (_hasInitialExpression)
|
||||
_animator.CrossFade(_initialExpressionHash, 0.3f, 1, normalizedTimeOffset: 0f);
|
||||
}
|
||||
|
||||
private async Awaitable RotateTowardPlayer(Transform target)
|
||||
{
|
||||
if (Camera.main == null) return;
|
||||
var playerCam = Camera.main.transform;
|
||||
|
||||
float duration = 0.5f;
|
||||
float elapsed = 0f;
|
||||
|
||||
while (elapsed < duration)
|
||||
{
|
||||
Vector3 dir = playerCam.position - target.position;
|
||||
dir.y = 0f;
|
||||
if (dir.sqrMagnitude > 0.0001f)
|
||||
{
|
||||
var targetRot = Quaternion.LookRotation(dir);
|
||||
target.rotation = Quaternion.Slerp(target.rotation, targetRot, 10f * Time.deltaTime);
|
||||
}
|
||||
elapsed += Time.deltaTime;
|
||||
await Awaitable.NextFrameAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private void RestoreRotations()
|
||||
{
|
||||
foreach (var kvp in _originalRotations)
|
||||
{
|
||||
if (kvp.Key != null)
|
||||
_ = RotateToRotation(kvp.Key, kvp.Value);
|
||||
}
|
||||
_originalRotations.Clear();
|
||||
}
|
||||
|
||||
private async Awaitable RotateToRotation(Transform target, Quaternion targetRotation)
|
||||
{
|
||||
float duration = 0.5f;
|
||||
float elapsed = 0f;
|
||||
while (elapsed < duration)
|
||||
{
|
||||
if (target == null) return;
|
||||
target.rotation = Quaternion.Slerp(target.rotation, targetRotation, 10f * Time.deltaTime);
|
||||
elapsed += Time.deltaTime;
|
||||
await Awaitable.NextFrameAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Awaitable PlayNode(DialogNode node)
|
||||
{
|
||||
var speakerName = node.Speaker != null ? node.Speaker.Name : "(누구?)";
|
||||
Debug.Log($"[{speakerName}] {node.TalkText}");
|
||||
|
||||
// 보이스 재생
|
||||
if (node.Voice != null && node.Speaker != null)
|
||||
{
|
||||
var voiceObj = CharacterVoiceObject.Find(node.Speaker);
|
||||
if (voiceObj != null && node.Voice.Clip != null)
|
||||
voiceObj.Play(node.Voice.Clip);
|
||||
}
|
||||
|
||||
// 플레이어 향해 회전
|
||||
if (node.LookAtPlayer && node.Speaker != null)
|
||||
{
|
||||
var voiceObj = CharacterVoiceObject.Find(node.Speaker);
|
||||
if (voiceObj != null)
|
||||
{
|
||||
_originalRotations.TryAdd(voiceObj.transform, voiceObj.transform.rotation);
|
||||
_ = RotateTowardPlayer(voiceObj.transform);
|
||||
}
|
||||
}
|
||||
|
||||
if (node.Gesture != null)
|
||||
_animator.CrossFade(node.Gesture.StateName, node.Gesture.CrossFadeDuration, node.Gesture.AnimationLayer);
|
||||
if (node.Expression != null)
|
||||
_animator.CrossFade(node.Expression.StateName, node.Expression.CrossFadeDuration, node.Expression.AnimationLayer);
|
||||
|
||||
// 대기 시간 결정
|
||||
float wait = 0f;
|
||||
if (node.Voice != null && node.Voice.Clip != null)
|
||||
wait = node.Voice.Clip.length;
|
||||
else
|
||||
wait = node.LineDuration;
|
||||
|
||||
if (wait > 0f)
|
||||
await Awaitable.WaitForSecondsAsync(wait);
|
||||
else
|
||||
await WaitForAdvanceInput(); // 수동 진행
|
||||
}
|
||||
|
||||
private async Awaitable<int> WaitForChoice(DialogNode node)
|
||||
{
|
||||
//선택을 기다리는 함수 수정해서 사용할것
|
||||
/*
|
||||
if (ChoiceHud.Instance == null)
|
||||
{
|
||||
Debug.LogWarning("[DialogPlayer] ChoiceHud 없음 — 0번 자동 선택");
|
||||
return 0;
|
||||
}
|
||||
return await ChoiceHud.Instance.Show(node.Speaker, node.ChoiceQuestion ,node.Choices);
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private async Awaitable WaitForAdvanceInput()
|
||||
{
|
||||
// TODO: VR 컨트롤러 버튼 입력 대기. 일단은 1초 대기
|
||||
await Awaitable.WaitForSecondsAsync(1f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9838e0e0a3edefa4e92ddbb98aaa3ce5
|
||||
Reference in New Issue
Block a user