장비 개념 추가 및 Kaykit 애셋 추가
This commit is contained in:
58
Assets/Scripts/Player/PlayerEquipmentHandler.cs
Normal file
58
Assets/Scripts/Player/PlayerEquipmentHandler.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerEquipmentHandler : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private Transform toolAnchor; // 캐릭터 손의 소켓 위치
|
||||
private PlayerInventory _inventory;
|
||||
private GameObject _currentToolInstance; // 현재 생성된 도구 모델
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_inventory = GetComponent<PlayerInventory>();
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// 인벤토리의 슬롯 변경 이벤트 구독
|
||||
// OnSlotChanged는 (이전 값, 새 값) 두 개의 인자를 전달합니다.
|
||||
_inventory.OnSlotChanged += HandleSlotChanged;
|
||||
|
||||
// 게임 시작 시 처음에 들고 있는 아이템 모델 생성
|
||||
UpdateEquippedModel(_inventory.SelectedSlotIndex);
|
||||
}
|
||||
|
||||
private void HandleSlotChanged(int previousValue, int newValue)
|
||||
{
|
||||
UpdateEquippedModel(newValue);
|
||||
}
|
||||
|
||||
private void UpdateEquippedModel(int slotIndex)
|
||||
{
|
||||
// 1. 기존 도구가 있다면 파괴
|
||||
if (_currentToolInstance != null)
|
||||
{
|
||||
Destroy(_currentToolInstance);
|
||||
}
|
||||
|
||||
// 2. 현재 선택된 슬롯의 데이터 확인
|
||||
ItemData data = _inventory.GetItemDataInSlot(slotIndex);
|
||||
|
||||
// 3. 도구인 경우에만 모델 생성
|
||||
if (data != null && data.isTool && data.toolPrefab != null)
|
||||
{
|
||||
_currentToolInstance = Instantiate(data.toolPrefab, toolAnchor);
|
||||
|
||||
// ItemData에 설정된 오프셋 적용
|
||||
_currentToolInstance.transform.localPosition = data.equipPositionOffset;
|
||||
_currentToolInstance.transform.localRotation = Quaternion.Euler(data.equipRotationOffset);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
// 이벤트 구독 해제 (메모리 누수 방지)
|
||||
if (_inventory != null)
|
||||
_inventory.OnSlotChanged -= HandleSlotChanged;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user