콜라이더 충돌 시 npc 대사 출력 기능 구현 및 HP 바 기능 구현

This commit is contained in:
김민구
2026-07-08 03:50:56 +09:00
parent b160214799
commit 8f742e50d9
5 changed files with 412 additions and 373 deletions
@@ -4,9 +4,29 @@ using UnityEngine.Networking;
public class NPCDialogueManager : MonoBehaviour
{
[Header("UI 연결")]
public GameObject dialoguePanel; // 대화창 배경 패널
public TMPro.TextMeshProUGUI dialogueText; // 대사 텍스트 TMP 컴포넌트
private string lastDialogueText = "대화 버튼을 눌러 조언을 얻으세요.";
private string targetDialogueText = ""; // 목표 대사 텍스트
private Coroutine typingCoroutine; // 타이핑 코루틴 제어용
private string targetDialogueText = ""; // 목표 대사 텍스트
private Coroutine typingCoroutine; // 타이핑 코루틴 제어용
// JSON 파싱용 Dialogue 응답 매핑 클래스 (스코프 오류 방지를 위해 내부 클래스로 내장)
[System.Serializable]
public class DialogueResponse
{
public string dialogue;
}
private void Start()
{
// 시작 시 대화 패널을 비활성화해 둡니다.
if (dialoguePanel != null)
{
dialoguePanel.SetActive(false);
}
}
// NPC 대화를 요청하는 메서드
public void RequestNPCDialogue(int currentGeneration)
@@ -14,14 +34,39 @@ public class NPCDialogueManager : MonoBehaviour
StartCoroutine(FetchNPCDialogue(currentGeneration));
}
// NPC 영역을 벗어났을 때 대화창을 명시적으로 닫는 메서드
public void CloseDialogue()
{
if (typingCoroutine != null)
{
StopCoroutine(typingCoroutine);
typingCoroutine = null;
}
if (dialoguePanel != null)
{
dialoguePanel.SetActive(false); // 대화 배경 끄기
}
}
private IEnumerator FetchNPCDialogue(int currentGeneration)
{
// 1. 대기 연출: 요청 즉시 로딩중 텍스트를 띄워 렉처럼 보이지 않게 합니다.
// 1. 대화 패널 활성화 및 대기 연출 문구 노출
if (dialoguePanel != null)
{
dialoguePanel.SetActive(true);
}
if (typingCoroutine != null)
{
StopCoroutine(typingCoroutine);
}
lastDialogueText = "늙은 문지기 발두르가 눈을 지그시 감고 네 선조들의 최후를 떠올리는 중입니다...";
if (dialogueText != null)
{
dialogueText.text = lastDialogueText;
}
// FastAPI의 NPC 대화 조회 엔드포인트 URL 구성
string url = $"http://127.0.0.1:8000/api/game/npc-dialogue?current_generation={currentGeneration}";
@@ -36,6 +81,10 @@ public class NPCDialogueManager : MonoBehaviour
{
Debug.LogError($"[Dialogue] NPC 대사 조회 실패: {request.error}");
lastDialogueText = "문지기 발두르가 침묵하고 있습니다. (서버 연결 실패)";
if (dialogueText != null)
{
dialogueText.text = lastDialogueText;
}
}
else
{
@@ -54,6 +103,10 @@ public class NPCDialogueManager : MonoBehaviour
{
Debug.LogError($"[Dialogue] JSON 파싱 에러: {ex.Message}");
lastDialogueText = "문지기의 말을 이해할 수 없습니다. (데이터 파싱 오류)";
if (dialogueText != null)
{
dialogueText.text = lastDialogueText;
}
}
}
}
@@ -63,13 +116,18 @@ public class NPCDialogueManager : MonoBehaviour
private IEnumerator TypeTextEffect(string fullText)
{
lastDialogueText = "";
// 문지기가 고민하면서 한 글자씩 읊조리는 속도 (0.04초 간격)
float letterDelay = 0.04f;
foreach (char letter in fullText.ToCharArray())
{
lastDialogueText += letter;
// 텍스트 UI 컴포넌트 갱신
if (dialogueText != null)
{
dialogueText.text = lastDialogueText;
}
// 쉼표(,)나 마침표(.) 등 문장이 끊기는 지점에서는 살짝 더 더듬는 효과(딜레이 추가) 제공
if (letter == ',' || letter == '.')
{
@@ -83,15 +141,13 @@ public class NPCDialogueManager : MonoBehaviour
typingCoroutine = null;
}
// 디버그용 임시 GUI 버튼 및 대사 출력창
// 디버그용 임시 OnGUI 버튼
private void OnGUI()
{
GUI.backgroundColor = Color.yellow;
// NPC 대화 요청 버튼 (화면 우측 상단 배치)
if (GUI.Button(new Rect(Screen.width - 240, 10, 220, 50), "Debug: Get NPC Dialogue"))
{
// 플레이어 오브젝트를 찾아 현재 세대 값을 가져옵니다.
Player player = FindObjectOfType<Player>();
if (player != null)
{
@@ -103,21 +159,16 @@ public class NPCDialogueManager : MonoBehaviour
}
}
// 대사 출력 박스 배경 및 텍스트 렌더링
GUI.color = Color.white;
GUI.Box(new Rect(Screen.width - 450, 70, 430, 120), "늙은 문지기 '발두르'의 조언");
GUIStyle dialogueStyle = new GUIStyle(GUI.skin.label);
dialogueStyle.wordWrap = true;
dialogueStyle.fontSize = 14;
GUI.Label(new Rect(Screen.width - 440, 95, 410, 90), lastDialogueText, dialogueStyle);
if (dialoguePanel != null && dialoguePanel.activeSelf)
{
GUI.color = Color.white;
GUI.Box(new Rect(Screen.width - 450, 70, 430, 120), "늙은 문지기 '발두르'의 조언 (Debug)");
GUIStyle dialogueStyle = new GUIStyle(GUI.skin.label);
dialogueStyle.wordWrap = true;
dialogueStyle.fontSize = 14;
GUI.Label(new Rect(Screen.width - 440, 95, 410, 90), lastDialogueText, dialogueStyle);
}
}
}
// JSON 파싱용 Dialogue 응답 매핑 클래스
[System.Serializable]
public class DialogueResponse
{
public string dialogue;
}