코드 리팩토링
재사용성 및 확장성을 고려하여 코드 전반을 리팩토링함
This commit is contained in:
@@ -1,25 +1,63 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Handles equipment visuals for the player.
|
||||
/// Uses the new EquipmentSlot system while maintaining backwards compatibility.
|
||||
/// </summary>
|
||||
public class PlayerEquipmentHandler : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private Transform toolAnchor; // 캐릭터 손의 소켓 위치
|
||||
[Header("Equipment Settings")]
|
||||
[SerializeField] private Transform mainHandAnchor;
|
||||
[SerializeField] private Transform offHandAnchor;
|
||||
|
||||
[Header("Slot Configuration")]
|
||||
[SerializeField] private EquipmentSlot mainHandSlot;
|
||||
|
||||
private PlayerInventory _inventory;
|
||||
private GameObject _currentToolInstance; // 현재 생성된 도구 모델
|
||||
private GameObject _currentToolInstance;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_inventory = GetComponent<PlayerInventory>();
|
||||
|
||||
// Initialize main hand slot if not configured in inspector
|
||||
if (mainHandSlot == null)
|
||||
{
|
||||
mainHandSlot = new EquipmentSlot
|
||||
{
|
||||
SlotType = EquipmentSlotType.MainHand,
|
||||
AttachPoint = mainHandAnchor
|
||||
};
|
||||
}
|
||||
else if (mainHandSlot.AttachPoint == null)
|
||||
{
|
||||
mainHandSlot.AttachPoint = mainHandAnchor;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// 인벤토리의 슬롯 변경 이벤트 구독
|
||||
// OnSlotChanged는 (이전 값, 새 값) 두 개의 인자를 전달합니다.
|
||||
_inventory.OnSlotChanged += HandleSlotChanged;
|
||||
// Subscribe to inventory slot changes
|
||||
if (_inventory != null)
|
||||
{
|
||||
_inventory.OnSlotChanged += HandleSlotChanged;
|
||||
|
||||
// 게임 시작 시 처음에 들고 있는 아이템 모델 생성
|
||||
UpdateEquippedModel(_inventory.SelectedSlotIndex);
|
||||
// Initialize with current slot
|
||||
UpdateEquippedModel(_inventory.SelectedSlotIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
// Unsubscribe to prevent memory leaks
|
||||
if (_inventory != null)
|
||||
{
|
||||
_inventory.OnSlotChanged -= HandleSlotChanged;
|
||||
}
|
||||
|
||||
// Clean up equipment
|
||||
mainHandSlot?.Unequip();
|
||||
}
|
||||
|
||||
private void HandleSlotChanged(int previousValue, int newValue)
|
||||
@@ -29,30 +67,58 @@ public class PlayerEquipmentHandler : NetworkBehaviour
|
||||
|
||||
private void UpdateEquippedModel(int slotIndex)
|
||||
{
|
||||
// 1. 기존 도구가 있다면 파괴
|
||||
if (_currentToolInstance != null)
|
||||
// Get item data for the selected slot
|
||||
ItemData data = _inventory?.GetItemDataInSlot(slotIndex);
|
||||
|
||||
// Use new equipment slot system
|
||||
if (data != null && data.CanBeEquipped)
|
||||
{
|
||||
Destroy(_currentToolInstance);
|
||||
// Use IEquippableItem interface
|
||||
mainHandSlot.Equip(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
mainHandSlot.Unequip();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
// Update legacy reference for any code that might check it
|
||||
_currentToolInstance = mainHandSlot.CurrentEquipment;
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
/// <summary>
|
||||
/// Get the currently equipped item data.
|
||||
/// </summary>
|
||||
public ItemData GetEquippedItem()
|
||||
{
|
||||
// 이벤트 구독 해제 (메모리 누수 방지)
|
||||
if (_inventory != null)
|
||||
_inventory.OnSlotChanged -= HandleSlotChanged;
|
||||
return mainHandSlot?.EquippedItem;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the currently equipped tool instance.
|
||||
/// </summary>
|
||||
public GameObject GetCurrentToolInstance()
|
||||
{
|
||||
return mainHandSlot?.CurrentEquipment ?? _currentToolInstance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if player has equipment in main hand.
|
||||
/// </summary>
|
||||
public bool HasMainHandEquipment => mainHandSlot?.HasEquipment ?? false;
|
||||
|
||||
/// <summary>
|
||||
/// Force refresh the equipped model.
|
||||
/// </summary>
|
||||
public void RefreshEquipment()
|
||||
{
|
||||
if (_inventory != null)
|
||||
{
|
||||
UpdateEquippedModel(_inventory.SelectedSlotIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the main hand equipment slot for advanced usage.
|
||||
/// </summary>
|
||||
public EquipmentSlot MainHandSlot => mainHandSlot;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user