PlayerMove 스크립트 Player로 수정

This commit is contained in:
김민구
2026-07-06 20:06:06 +09:00
parent d678ca4f17
commit da5ce5e135
2 changed files with 93 additions and 5 deletions
@@ -14,7 +14,7 @@ public class InputContoller
}
}
public class PlayerMove : MonoBehaviour
public class Player : MonoBehaviour
{
// 컴포넌트 프로퍼티
public Rigidbody2D rb { get; private set; }
@@ -36,6 +36,20 @@ public class PlayerMove : MonoBehaviour
private Animator animator;
// 캐릭터 생성 정보 캐싱
private string characterName = "데이터 로딩 중...";
private int generation = 1;
public int Generation => generation;
public string CharacterName => characterName;
private static readonly string[] RANDOM_NAMES = new string[]
{
"자르반", "가렌", "럭스", "다리우스", "야스오", "티모", "아리", "이즈리얼",
"조이", "제이스", "카이사", "제드", "탈론", "리븐", "애쉬", "트린다미어",
"소나", "룰루", "유미", "레오나", "다이애나", "세주아니", "우디르", "신 짜오"
};
void Awake()
{
// 1. Rigidbody2D 할당
@@ -58,6 +72,12 @@ public class PlayerMove : MonoBehaviour
maxHp = hp;
}
void Start()
{
// 시작 시 백엔드로부터 다음 세대 정보 조회
StartCoroutine(GetNextGenerationFromServer());
}
private void Update()
{
if (hp <= 0)
@@ -166,6 +186,9 @@ public class PlayerMove : MonoBehaviour
animator.Update(0f);
}
Debug.Log($"플레이어 체력이 {hp}로 회복되었습니다.");
// 체력 복구 시 다음 캐릭터 이름 및 세대 정보 갱신
StartCoroutine(GetNextGenerationFromServer());
}
// 임시 디버그용 화면 버튼 (게임 실행 시 화면 좌측 상단에 뜸)
@@ -189,13 +212,66 @@ public class PlayerMove : MonoBehaviour
{
DebugRestoreHP();
}
// 캐릭터 정보 UI 노출
GUI.color = Color.white;
GUI.skin.label.fontSize = 16;
GUI.Label(new Rect(10, 200, 400, 30), $"이름: {characterName}");
GUI.Label(new Rect(10, 230, 400, 30), $"세대: {generation}대손");
}
// 백엔드로 사망 원인을 전송하는 코루틴
// 백엔드로부터 다음 세대 번호를 받아와 랜덤 이름과 매핑하는 코루틴
private System.Collections.IEnumerator GetNextGenerationFromServer()
{
string url = "http://127.0.0.1:8000/api/game/generation/next";
using (UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.Get(url))
{
yield return request.SendWebRequest();
if (request.result == UnityEngine.Networking.UnityWebRequest.Result.ConnectionError ||
request.result == UnityEngine.Networking.UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("다음 세대 번호 조회 실패: " + request.error);
// 실패 시 로컬 기본값 적용
generation = 1;
}
else
{
try
{
string json = request.downloadHandler.text;
NextGenerationResponse response = JsonUtility.FromJson<NextGenerationResponse>(json);
generation = response.next_generation;
Debug.Log($"서버로부터 다음 세대 번호({generation}대) 조회 성공.");
}
catch (System.Exception ex)
{
Debug.LogError("세대 JSON 파싱 에러: " + ex.Message);
generation = 1;
}
}
}
// 이름 랜덤 선택 후 세대 접미사 추가 (예: 자르반 3세)
string baseName = RANDOM_NAMES[Random.Range(0, RANDOM_NAMES.Length)];
characterName = $"{baseName} {generation}세";
Debug.Log($"캐릭터 생성 완료! 이름: {characterName}, 세대: {generation}대");
}
// 백엔드로 완성된 사망 기록을 전송하는 코루틴
private System.Collections.IEnumerator SendDeathReport(string cause)
{
string url = "http://127.0.0.1:8000/api/game/death";
DeathRecordRequest requestData = new DeathRecordRequest { cause_of_death = cause };
AncestorDeathCreate requestData = new AncestorDeathCreate
{
name = characterName,
generation = generation,
cause_of_death = cause,
floor = 1 // 장소는 임시로 1층 고정
};
string json = JsonUtility.ToJson(requestData);
using (UnityEngine.Networking.UnityWebRequest request = new UnityEngine.Networking.UnityWebRequest(url, "POST"))
@@ -215,14 +291,26 @@ public class PlayerMove : MonoBehaviour
else
{
Debug.Log("사망 기록 전송 성공: " + request.downloadHandler.text);
// 사망 전송 성공 즉시 다음 세대 정보 받아와 자동 갱신
StartCoroutine(GetNextGenerationFromServer());
}
}
}
}
// JSON 파싱용 요청 스키마 객체
// JSON 파싱용 세대 응답 스키마 객체
[System.Serializable]
public class DeathRecordRequest
public class NextGenerationResponse
{
public int next_generation;
}
// JSON 전송용 사망 기록 객체
[System.Serializable]
public class AncestorDeathCreate
{
public string name;
public int generation;
public string cause_of_death;
public int floor;
}